JavaScript error tracking and monitoring library for Node.js and browser environments with telemetry, automatic error grouping, and real-time notifications
—
Node.js specific features including Express middleware, AWS Lambda integration, callback wrapping, and local variable capture for enhanced server-side error tracking.
Creates Express.js middleware for handling errors in Express applications.
/**
* Create Express.js error handling middleware
* @returns Express error handler middleware function
*/
function errorHandler(): ExpressErrorHandler;
type ExpressErrorHandler = (
err: any,
request: any,
response: any,
next: ExpressNextFunction
) => any;
interface ExpressNextFunction {
(err?: any): void;
}Usage Example:
const express = require('express');
const Rollbar = require('rollbar');
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
environment: 'production'
});
const app = express();
// Your routes
app.get('/', (req, res) => {
throw new Error('This will be caught by Rollbar');
});
// Use Rollbar error handler (should be last middleware)
app.use(rollbar.errorHandler());
app.listen(3000);Wraps AWS Lambda handlers with error reporting and timeout handling.
/**
* Wrap AWS Lambda handler with error reporting
* @param handler - Lambda handler function to wrap
* @returns Wrapped Lambda handler with error reporting
*/
function lambdaHandler<T = object>(
handler: LambdaHandler<T>
): LambdaHandler<T>;
type LambdaHandler<TEvent = any, TResult = any, TContext = any> = (
event: TEvent,
context: TContext,
callback: Callback<TResult>
) => void | Promise<TResult>;Usage Examples:
const Rollbar = require('rollbar');
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
environment: 'production',
captureLambdaTimeouts: true
});
// Wrap callback-style handler
exports.handler = rollbar.lambdaHandler((event, context, callback) => {
try {
const result = processEvent(event);
callback(null, result);
} catch (error) {
callback(error); // Will be reported to Rollbar
}
});
// Wrap async handler
exports.asyncHandler = rollbar.lambdaHandler(async (event, context) => {
// Any uncaught errors will be reported
const data = await fetchData(event.id);
return processData(data);
});Wraps callback functions with error handling.
/**
* Wrap callback function with error handling
* @param callback - Callback function to wrap
* @returns Wrapped callback that reports errors to Rollbar
*/
function wrapCallback(callback: Function): Function;Usage Example:
const fs = require('fs');
// Wrap callback to catch and report errors
fs.readFile('file.txt', rollbar.wrapCallback((err, data) => {
if (err) {
// Error will be automatically reported to Rollbar
return;
}
console.log(data.toString());
}));Capture local variables from stack frames for enhanced debugging (Node.js 10+).
interface LocalsSettings {
module: LocalsType;
enabled?: boolean;
uncaughtOnly?: boolean;
depth?: number;
maxProperties?: number;
maxArray?: number;
}
type LocalsOptions = LocalsType | LocalsSettings;Configuration Example:
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
locals: {
enabled: true,
uncaughtOnly: false, // Capture for all errors, not just uncaught
depth: 3, // Maximum call stack depth to capture
maxProperties: 5, // Maximum object properties per frame
maxArray: 5 // Maximum array elements per frame
}
});Automatically collect and include HTTP request data with error reports.
/**
* Custom function to add request data to error reports
*/
interface AddRequestDataFunction {
(data: Dictionary, req: Dictionary): void;
}Configuration Example:
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
addRequestData: (data, req) => {
// Add custom request data
data.custom_request_id = req.headers['x-request-id'];
data.user_agent = req.headers['user-agent'];
data.ip_address = req.ip;
}
});Automatically capture and report Lambda function timeouts.
captureLambdaTimeouts?: boolean;Configuration Example:
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
captureLambdaTimeouts: true // Enable Lambda timeout capture
});Enable source map processing for better stack traces in Node.js.
nodeSourceMaps?: boolean;Configuration Example:
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
nodeSourceMaps: true // Enable source map processing
});host?: string;The hostname of the server. Defaults to os.hostname().
exitOnUncaughtException?: boolean;Whether to exit the process when an uncaught exception occurs. Default is true.
Server-side includes additional headers that are scrubbed by default:
[
'authorization', 'www-authorization', 'http_authorization',
'omniauth.auth', 'cookie', 'oauth-access-token', 'x-access-token',
'x_csrf_token', 'http_x_csrf_token', 'x-csrf-token'
]interface ServerPayload {
server?: {
branch?: string;
host?: string;
root?: string;
[property: string]: any;
};
}const express = require('express');
const Rollbar = require('rollbar');
// Initialize Rollbar
const rollbar = new Rollbar({
accessToken: 'YOUR_POST_SERVER_ITEM_ACCESS_TOKEN',
environment: process.env.NODE_ENV || 'development',
// Error handling
captureUncaught: true,
captureUnhandledRejections: true,
exitOnUncaughtException: true,
// Reporting levels
reportLevel: 'warning',
// Local variable capture
locals: {
enabled: true,
uncaughtOnly: false,
depth: 3
},
// Request data
addRequestData: (data, req) => {
data.headers = req.headers;
data.method = req.method;
data.url = req.url;
data.query = req.query;
data.body = req.body;
},
// Payload customization
payload: {
server: {
host: require('os').hostname(),
branch: process.env.GIT_BRANCH || 'main'
},
environment: process.env.NODE_ENV || 'development'
}
});
const app = express();
// Middleware
app.use(express.json());
// Routes
app.get('/', (req, res) => {
rollbar.info('Homepage accessed');
res.send('Hello World!');
});
app.get('/error', (req, res) => {
throw new Error('Test error for Rollbar');
});
// Rollbar error handler (must be last)
app.use(rollbar.errorHandler());
app.listen(3000, () => {
rollbar.info('Server started on port 3000');
});
// Graceful shutdown
process.on('SIGTERM', () => {
rollbar.info('Server shutting down');
rollbar.wait(() => {
process.exit(0);
});
});const Rollbar = require('rollbar');
const rollbar = new Rollbar({
accessToken: 'YOUR_ACCESS_TOKEN',
environment: process.env.ENVIRONMENT || 'development',
captureLambdaTimeouts: true,
captureUncaught: true,
captureUnhandledRejections: true
});
// API Gateway handler
exports.handler = rollbar.lambdaHandler(async (event, context) => {
rollbar.info('Lambda function invoked', {
requestId: context.awsRequestId,
functionName: context.functionName
});
try {
const result = await processRequest(event);
return {
statusCode: 200,
body: JSON.stringify(result)
};
} catch (error) {
rollbar.error('Lambda function error', error, {
event: event,
context: context
});
return {
statusCode: 500,
body: JSON.stringify({ error: 'Internal Server Error' })
};
}
});
async function processRequest(event) {
// Your business logic here
if (Math.random() < 0.1) {
throw new Error('Random error for testing');
}
return { message: 'Success', timestamp: new Date().toISOString() };
}Install with Tessl CLI
npx tessl i tessl/npm-rollbar