-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.d.ts
171 lines (134 loc) · 4.01 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {Primitive, JsonObject} from 'type-fest';
export {default as errorConstructors} from './error-constructors.js';
export type ErrorObject = {
name?: string;
message?: string;
stack?: string;
cause?: unknown;
code?: string;
} & JsonObject;
export type ErrorLike = {
[key: string]: unknown;
name: string;
message: string;
stack: string;
cause?: unknown;
code?: string;
};
export interface Options {
/**
The maximum depth of properties to preserve when serializing/deserializing.
@default Number.POSITIVE_INFINITY
@example
```
import {serializeError} from 'serialize-error';
const error = new Error('🦄');
error.one = {two: {three: {}}};
console.log(serializeError(error, {maxDepth: 1}));
//=> {name: 'Error', message: '…', one: {}}
console.log(serializeError(error, {maxDepth: 2}));
//=> {name: 'Error', message: '…', one: { two: {}}}
```
*/
readonly maxDepth?: number;
/**
Indicate whether to use a `.toJSON()` method if encountered in the object. This is useful when a custom error implements its own serialization logic via `.toJSON()` but you prefer to not use it.
@default true
*/
readonly useToJSON?: boolean;
}
/**
Serialize an `Error` object into a plain object.
- Non-error values are passed through.
- Custom properties are preserved.
- Buffer properties are replaced with `[object Buffer]`.
- Circular references are handled.
- If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
- It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.
@example
```
import {serializeError} from 'serialize-error';
const error = new Error('🦄');
console.log(error);
//=> [Error: 🦄]
console.log(serializeError(error));
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
```
@example
```
import {serializeError} from 'serialize-error';
class ErrorWithDate extends Error {
constructor() {
super();
this.date = new Date();
}
}
const error = new ErrorWithDate();
console.log(serializeError(error));
//=> {date: '1970-01-01T00:00:00.000Z', name, message, stack}
```
@example
```
import {serializeError} from 'serialize-error';
const error = new Error('Unicorn');
error.horn = {
toJSON() {
return 'x';
}
};
serializeError(error);
// => {horn: 'x', name, message, stack}
```
*/
export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
? ErrorType
: ErrorObject;
/**
Deserialize a plain object or any value into an `Error` object.
- `Error` objects are passed through.
- Objects that have at least a `message` property are interpreted as errors.
- All other values are wrapped in a `NonError` error.
- Custom properties are preserved.
- Non-enumerable properties are kept non-enumerable (name, message, stack, cause).
- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
- Circular references are handled.
- Native error constructors are preserved (TypeError, DOMException, etc) and more can be added.
@example
```
import {deserializeError} from 'serialize-error';
const error = deserializeError({
message: 'aaa',
stack: 'at <anonymous>:1:13'
});
console.log(error);
// Error: aaa
// at <anonymous>:1:13
```
*/
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
```
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
```
*/
export function isErrorLike(value: unknown): value is ErrorLike;