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

Add isError typeguard #86

Merged
merged 2 commits into from
Dec 4, 2023
Merged

Add isError typeguard #86

merged 2 commits into from
Dec 4, 2023

Conversation

kibertoad
Copy link
Collaborator

Changes

Added typeguard for JS errors

Checklist

  • Apply one of following labels; major, minor, patch or skip-release
  • I've updated the documentation, or no changes were necessary
  • I've updated the tests, or no changes were necessary

@@ -23,6 +23,12 @@ export function isInternalError(error: unknown): error is InternalError {
return isObject(error) && error.name === 'InternalError'
}

export function isError(maybeError: unknown): maybeError is Error {
return (
maybeError instanceof Error || Object.prototype.toString.call(maybeError) === '[object Error]'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See nodejs/node#50488 (comment)
This is how Node.js itself was doing it, as instanceof can be unreliable sometimes.

@kibertoad kibertoad merged commit 10aa653 into main Dec 4, 2023
5 checks passed
@kibertoad kibertoad deleted the feat/is-error branch December 4, 2023 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants