fix: log error cause chain in logerror function#6837
fix: log error cause chain in logerror function#6837OP-Prajwal wants to merge 1 commit intoexpressjs:masterfrom
Conversation
- Add support for logging Error.cause property - Recursively log entire cause chain with 'Caused by:' prefix - Maintains backward compatibility with errors without cause - Fixes expressjs#6462
|
Thanks for your contribution! Noticed that #6464 also solves the same issue, would it make sense to collaborate there instead of having two fixes? That way, the maintainers would review only a single fix. |
According to me Printing an entire error object is somewhat unnecessary because it will be too big and confusing for people to debug the issue. I would prefer to print only the cause as I did in my pr. |
How exactly printing all |
jonchurch
left a comment
There was a problem hiding this comment.
The biggest problem here is the multiple writes to stderr
This means there's no guarantee you get a coherent message to the console.
If you get multiple requests at the same time which error, they may be interleaved within each other.
aka:
Error: Query failed ← request A
at handler.js:10
Error: Connection refused ← request B interleaved
at db.js:5
Caused by: ECONNREFUSED ← request B's cause
at net.js:100
Caused by: DB timeout ← request A's cause, now detached
at pool.js:42
|
Ty for the contribution. This recursing approach is not preferred, as it reinvents |
Fixes #6462
Problem
The
logerrorfunction in Express was only loggingerr.stack, which doesn't display the errorcauseproperty introduced in Node.js 16.9.0. This meant that error chains were not visible in logs, making debugging more difficult.Solution
Modified the
logerror()function to explicitly log the complete error cause chain. When an error has acause, it now logs each error in the chain with a "Caused by:" prefix for clarity.Changes
lib/application.js-logerror()function now iterates through the error cause chaincausework exactly as beforeBefore
After
Testing