Skip to content

Better support for Error.cause in util.inspect/console.log #40859

Description

@targos

Is your feature request related to a problem? Please describe.

Given this code:

const one = new Error('one');
const two = new Error('two', { cause: one });
const three = new Error('three', { cause: two });
console.log(three);
Error: three
    at Object.<anonymous> (/test.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    ...

The output only contains the error message and stack for error three.
It is already possible to see the value of cause, when showHidden is enabled, but in my opinion this is not a good general solution because it makes the output too verbose and requires to set a global config for console.log.

Describe the solution you'd like

I'm open to suggestions. One idea I have is to output something recursively indented like this:

Error: three
    at Object.<anonymous> (/test.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    ...
Caused by:
  Error: two
      at Object.<anonymous> (/test.js:2:13)
      at Module._compile (node:internal/modules/cjs/loader:1095:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
      ...
  Caused by:
    Error: one
        at Object.<anonymous> (/test.js:1:13)
        at Module._compile (node:internal/modules/cjs/loader:1095:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
        ...

/cc @nodejs/util

Activity

  1. added
    feature requestIssues requesting new Node.js features.
    errorsIssues and PRs related to JavaScript errors originating in Node.js core.
    on Nov 18, 2021
  2. added
    utilIssues and PRs related to the built-in util module.
    on Nov 18, 2021
  3. self-assigned this
    on Nov 18, 2021
  4. BridgeAR commented on Nov 24, 2021

    @BridgeAR
    Member

    I implemented something locally. It always prints the non-enumerable cause property and hides duplicated stack frames as long as they are included in the causing error stack as well. @targos does this match your needs?

    image

  5. targos commented on Nov 25, 2021

    @targos
    MemberAuthor

    Looks good to me!

  6. kanongil commented on Nov 28, 2021

    @kanongil
    Contributor

    There is already a bit of discussion on this here: #38725 (comment)

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

Metadata

Metadata

Assignees

Labels

errorsIssues and PRs related to JavaScript errors originating in Node.js core.feature requestIssues requesting new Node.js features.utilIssues and PRs related to the built-in util module.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions