source: node_modules/serialize-error/readme.md

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.6 KB
Line 
1# serialize-error
2
3> Serialize/deserialize an error into a plain object
4
5Useful if you for example need to `JSON.stringify()` or `process.send()` the error.
6
7## Install
8
9```
10$ npm install serialize-error
11```
12
13## Usage
14
15```js
16const {serializeError, deserializeError} = require('serialize-error');
17
18const error = new Error('🦄');
19
20console.log(error);
21//=> [Error: 🦄]
22
23const serialized = serializeError(error)
24
25console.log(serialized);
26//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
27
28const deserialized = deserializeError(serialized);
29
30console.log(deserialized);
31//=> [Error: 🦄]
32```
33
34## API
35
36### serializeError(value, options?)
37
38Type: `Error | unknown`
39
40Serialize an `Error` object into a plain object.
41
42Non-error values are passed through.
43Custom properties are preserved.
44Non-enumerable properties are kept non-enumerable (name, message, stack).
45Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
46Buffer properties are replaced with `[object Buffer]`.
47Circular references are handled.
48If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
49It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.
50
51`.toJSON` example:
52
53```js
54class ErrorWithDate extends Error {
55 constructor() {
56 super();
57 this.date = new Date();
58 }
59}
60const error = new ErrorWithDate();
61serializeError(date);
62// => {date: '1970-01-01T00:00:00.000Z', name, message, stack}
63
64class ErrorWithToJSON extends Error {
65 constructor() {
66 super('🦄');
67 this.date = new Date();
68 }
69
70 toJSON() {
71 return serializeError(this);
72 }
73}
74const error = new ErrorWithToJSON();
75console.log(serializeError(error));
76// => {date: '1970-01-01T00:00:00.000Z', message: '🦄', name, stack}
77```
78
79### deserializeError(value, options?)
80
81Type: `{[key: string]: unknown} | unknown`
82
83Deserialize a plain object or any value into an `Error` object.
84
85`Error` objects are passed through.
86Non-error values are wrapped in a `NonError` error.
87Custom properties are preserved.
88Circular references are handled.
89
90### options
91
92Type: `object`
93
94#### maxDepth
95
96Type: `number`\
97Default: `Number.POSITIVE_INFINITY`
98
99The maximum depth of properties to preserve when serializing/deserializing.
100
101```js
102const {serializeError} = require('serialize-error');
103
104const error = new Error('🦄');
105error.one = {two: {three: {}}};
106
107console.log(serializeError(error, {maxDepth: 1}));
108//=> {name: 'Error', message: '…', one: {}}
109
110console.log(serializeError(error, {maxDepth: 2}));
111//=> {name: 'Error', message: '…', one: { two: {}}}
112```
Note: See TracBrowser for help on using the repository browser.