-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement ES stage-3 proposal: error-cause #38725
Comments
Do you mean add |
I mean implementing the formal ES proposal spec: https://github.com/tc39/proposal-error-cause function runTheJob() {
try {
someRiskyComputation();
} catch (e) {
throw new Error("The job failed, see why: ", { cause: e });
}
}
try {
runTheJob();
} catch (e) {
console.error(e);
} On failure, it should print something like:
Maybe some internal NodeJS code can benefit from this in some places, but I can't tell where exactly, and the spec implementation is the first step before thinking about using this :) To give an example, here's what a Java stack trace looks like:
(source: https://www.twilio.com/blog/how-to-read-and-understand-a-java-stacktrace). The goal is to replace annoying code such as: try {
someRiskyJob(jobData)
} catch (e) {
console.error("I'm logging some message because JS errors do not have a cause and I need to provide more context, here's some useful data:" + JSON.stringify(jobData));
throw e;
} |
But the spec changes internals of the Error constructor, which is implemented by the V8 engine, not Node.js directly. |
/cc @legendecas. As the author of the proposal and a Node.js collaborator, do you think there's something that we can do to have something in Node.js before V8 implements it? |
I just realized that V8 already implemented it behind a flag a month ago: https://chromium-review.googlesource.com/c/v8/v8/+/2784681 As soon as the feature is stabilized, we can look into backporting it. |
Note that the proposal at https://github.com/tc39/proposal-error-cause is just about adding a
|
Edit: looks like error logging is not std, cf the Firefox issue:
I have no idea how engines decide to log console errors (and if there is any std error logging), but this looks not related to the JS language, so not too surprised it's not part of the TC39 spec. Any idea? The proposal shows: try {
await doJob();
} catch (e) {
console.log(e);
console.log('Caused by', e.cause);
} However I really hope in the long run we won't need to have to log the cause, particularly when this is potentially a recursive structurer console.log('Caused by', e.cause.cause.cause.cause); Having this supported in ES looks like a good first step in that direction |
@targos yeah we need updates for both error causes and aggregate errors in |
The spec of error cause is meant for language implementations but for runtime nor devtools. So the things we can do in Node.js are:
I believe we can start with item 1 as whether or not the error constructor can receive the new parameter, the cause property on error objects can always be inspected with or without v8's change. Item 2 requires v8's change on error constructor so that we can create new errors with cause property. But that's not necessary requirement: we can still assign the cause property after the constructor either ways. Devtools have their own protocols, like @hemanth mentioned. Currently the chrome devtools protocol doesn't reflect error object's cause by default. Anyway, that's up to v8 and chrome devtools, there is nothing Node.js can do as far as I could tell. |
Error causes was released in
Looks like the inner workings of node/lib/internal/console/constructor.js Line 327 in f182b9b
node/lib/internal/util/inspect.js Line 1881 in f182b9b
And node/lib/internal/util/inspect.js Lines 1163 to 1166 in f182b9b
Lastly it does some tweaking of the output, and that's probably where the causes could be iterated through and appended, probably by recursively calling I can try and make a stab at doing that if it sounds like a good idea to everyone?
I tried searching through the Node.js projects for cases where this could be useful to do, my regexps returned no cases, do you know of any @legendecas? |
While inspecting errors, always visualize the cause. That property is non-enumerable by default while being useful in general for debugging. Duplicated stack frames are hidden. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #41002 Fixes: #40859 Fixes: #38725 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
While inspecting errors, always visualize the cause. That property is non-enumerable by default while being useful in general for debugging. Duplicated stack frames are hidden. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #41002 Fixes: #40859 Fixes: #38725 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
While inspecting errors, always visualize the cause. That property is non-enumerable by default while being useful in general for debugging. Duplicated stack frames are hidden. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #41002 Fixes: #40859 Fixes: #38725 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
While inspecting errors, always visualize the cause. That property is non-enumerable by default while being useful in general for debugging. Duplicated stack frames are hidden. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #41002 Fixes: #40859 Fixes: #38725 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
While inspecting errors, always visualize the cause. That property is non-enumerable by default while being useful in general for debugging. Duplicated stack frames are hidden. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: nodejs#41002 Fixes: nodejs#40859 Fixes: nodejs#38725 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
While inspecting errors, always visualize the cause. That property is non-enumerable by default while being useful in general for debugging. Duplicated stack frames are hidden. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #41002 Fixes: #40859 Fixes: #38725 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Is your feature request related to a problem? Please describe.
Wrapping/enhancing errors with more context is complicated in JavaScript, while it's supported in multiple languages (like Java)
Describe the solution you'd like
Implement this stage-3 error cause proposal: https://github.com/tc39/proposal-error-cause
It's also worth making sure that logging an error with a causal chain logs the full causal chain recursively by default: this would make it easier to find the root cause of a bug without requiring the user to loop over the causal chain.
@mcollina suggested to open an issue
Describe alternatives you've considered
Using a custom non-std error library, but this would only work in my own code.
The text was updated successfully, but these errors were encountered: