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?

Advanced

Answer

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.

Common follow-ups: What other categories of data besides credentials commonly need scrubbing from error reports for compliance reasons (like GDPR)?;How does Sentry's error grouping/fingerprinting work to avoid flooding a dashboard with thousands of instances of the same underlying issue?