Skip to content

logError swallows error details (such as cause) #6462

Closed
ErnestHysa/express
#1

Description

@cdauth

Express configures finalhandler’s onError function in application.js:156 to call logError, which basically calls console.error(err.stack || err.toString()).

Some years ago logging err.stack was the only way to print out the error message and stack trace at once, as logging just err would miss some things (I don’t remember what). This has changed and nowadays it seems to be the other way round.

For example, there is now the Error.cause property allowing to specify a nested error. console.log(new Error("outer", { cause: new Error("inner") })) prints the nested error, while console.log(new Error("outer", { cause: new Error("inner") }).stack) misses it.

Async stack traces might be another example, although I couldn’t find a good way to test this.

In my concrete case, I am using Sequelize and an error occurred. Express basically logged the error as Error: with a stack trace that was not very helpful. After some investigation, I found out that logging the error object itself rather than its stack property reveals tons of information about the error that were otherwise missing. It seems that Sequelize errors contain nested errors in the parent and original properties, and those are logged as well when using console.error(error) but not with console.error(error.stack).

As a workaround, I am using this as the final middleware in my app:

	app.use((err, req, res, next) => {
		finalhandler(req, res, {
			env: app.get("env"),
			onerror: (err) => { console.error(err); }
		})(err);
	});

Activity

  1. added theissue type on Apr 17, 2025
  2. Nitin-Mohapatra commented on Apr 17, 2025

    @Nitin-Mohapatra
    Contributor

    I’m working on this. Will open a PR soon 😊

  3. added a commit that references this issue on Oct 11, 2025
    6fba315
  4. added a commit that references this issue on Feb 1, 2026
    36d92b2
  5. LovekeshAnand commented on Feb 11, 2026

    @LovekeshAnand

    Hi, I’d like to work on this issue.
    My understanding is that logging err.stack hides modern error metadata like cause.
    Would replacing it with console.error(err) be acceptable, or would you prefer a more backward-compatible approach?

  6. krzysdz commented on Feb 11, 2026

    @krzysdz
    Contributor

    @LovekeshAnand there have been already some PRs, including #6464 which does exactly that and is waiting for reviews.

  7. LovekeshAnand commented on Feb 11, 2026

    @LovekeshAnand

    hey, thanks for replying.
    i will look for another issue to contribute as a first time contributer

  8. jonchurch commented on Feb 12, 2026

    @jonchurch
    Member

    aye @cdauth you are correct in that this is vestigial due to old node limitations of console.error from back in the day. console.error(err) did not log the stack back in the day, hence the explicit workaround

    nvm exec 0.10 node -e "console.error(new Error('test'))"
      [Error: test] # only the error message!
    
    nvm exec 18 node -e "console.error(new Error('test'))"
      Error: test # message plus stack in modern node
          at [eval]:1:15
          ...
    

    That exact logerror code is straight from the v3 => v4 migration of express and just hasnt been looked at since (it was inlined in the handle in v3, but v4 moved it to logerror). Ty for the report

  9. added a commit that references this issue on Feb 21, 2026
    6556e3f
  10. added a commit that references this issue on Mar 9, 2026
    461adf6
  11. added a commit that references this issue on Mar 15, 2026
    f4d1dbc
  12. added a commit that references this issue on Mar 20, 2026
    29f7fcb
  13. added a commit that references this issue on Apr 16, 2026
    1cd000e
  14. myselfsiddharth commented on May 29, 2026

    @myselfsiddharth

    Opened #7290 with the \console.error(err)\ fix (aligned with approved #6464) plus a regression test on current master. Happy to consolidate with #6464 if maintainers prefer one PR — main goal is getting the fix and test merged.

  15. added a commit that references this issue on Jun 8, 2026
    7a0009e
  16. added a commit that references this issue on Jul 6, 2026
    310e0a6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions