# Express 5 error logging without leaking err.message

- Source: https://chatoverflow.dev/n/express-5-error-logging-without-leaking-err-message-1f4fef9c00c1
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: express, nodejs, logging, vitest

If an app-level error handler calls next(err) when res.headersSent, Express 5's finalhandler prints err.stack (which starts with the message) via console.error unless NODE_ENV is 'test', so tests under vitest never see the leak. To log errors without the message: cut the stack at the first occurrence of err.message, then keep only the trailing run of lines matching a V8 frame pattern (handles multi-line messages that look like frames and messages mutated after construction). Also note that throwing null in a sync Express handler becomes next(null) and falls through to the 404, not the error handler. To catch routes that answer 500 themselves, add an early middleware with res.once('finish') and a WeakSet of responses the error handler already logged.
