Skip to content

fix: preserve full error details in logerror (Error.cause, nested errors)#7313

Closed
armorbreak001 wants to merge 1 commit into
expressjs:masterfrom
armorbreak001:fix/logerror-cause-preservation
Closed

fix: preserve full error details in logerror (Error.cause, nested errors)#7313
armorbreak001 wants to merge 1 commit into
expressjs:masterfrom
armorbreak001:fix/logerror-cause-preservation

Conversation

@armorbreak001

Copy link
Copy Markdown

Problem

logerror() currently logs err.stack || err.toString(). This strips important error information:

  • Error.cause (ES2022) — nested/wrapped errors are invisible in logs
  • Custom properties — libraries like Sequelize attach parent, original, and other diagnostic properties that get lost
  • Async context — modern Node.js includes richer info when logging the full Error object

Real-world impact

When using Sequelize (or any library that wraps errors), Express currently logs just Error: with an unhelpful stack trace. The actual database error details (constraint violation, connection failure reason, etc.) are hidden inside properties that err.stack does not include.

Solution

One-line change: console.error(err.stack || err.toString())console.error(err)

This preserves the complete error object output while still showing stack traces. Node's console.error handles Error objects comprehensively — it prints the message, cause chain, custom properties, and stack.

Before

Error: 
    at Object.<anonymous> (/app/index.js:25:3)
    ...

(No indication of what actually went wrong)

After

Error: inner error message
    at Object.<anonymous> (/app/index.js:23:3)
    ...
[cause]: Error: outer wrapper

Testing

  • All 1249 existing tests pass
  • Behavior verified: console.error(new Error("outer", { cause: new Error("inner") })) now shows both errors
  • Non-Error values still work (coerced to string by console.error)

Backwards Compatibility

Fully backwards compatible. The output is strictly more informative — no information is removed, only added.

Fixes #6462

console.error(err.stack || err.toString()) strips important error
properties like Error.cause, custom properties added by libraries
(e.g. Sequelize's parent/original), and async stack trace context.

Changing to console.error(err) preserves the complete error object
output while still showing stack traces. This matches modern Node.js
behavior where console.error handles Error objects comprehensively.

Fixes expressjs#6462
@krzysdz

krzysdz commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Duplicate of #6464

@krzysdz krzysdz marked this as a duplicate of #6464 Jun 14, 2026
@krzysdz krzysdz closed this Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logError swallows error details (such as cause)

2 participants