forked from sindresorhus/serialize-error
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
122 lines (95 loc) · 2.85 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
import {Primitive, JsonObject} from 'type-fest';
export type ErrorObject = {
name?: string;
stack?: string;
message?: string;
code?: string;
} & JsonObject;
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;
}
/**
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';
class ErrorWithToJSON extends Error {
constructor() {
super('🦄');
this.date = new Date();
}
toJSON() {
return serializeError(this);
}
}
const error = new ErrorWithToJSON();
console.log(serializeError(error));
// => {date: '1970-01-01T00:00:00.000Z', message: '🦄', name, 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.
- Non-error 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.
@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;