How to use error tracking tools like Sentry to catch production bugs
error tracking, Sentry setup, breadcrumbs, error context, alerting, grouping errors
Why You Need Error Tracking
Production errors you do not know about are the most dangerous kind. Users encounter them, leave, and never report them. Error tracking tools like Sentry automatically capture every unhandled exception, attach full stack traces, device context, and user session data, and notify you immediately.
Setting Up Sentry
// Install
npm install @sentry/node
// Initialize at app entry point
const Sentry = require('@sentry/node');
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV,
release: process.env.GIT_COMMIT_SHA,
});
// Capture handled errors with context
try {
await processPayment(order);
} catch (err) {
Sentry.withScope(scope => {
scope.setTag('orderId', order.id);
scope.setUser({ id: order.userId });
Sentry.captureException(err);
});
throw err;
}
Breadcrumbs and Grouping
Sentry records breadcrumbs -- a timeline of events leading up to an error: navigation events, console logs, HTTP requests, user clicks. These breadcrumbs are the equivalent of a debugger stack trace for production. Sentry also groups similar errors together, so you see this error occurred 347 times rather than 347 separate notifications. Prioritize errors by frequency and affected user count, not by recency alone.
