express icon indicating copy to clipboard operation
express copied to clipboard

fix: log error cause chain in logerror function

Open OP-Prajwal opened this issue 3 months ago • 3 comments

Fixes #6462

Problem

The logerror function in Express was only logging err.stack, which doesn't display the error cause property 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 a cause, it now logs each error in the chain with a "Caused by:" prefix for clarity.

Changes

  • Updated lib/application.js - logerror() function now iterates through the error cause chain
  • Maintains backward compatibility - errors without cause work exactly as before
  • All existing tests pass (1236 passing)

Before

Error: Failed to fetch user data
    at ...

After

Error: Failed to fetch user data
    at ...

Caused by: Error: Failed to execute query
    at ...

Caused by: Error: Database connection timeout
    at ...

Testing

  • All existing tests pass (1236 passing)
  • Tested with error chains using Node.js Error.cause
  • Backward compatible with errors without cause
  • Requires Node.js 16.9.0+ for Error.cause support

OP-Prajwal avatar Oct 11 '25 09:10 OP-Prajwal

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.

efekrskl avatar Oct 25 '25 13:10 efekrskl

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.

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. Hope u get the actual point

OP-Prajwal avatar Oct 25 '25 14:10 OP-Prajwal

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. Hope u get the actual point

How exactly printing all .stack properties of error and its causes results in less text than logging full error (which does exactly the same thing, but can also automatically indent nested errors/objects)? Using just console.error(err) like in #6464 helps with AggregateErrors too.

krzysdz avatar Oct 27 '25 20:10 krzysdz