Skip to content

Add isError typeguard#86

Merged
kibertoad merged 2 commits intomainfrom
feat/is-error
Dec 4, 2023
Merged

Add isError typeguard#86
kibertoad merged 2 commits intomainfrom
feat/is-error

Conversation

@kibertoad
Copy link
Copy Markdown
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

Comment thread src/utils/typeUtils.ts

export function isError(maybeError: unknown): maybeError is Error {
return (
maybeError instanceof Error || Object.prototype.toString.call(maybeError) === '[object Error]'
Copy link
Copy Markdown
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
@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