Skip to content
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

process: add reportError #41912

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

benjamingr
Copy link
Member

@benjamingr benjamingr commented Feb 9, 2022

Fixes: #38947
Spec: https://html.spec.whatwg.org/#runtime-script-errors

I'm opening as draft for discussion - mostly I want feedback on "are we sure crash on next tick" is the right behavior :] (I think it is but want opinions).

This is missing the wpts but that probably shouldn't block landing.

cc @mcollina @addaleax @Linkgoron

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. tools Issues and PRs related to the tools directory. labels Feb 9, 2022
@aduh95 aduh95 added the semver-major PRs that contain breaking changes and should be released in the next major version. label Feb 9, 2022
@@ -268,6 +268,7 @@ let knownGlobals = [
setInterval,
setTimeout,
queueMicrotask,
reportError,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go under this comment, with the same caveat

node/test/common/index.js

Lines 296 to 301 in 6ec2253

// TODO(@ethan-arrowood): Similar to previous checks, this can be temporary
// until v16.x is EOL. Once all supported versions have structuredClone we
// can add this to the list above instead.
if (global.structuredClone) {
knownGlobals.push(global.structuredClone);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 I don't understand why - this PR will land on the next semver-major - other versions won't have this in common/index.js so they won't expect the global and will (rightfully) error if it's present right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC that’s there so make test-doc can pass without needing to compile the latest master.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be +1 on this landing in Node.js as a global. Node.js core it self uses similar utility APIs already, here's a few occurrences I could find:

function emitUncaughtException(err) {
process.nextTick(() => { throw err; });
}

}), (error) => process.nextTick(() => { throw error; }));

if (process.throwDeprecation) {
// Delay throwing the error to guarantee that all former warnings were
// properly logged.
return process.nextTick(() => {
throw warning;
});
}

// In a next tick because this is happening within
// a promise context, and if there are any errors
// thrown we don't want those to cause an unhandled
// rejection. Let's just escape the promise and
// handle it separately.
process.nextTick(() => { throw error; });

doc/api/globals.md Show resolved Hide resolved
* `error` {Error|any} an error to report.

The `reportError(error)` method triggers an uncaught exception with the error
`error`. This will crash the Node.js process by default.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to link to uncaught exception handling somewhere here so people can take action on the "will crash" bit of the doc.

doc/api/globals.md Outdated Show resolved Hide resolved
@benjamingr
Copy link
Member Author

Addressed comments @bmeck @Mesteery @aduh95 (I will refactor the internal code to use this in a later PR).

WDYT? Is the semantics reasonable to the point I should move this out of draft?

@benjamingr benjamingr marked this pull request as ready for review February 16, 2022 18:15
// The user called reportError with a non-error so we want to give them
// a way to get a stack trace. We do this by emitting a warning the user
// so the user can run node with `--trace-warnings` to get a stack tace.
process.emitWarning('reportError was called with a non-error.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.... I'm not convinced the warning is necessary or all that useful. Not blocking however.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly thing we shouldn’t encourage users to error with things without a stack trace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the browser warn on this?

Not every error condition needs a stack trace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every error condition needs a stack trace.

I am pretty sure when debugging a warning I would really want a stack trace though note this is opt in with --trace-warnings right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The individual user’s desire there may be for a stack trace, but that’s not universal, or the language wouldn't let you throw something without one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb as far as I'm aware the language is not yet aware of stack traces anyway? So the language is completely oblivious to the debugging experience (or the user) isn't it?

The only reason stuff like Error even exists is that language itself can throw stuff that subclasses it (like RangeError or TypeError) and as a possible extensibility point. The language is specific about this:

Instances of Error objects are thrown as exceptions when runtime errors occur. The Error objects may also serve
as base objects for user-defined exception classes.

(exists since es3)

In any case aren't "what errors users get" and "what the language lets you do" here completely orthogonal since it's something the runtime and not the language typically does?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - users (should) expect to be able to throw and catch any kind of value, because every kind of value can be an error. Thus, a warning here on an otherwise-valid error will be surprising and confusing to developers who expect that explicitly passing a non-Error object to reportError will be calmly accepted.

@aduh95
Copy link
Contributor

aduh95 commented Apr 3, 2022

This needs a rebase (Node.js 18.x semver cutoff is next week).

@benjamingr
Copy link
Member Author

I am honestly still kind of not sure we should actually do this - waiting for more people to ask for this or to weigh in this is a good idea

@devinrhode2
Copy link

I am honestly still kind of not sure we should actually do this - waiting for more people to ask for this or to weigh in this is a good idea

I would fricken love this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. semver-major PRs that contain breaking changes and should be released in the next major version. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reportException(ex) / reportError(ex)
8 participants