1 | import {Primitive, JsonObject} from 'type-fest';
|
---|
2 |
|
---|
3 | export type ErrorObject = {
|
---|
4 | name?: string;
|
---|
5 | stack?: string;
|
---|
6 | message?: string;
|
---|
7 | code?: string;
|
---|
8 | } & JsonObject;
|
---|
9 |
|
---|
10 | export interface Options {
|
---|
11 | /**
|
---|
12 | The maximum depth of properties to preserve when serializing/deserializing.
|
---|
13 |
|
---|
14 | @default Number.POSITIVE_INFINITY
|
---|
15 |
|
---|
16 | @example
|
---|
17 | ```
|
---|
18 | import {serializeError} from 'serialize-error';
|
---|
19 |
|
---|
20 | const error = new Error('🦄');
|
---|
21 | error.one = {two: {three: {}}};
|
---|
22 |
|
---|
23 | console.log(serializeError(error, {maxDepth: 1}));
|
---|
24 | //=> {name: 'Error', message: '…', one: {}}
|
---|
25 |
|
---|
26 | console.log(serializeError(error, {maxDepth: 2}));
|
---|
27 | //=> {name: 'Error', message: '…', one: { two: {}}}
|
---|
28 | ```
|
---|
29 | */
|
---|
30 | readonly maxDepth?: number;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | Serialize an `Error` object into a plain object.
|
---|
35 |
|
---|
36 | Non-error values are passed through.
|
---|
37 | Custom properties are preserved.
|
---|
38 | Buffer properties are replaced with `[object Buffer]`.
|
---|
39 | Circular references are handled.
|
---|
40 | If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
|
---|
41 | It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.
|
---|
42 |
|
---|
43 | @example
|
---|
44 | ```
|
---|
45 | import {serializeError} from 'serialize-error';
|
---|
46 |
|
---|
47 | const error = new Error('🦄');
|
---|
48 |
|
---|
49 | console.log(error);
|
---|
50 | //=> [Error: 🦄]
|
---|
51 |
|
---|
52 | console.log(serializeError(error));
|
---|
53 | //=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
|
---|
54 |
|
---|
55 | class ErrorWithDate extends Error {
|
---|
56 | constructor() {
|
---|
57 | super();
|
---|
58 | this.date = new Date();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | const error = new ErrorWithDate();
|
---|
62 |
|
---|
63 | console.log(serializeError(error));
|
---|
64 | //=> {date: '1970-01-01T00:00:00.000Z', name, message, stack}
|
---|
65 |
|
---|
66 | class ErrorWithToJSON extends Error {
|
---|
67 | constructor() {
|
---|
68 | super('🦄');
|
---|
69 | this.date = new Date();
|
---|
70 | }
|
---|
71 |
|
---|
72 | toJSON() {
|
---|
73 | return serializeError(this);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | const error = new ErrorWithToJSON();
|
---|
77 | console.log(serializeError(error));
|
---|
78 | // => {date: '1970-01-01T00:00:00.000Z', message: '🦄', name, stack}
|
---|
79 | ```
|
---|
80 | */
|
---|
81 | export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
|
---|
82 | ? ErrorType
|
---|
83 | : ErrorObject;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | Deserialize a plain object or any value into an `Error` object.
|
---|
87 |
|
---|
88 | `Error` objects are passed through.
|
---|
89 | Non-error values are wrapped in a `NonError` error.
|
---|
90 | Custom properties are preserved.
|
---|
91 | Non-enumerable properties are kept non-enumerable (name, message, stack).
|
---|
92 | Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
|
---|
93 | Circular references are handled.
|
---|
94 |
|
---|
95 | @example
|
---|
96 | ```
|
---|
97 | import {deserializeError} from 'serialize-error';
|
---|
98 |
|
---|
99 | const error = deserializeError({
|
---|
100 | message: 'aaa',
|
---|
101 | stack: 'at <anonymous>:1:13'
|
---|
102 | });
|
---|
103 |
|
---|
104 | console.log(error);
|
---|
105 | // Error: aaa
|
---|
106 | // at <anonymous>:1:13
|
---|
107 | ```
|
---|
108 | */
|
---|
109 | export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error;
|
---|