sentry-sdk-learning
v1.0.5
Published
A custom Sentry SDK for learning
Maintainers
Readme
Sentry SDK Learning
A lightweight, custom-built Sentry SDK for learning purposes. This library demonstrates the core concepts of error tracking, including automatic error capture, DSN parsing, and event transport.
🚀 Features
- ✅ Automatic Error Capture: Catches uncaught exceptions (
window.onerror) - ✅ Promise Rejection Handling: Catches unhandled Promise rejections
- ✅ Manual Capture: Report handled errors with
captureException - ✅ Smart DSN Parsing: Automatically extracts project ID and auth keys
- ✅ Stack Trace Parsing: Formats stack traces for Sentry dashboard
- ✅ Zero Dependencies: Built from scratch with vanilla JavaScript
📦 Installation
Install the package via npm:
npm install sentry-sdk-learning🛠 Usage
1. Initialize the SDK
Option A: Using the Provider (Recommended for React)
Wrap your application with SentryProvider. This handles initialization AND automatically catches render errors in your component tree.
import { SentryProvider } from 'sentry-sdk-learning';
function App() {
return (
<SentryProvider config={{
dsn: 'YOUR_SENTRY_DSN_HERE',
environment: 'production',
release: 'v1.0.0'
}}>
<YourApp />
</SentryProvider>
);
}Option B: Manual Initialization
Import and initialize the SDK at the entry point of your application (e.g., main.jsx, App.js, or index.js).
import Sentry from 'sentry-sdk-learning';
Sentry.init({
dsn: 'YOUR_SENTRY_DSN_HERE',
environment: 'production', // optional (default: production)
release: 'v1.0.0' // optional (default: unknown)
});2. Manual Error Capture
You can manually report errors that are caught in try-catch blocks:
try {
// Your risky code
throw new Error("Something went wrong!");
} catch (error) {
Sentry.captureException(error);
}3. Automatic Capture
The SDK automatically listens for:
- Uncaught JavaScript errors
- Unhandled Promise rejections
No extra code is needed! Just initialize the SDK, and it will watch over your app.
📖 API Reference
init(options)
Initializes the SDK. Must be called before using other methods.
| Option | Type | Required | Description |
|--------|------|----------|-------------|
| dsn | string | Yes | Your Sentry DSN URL |
| environment | string | No | Environment name (e.g., 'dev', 'prod') |
| release | string | No | Application version string |
| beforeSend | function | No | Hook to modify event before sending |
captureException(error)
Manually captures an error object and sends it to Sentry.
| Parameter | Type | Description |
|-----------|------|-------------|
| error | Error | The error object to capture |
🤝 Contributing
This project is for educational purposes. Feel free to fork it and experiment!
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
This project is licensed under the MIT License.
