Node.js › Error Handling
How would you implement automatic error reporting to a service like Sentry in a Node.js application, and what data should be scrubbed before sending?
AdvancedAnswer
Sentry (and similar tools) provide an SDK that automatically captures uncaught exceptions, unhandled rejections, and manually reported errors, sending them along with stack traces, request context, and environment details to a centralized dashboard for triage and alerting -- before sending, sensitive data (passwords, tokens, credit card numbers, personally identifiable information) should be explicitly scrubbed via the SDK's configurable 'beforeSend' hook, since error payloads can easily and accidentally include sensitive request bodies or headers.
Sentry.init({
dsn: process.env.SENTRY_DSN,
beforeSend(event) {
if (event.request?.data?.password) delete event.request.data.password;
if (event.request?.headers?.authorization) delete event.request.headers.authorization;
return event;
},
});
app.use(Sentry.Handlers.errorHandler());Real-world example A team configures Sentry's beforeSend hook to strip Authorization headers and password fields from every captured error event, after discovering an early integration had been inadvertently sending full request bodies (including plaintext passwords from failed login attempts) to their error-tracking dashboard.