jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: Support `Error.prototype.cause`

Open silverwind opened this issue 3 years ago • 7 comments

🚀 Feature Proposal

Some new APIs like fetch make use of Error.prototype.cause which jest does not display at all for thrown errors, which makes debugging these errors harder than necessary.

Motivation

It will make errors with cause easier to debug.

Example

test("cause", () => {
  try {
    throw new Error("cause");
  } catch (err) {
    throw new Error("wrapped", {cause: err});
  }
});

Will output only the wrapped error message, which is often unhelpful:

 FAIL  ./test.js
  ● cause

    wrapped

It'd be better if the cause message would also be shown:

 FAIL  ./test.js
  ● cause

    wrapped
    cause

Pitch

Because Error.prototype.cause is a standard Javascript feature.

silverwind avatar Sep 13 '22 22:09 silverwind

We should definitely add this, yeah! I added support to our logger at work just a few weeks ago 😀

PR welcome, most changes should be in https://github.com/facebook/jest/blob/d1626e9395263b0c7c207777c0ca570b2147aaa8/packages/jest-message-util/src/index.ts

SimenB avatar Sep 14 '22 07:09 SimenB

As errors in error.cause can contain more errors with causes attached, I guess a recursion limit will be necessary to prevent circular references and such. I guess a limit of 10 should work for most cases.

silverwind avatar Sep 14 '22 08:09 silverwind

And like node, duplicate traces should be removed.

$ node -p "new Error('asdasd', {cause: new Error('cause')})"
Error: asdasd
    at [eval]:1:1
    at Script.runInThisContext (node:vm:129:12)
    ... 4 lines matching cause stack trace ...
    at node:internal/main/eval_string:27:3 {
  [cause]: Error: cause
      at [eval]:1:29
      at Script.runInThisContext (node:vm:129:12)
      at Object.runInThisContext (node:vm:313:38)
      at node:internal/process/execution:76:19
      at [eval]-wrapper:6:22
      at evalScript (node:internal/process/execution:75:60)
      at node:internal/main/eval_string:27:3
}

(note 4 lines matching cause stack trace )


Not necessary for an initial implementation, of course

SimenB avatar Sep 14 '22 09:09 SimenB

util.inspect() on an Error will produce a string with the cause in the expected format:

$ node -e "console.log(util.inspect(new Error('asdasd', {cause: new Error('cause')})))"
Error: asdasd
    at [eval]:1:26
    at Script.runInThisContext (node:vm:129:12)
    ... 4 lines matching cause stack trace ...
    at node:internal/main/eval_string:27:3 {
  [cause]: Error: cause
      at [eval]:1:54
      at Script.runInThisContext (node:vm:129:12)
      at Object.runInThisContext (node:vm:305:38)
      at node:internal/process/execution:76:19
      at [eval]-wrapper:6:22
      at evalScript (node:internal/process/execution:75:60)
      at node:internal/main/eval_string:27:3
}

davecardwell avatar Sep 18 '22 18:09 davecardwell

Related change there is https://github.com/nodejs/node/pull/41002.

silverwind avatar Sep 19 '22 08:09 silverwind

I can take this up if no one has started work @SimenB

kkyusufk avatar Sep 20 '22 12:09 kkyusufk

Go for it! 🙂

SimenB avatar Sep 20 '22 14:09 SimenB

Hey apologies, I am starting work on this today. I got caught in some other work.

kkyusufk avatar Oct 13 '22 18:10 kkyusufk

Duplicate of #12053.

PR is of course still very much welcome!

SimenB avatar Oct 26 '22 12:10 SimenB

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Nov 26 '22 00:11 github-actions[bot]