-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Changes from 10 commits
d821a58
8554da3
c08078e
df9a020
1df72d3
b7c00cb
a6ad073
0a35fa9
110d01d
cdea573
0f11938
f7ccf08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,13 @@ export type ErrorObject = { | |||
code?: string; | ||||
} & JsonObject; | ||||
|
||||
export type ErrorLike = { | ||||
name: string; | ||||
stack: string; | ||||
message: string; | ||||
code?: string; | ||||
} & JsonObject; | ||||
|
||||
export interface Options { | ||||
/** | ||||
The maximum depth of properties to preserve when serializing/deserializing. | ||||
|
@@ -120,3 +127,37 @@ console.log(error); | |||
``` | ||||
*/ | ||||
export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error; | ||||
|
||||
/** | ||||
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 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of the examples use ``` too. I dropped If you want to drop ``` from all of them it can be done in a separate commit I suppose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍 |
||||
import {isErrorLike} from 'serialize-error'; | ||||
|
||||
const error = new Error('🦄'); | ||||
error.one = {two: {three: {}}}; | ||||
|
||||
isErrorLike({ | ||||
name: 'DOMException', | ||||
message: 'It happened', | ||||
stack: 'at foo (index.js:2:9)', | ||||
}); | ||||
//=> true | ||||
|
||||
isErrorLike(new Error('🦄')); | ||||
//=> true | ||||
|
||||
isErrorLike(serializeError(new Error('🦄')); | ||||
//=> true | ||||
|
||||
isErrorLike({ | ||||
name: 'Bluberricious pancakes', | ||||
stack: 12, | ||||
ingredients: 'Blueberry', | ||||
}); | ||||
//=> false | ||||
``` | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
*/ | ||||
export function isErrorLike(value: unknown): value is ErrorLike; |
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.
& JsonObject
is not correct here because the JS code is not actually ensuring that.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.
Should we also add
cause
as optional? Same withErrorObject
.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.
I suppose the solution is to use a generic Record then
Done