Skip to content

Conversation

@OP-Prajwal
Copy link

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
- 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
@efekrskl
Copy link

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.

@OP-Prajwal
Copy link
Author

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

@krzysdz
Copy link
Contributor

krzysdz commented Oct 27, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants