source: node_modules/serialize-error/index.d.ts

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import {Primitive, JsonObject} from 'type-fest';
2
3export type ErrorObject = {
4 name?: string;
5 stack?: string;
6 message?: string;
7 code?: string;
8} & JsonObject;
9
10export 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/**
34Serialize an `Error` object into a plain object.
35
36Non-error values are passed through.
37Custom properties are preserved.
38Buffer properties are replaced with `[object Buffer]`.
39Circular references are handled.
40If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
41It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.
42
43@example
44```
45import {serializeError} from 'serialize-error';
46
47const error = new Error('🦄');
48
49console.log(error);
50//=> [Error: 🦄]
51
52console.log(serializeError(error));
53//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
54
55class ErrorWithDate extends Error {
56 constructor() {
57 super();
58 this.date = new Date();
59 }
60}
61const error = new ErrorWithDate();
62
63console.log(serializeError(error));
64//=> {date: '1970-01-01T00:00:00.000Z', name, message, stack}
65
66class ErrorWithToJSON extends Error {
67 constructor() {
68 super('🦄');
69 this.date = new Date();
70 }
71
72 toJSON() {
73 return serializeError(this);
74 }
75}
76const error = new ErrorWithToJSON();
77console.log(serializeError(error));
78// => {date: '1970-01-01T00:00:00.000Z', message: '🦄', name, stack}
79```
80*/
81export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
82 ? ErrorType
83 : ErrorObject;
84
85/**
86Deserialize a plain object or any value into an `Error` object.
87
88`Error` objects are passed through.
89Non-error values are wrapped in a `NonError` error.
90Custom properties are preserved.
91Non-enumerable properties are kept non-enumerable (name, message, stack).
92Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
93Circular references are handled.
94
95@example
96```
97import {deserializeError} from 'serialize-error';
98
99const error = deserializeError({
100 message: 'aaa',
101 stack: 'at <anonymous>:1:13'
102});
103
104console.log(error);
105// Error: aaa
106// at <anonymous>:1:13
107```
108*/
109export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error;
Note: See TracBrowser for help on using the repository browser.