-
Notifications
You must be signed in to change notification settings - Fork 12.9k
feat: add Error.isError() to ESNext lib #60788
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
Conversation
For reference, this is soon to land in Node v24. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the PR to minimize it. I think it's good now, though my pushing to it means I don't have permission to actually approve it.
/** | ||
* Indicates whether the argument provided is a built-in Error instance or not. | ||
*/ | ||
isError(error: unknown): error is Error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, in the spec, this is not a predicate on satisfying the shape of the Error prototype.
const pseudo = Reflect.construct(Error, [], Object)
Error.prototype.isPrototypeOf(pseudo) // false
'message' in pseudo // false
'name' in pseudo // false
Error.isError(pseudo) // true
By rights, this should really just return a boolean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It being a predicate was the main reason everyone wanted it....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(okay the realm problem is the real issue but still)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usefulness very much outweighs the downsides, we think.
Fixes #60787