question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature]: Print error.cause

See original GitHub issue

🚀 Feature Proposal

The proposal for error.cause is currently stage 3. This proposal is to support jest printing chains of cause properties if they are present on errors.

Motivation

Nested errors can be critical to debugging. JSON.parse might throw a syntax error when reading a file. But if the filename isn’t hardcoded in the stack from that called JSON.parse, a developer debugging would have no idea which particular file could not be parsed. The solution is to support chains of causally related errors: as errors bubble up through layers of code they can be caught and rethrown as a cause, allowing additional information to be captured.

Example

Imagine the application contains this code

const readJson = (path) => {
  try {
    const json = JSON.parse(fs.readFileSync(path));
  } catch (e) {
    throw new Error(`Failed to parse JSON from ${path}`, {cause: e});
  }
}

If the application throws during testing complete information is available about the problem. The code throws an error with a cause property defined, per the spec. The cause contains critical information about what the error is, but if jest does not opt in to printing it that information is only present in interactive environments for those who know to look for it. Currently jest would print

Error: Failed to parse JSON from 'path/to/file'.
  at readJson:5

If this feature is implemented jest would print:

Error: Failed to parse JSON from 'path/to/file'.
  at readJson:5
Caused by: SyntaxError: Unexpected token u in JSON at position 0
  at JSON.parse (<anonymous>)
  at readJson:3

Pitch

Jest has the ability to bring the improved debuggability that this proposal creates to a large audience. It is relevant and stable enough for inclusion.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:12
  • Comments:34 (28 by maintainers)

github_iconTop GitHub Comments

2reactions
mattfyshcommented, Dec 3, 2022

Looks like this issue has stalled - but it would be super valuable to ensure error chains are being correctly reported

1reaction
conartist6commented, Dec 3, 2021

Draft PR is up! Let the feedback roll in! I’ll be doing my own review there as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error.prototype.cause - JavaScript - MDN Web Docs
The cause data property of an Error instance indicates the specific original cause of the error. It is used when catching and re-throwing...
Read more >
Function address caused a Protection Fault - Printing error
When the print job fails, you receive the following instance of the error message;. Function address 12345 caused a protection fault.
Read more >
Windows 10: Still seeing a printing error? Here's how to fix it
Windows 10: Still seeing a printing error? Here's how to fix it · 1. Press the Windows key+r or select Start and type...
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
The error is caused by (or at least detected at) the token preceding the arrow: in the example, the error is detected at...
Read more >
You cannot connect to an Internet printer if the Internet Printing ...
Cause. This problem may occur if the Internet Printing Client feature is disabled on the computer. This problem may also occur if the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found