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