dwin-exception-tracker
v1.0.2
Published
Dwin Exception and Error Monitoring Agent SDK for Node.js applications.
Maintainers
Readme
dwin-exception-tracker
The Dwin Exception & Error Monitoring Agent SDK for Node.js.
Quickly capture unhandled runtime exceptions, HTTP request pipeline errors, and asynchronous promise rejections, sending detailed diagnostics and trace reports directly to your Dwin Dashboard.
Features
- Zero Dependencies: Lightweight, native implementation utilizing standard modern APIs.
- Express Middleware: Easy drop-in middleware for capturing all routing exceptions.
- Dynamic Service Resolution: Automatically resolves target service names from options, environment variables (
SERVICE_NAME), or defaults. - Manual Capture: Capture customized errors with rich context (e.g., custom endpoint name, sub-services).
Installation
From NPM Registry
npm install dwin-exception-trackerFrom Local Source (For Testing/Development)
You can link the agent locally in your parent application:
npm install /path/to/dwin-api-monitor/agentQuick Start
1. Initialize the SDK
Initialize the SDK at the top of your application's entrypoint (e.g., app.js or index.js).
const monitor = require('dwin-exception-tracker');
monitor.init({
apiKey: 'your-dwin-agent-api-key', // Copy this from your Dwin settings panel
serverUrl: 'http://localhost:3005', // Your Dwin monitoring dashboard backend URL
service: 'payment-api' // Dynamic name of this service
});2. Manual Exception Capturing
Wrap logic blocks inside try/catch and capture any thrown exceptions:
try {
// Application logic here...
throw new Error('Database connection timeout');
} catch (err) {
// Capture exception and send it to your Dwin console
monitor.captureException(err, {
endpoint: '/payments/process', // Optional request path context
service: 'payment-api' // Optional service override
});
throw err; // Re-throw if necessary
}3. Express.js Integration
To automatically capture all unhandled errors thrown inside Express route handlers, register the Dwin middleware at the very end of your middleware stack, after all route definitions:
const express = require('express');
const monitor = require('dwin-exception-tracker');
const app = express();
// ... define your route handlers ...
app.get('/api/checkout', (req, res) => {
throw new Error('Payment gateway refused connection');
});
// Register the Dwin Exception monitoring middleware
app.use(monitor.expressMiddleware());
// Optional: standard fallback error handler
app.use((err, req, res, next) => {
res.status(500).json({ error: 'Internal Server Error' });
});
app.listen(3000, () => console.log('App running on port 3000'));Configuration Options
Pass these options object parameters to monitor.init(options):
| Parameter | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| apiKey | string | null | Required. The API Key copied from settings. |
| serverUrl | string | 'http://localhost:3005' | Destination Dwin backend portal API url. |
| service | string | 'node-application' | Name of the running service (falls back to process.env.SERVICE_NAME). |
How to Publish to NPM
To publish this agent package to the npm registry for public or private team distribution, execute these steps:
- Set Up an NPM Account: Sign up at npmjs.com.
- Log In via Terminal:
npm login - Publishing a Private Namespace (Scoped):
If your package name matches
@yourorg/agent, publish it as a scoped package:- For public scoped packages (visible to everyone):
npm publish --access public - For private scoped packages (requires paid NPM plan):
npm publish
- For public scoped packages (visible to everyone):
- Publishing a Scoped/Unscoped Package:
Inside
/Users/apple/own/api-monitor/agent, run:npm publish
