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 isErrorLike() method #68

Merged
merged 12 commits into from Apr 4, 2022

Conversation

fregante
Copy link
Contributor

@fregante fregante commented Apr 2, 2022

Closes #47

Since deserializeError also accepts Error instances, I decided to do the same for this detector.

Names:

  • isErrorLike: it matches the "array-like" nomenclature, which also is true or real arrays
  • isSerializedError: can't use it because the function also matches regular Error instances
  • isErrorObject: it matches the ErrorObject type, but it reads like "is Error object", as in "is Error instance.

Unrelated suggestions:

  • rename the ErrorObject type to ErrorLike
  • make name, message and stack properties of this type mandatory, because they always exist in new Error() and its serialized counterpart

@fregante fregante changed the title Add isSerializedError Add isErrorLike Apr 2, 2022
@fregante fregante mentioned this pull request Apr 2, 2022
index.d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

You need to update the readme too.

@sindresorhus
Copy link
Owner

rename the ErrorObject type to ErrorLike

👍

make name, message and stack properties of this type mandatory, because they always exist in new Error() and its serialized counterpart

Sure, but we still need to keep the loose variant around for deserializeError.

@fregante
Copy link
Contributor Author

fregante commented Apr 2, 2022

Sure, but we still need to keep the loose variant around for deserializeError.

For now I added the ErrorLike type so that the checks in isErrorLike match the properties it verifies. You can later decide whether to drop the ErrorObject on the next major version.

deserializeError accepts ErrorObject | unknown, which just means unknown, so it can be dropped from its type.

readme.md Outdated Show resolved Hide resolved
index.d.ts Outdated
stack: string;
message: string;
code?: string;
} & JsonObject;
Copy link
Owner

Choose a reason for hiding this comment

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

& JsonObject is not correct here because the JS code is not actually ensuring that.

Copy link
Owner

Choose a reason for hiding this comment

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

Should we also add cause as optional? Same with ErrorObject.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

& JsonObject is not correct here because the JS code is not actually ensuring that.

I suppose the solution is to use a generic Record then

Should we also add cause as optional? Same with ErrorObject.

Done

index.d.ts Outdated
Predicate to determine whether a value looks like an error, even if it's not an instance of `Error`. It must have at least the `name`, `message`, and `stack` properties.

@example
```js
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
```js

Copy link
Contributor Author

@fregante fregante Apr 4, 2022

Choose a reason for hiding this comment

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

The rest of the examples use ``` too. I dropped js though.

If you want to drop ``` from all of them it can be done in a separate commit I suppose

Copy link
Owner

Choose a reason for hiding this comment

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

If you want to drop ``` from all of them it can be done in a separate commit I suppose

👍

});
//=> false
```

Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change

@sindresorhus
Copy link
Owner

Sorry for the back and forth. I would make the changes myself, but I don't have access since you did the PR from an org account.

@sindresorhus sindresorhus changed the title Add isErrorLike Add isErrorLike() method Apr 3, 2022
@fregante
Copy link
Contributor Author

fregante commented Apr 4, 2022

I don't have access since you did the PR from an org account.

Ahh that's annoying, sorry about that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add isErrorObject type guard / function
2 participants