@common.js/serialize-error 11.0.0 → 11.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.js +4 -4
- package/package.json +4 -3
- package/readme.md +0 -198
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [serialize-error](https://www.npmjs.com/package/serialize-error) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [serialize-error@11.0.
|
|
5
|
+
Exported from [serialize-error@11.0.1](https://www.npmjs.com/package/serialize-error/v/11.0.1) using https://github.com/etienne-martin/common.js.
|
package/index.js
CHANGED
|
@@ -298,11 +298,11 @@ var commonProperties = [
|
|
|
298
298
|
enumerable: false
|
|
299
299
|
},
|
|
300
300
|
];
|
|
301
|
-
var toJsonWasCalled =
|
|
301
|
+
var toJsonWasCalled = new WeakSet();
|
|
302
302
|
var toJSON = function(from) {
|
|
303
|
-
from
|
|
303
|
+
toJsonWasCalled.add(from);
|
|
304
304
|
var json = from.toJSON();
|
|
305
|
-
delete
|
|
305
|
+
toJsonWasCalled.delete(from);
|
|
306
306
|
return json;
|
|
307
307
|
};
|
|
308
308
|
var ref;
|
|
@@ -326,7 +326,7 @@ var destroyCircular = function(param) {
|
|
|
326
326
|
if (depth >= maxDepth) {
|
|
327
327
|
return to;
|
|
328
328
|
}
|
|
329
|
-
if (useToJSON && typeof from.toJSON === "function" && from
|
|
329
|
+
if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
|
|
330
330
|
return toJSON(from);
|
|
331
331
|
}
|
|
332
332
|
var continueDestroyCircular = function(value) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/serialize-error",
|
|
3
|
-
"version": "11.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "11.0.1",
|
|
4
|
+
"description": "serialize-error package exported as CommonJS modules",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "etienne-martin/common.js",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"node": ">=14.16"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
|
-
"test": "xo && ava && tsd"
|
|
13
|
+
"//test": "xo && ava && tsd",
|
|
14
|
+
"test": "ava && tsd"
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"index.js",
|
package/readme.md
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
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
|
-
```sh
|
|
10
|
-
npm install serialize-error
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
```js
|
|
16
|
-
import {serializeError, deserializeError} from '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
|
-
### Error constructors
|
|
35
|
-
|
|
36
|
-
When a serialized error with a known `name` is encountered, it will be deserialized using the corresponding error constructor, while unknown error names will be deserialized as regular errors:
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
import {deserializeError} from 'serialize-error';
|
|
40
|
-
|
|
41
|
-
const known = deserializeError({
|
|
42
|
-
name: 'TypeError',
|
|
43
|
-
message: '🦄'
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
console.log(known);
|
|
47
|
-
//=> [TypeError: 🦄] <-- Still a TypeError
|
|
48
|
-
|
|
49
|
-
const unknown = deserializeError({
|
|
50
|
-
name: 'TooManyCooksError',
|
|
51
|
-
message: '🦄'
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
console.log(unknown);
|
|
55
|
-
//=> [Error: 🦄] <-- Just a regular Error
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
The [list of known errors](./error-constructors.js) can be extended globally. This also works if `serialize-error` is a sub-dependency that's not used directly.
|
|
59
|
-
|
|
60
|
-
```js
|
|
61
|
-
import {errorConstructors} from 'serialize-error';
|
|
62
|
-
import {MyCustomError} from './errors.js'
|
|
63
|
-
|
|
64
|
-
errorConstructors.set('MyCustomError', MyCustomError)
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
**Warning:** Only simple and standard error constructors are supported, like `new MyCustomError(message)`. If your error constructor **requires** a second parameter or does not accept a string as first parameter, adding it to this map **will** break the deserialization.
|
|
68
|
-
|
|
69
|
-
## API
|
|
70
|
-
|
|
71
|
-
### serializeError(value, options?)
|
|
72
|
-
|
|
73
|
-
Serialize an `Error` object into a plain object.
|
|
74
|
-
|
|
75
|
-
- Non-error values are passed through.
|
|
76
|
-
- Custom properties are preserved.
|
|
77
|
-
- Non-enumerable properties are kept non-enumerable (name, message, stack).
|
|
78
|
-
- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
|
|
79
|
-
- Buffer properties are replaced with `[object Buffer]`.
|
|
80
|
-
- Circular references are handled.
|
|
81
|
-
- If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
|
|
82
|
-
- It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.
|
|
83
|
-
|
|
84
|
-
### value
|
|
85
|
-
|
|
86
|
-
Type: `Error | unknown`
|
|
87
|
-
|
|
88
|
-
### toJSON implementation examples
|
|
89
|
-
|
|
90
|
-
```js
|
|
91
|
-
import {serializeError} from 'serialize-error';
|
|
92
|
-
|
|
93
|
-
class ErrorWithDate extends Error {
|
|
94
|
-
constructor() {
|
|
95
|
-
super();
|
|
96
|
-
this.date = new Date();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const error = new ErrorWithDate();
|
|
101
|
-
|
|
102
|
-
serializeError(error);
|
|
103
|
-
// => {date: '1970-01-01T00:00:00.000Z', name, message, stack}
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
```js
|
|
107
|
-
import {serializeError} from 'serialize-error';
|
|
108
|
-
|
|
109
|
-
const error = new Error('Unicorn');
|
|
110
|
-
|
|
111
|
-
error.horn = {
|
|
112
|
-
toJSON() {
|
|
113
|
-
return 'x';
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
serializeError(error);
|
|
118
|
-
// => {horn: 'x', name, message, stack}
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### deserializeError(value, options?)
|
|
122
|
-
|
|
123
|
-
Deserialize a plain object or any value into an `Error` object.
|
|
124
|
-
|
|
125
|
-
- `Error` objects are passed through.
|
|
126
|
-
- Objects that have at least a `message` property are interpreted as errors.
|
|
127
|
-
- All other values are wrapped in a `NonError` error.
|
|
128
|
-
- Custom properties are preserved.
|
|
129
|
-
- Non-enumerable properties are kept non-enumerable (name, message, stack, cause).
|
|
130
|
-
- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
|
|
131
|
-
- Circular references are handled.
|
|
132
|
-
- [Native error constructors](./error-constructors.js) are preserved (TypeError, DOMException, etc) and [more can be added.](#error-constructors)
|
|
133
|
-
|
|
134
|
-
### value
|
|
135
|
-
|
|
136
|
-
Type: `{message: string} | unknown`
|
|
137
|
-
|
|
138
|
-
### options
|
|
139
|
-
|
|
140
|
-
Type: `object`
|
|
141
|
-
|
|
142
|
-
#### maxDepth
|
|
143
|
-
|
|
144
|
-
Type: `number`\
|
|
145
|
-
Default: `Number.POSITIVE_INFINITY`
|
|
146
|
-
|
|
147
|
-
The maximum depth of properties to preserve when serializing/deserializing.
|
|
148
|
-
|
|
149
|
-
```js
|
|
150
|
-
import {serializeError} from 'serialize-error';
|
|
151
|
-
|
|
152
|
-
const error = new Error('🦄');
|
|
153
|
-
error.one = {two: {three: {}}};
|
|
154
|
-
|
|
155
|
-
console.log(serializeError(error, {maxDepth: 1}));
|
|
156
|
-
//=> {name: 'Error', message: '🦄', one: {}}
|
|
157
|
-
|
|
158
|
-
console.log(serializeError(error, {maxDepth: 2}));
|
|
159
|
-
//=> {name: 'Error', message: '🦄', one: { two: {}}}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
#### useToJSON
|
|
163
|
-
|
|
164
|
-
Type: `boolean`\
|
|
165
|
-
Default: `true`
|
|
166
|
-
|
|
167
|
-
Indicate whether to use a `.toJSON()` method if encountered in the object. This is useful when a custom error implements [its own serialization logic via `.toJSON()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior) but you prefer to not use it.
|
|
168
|
-
|
|
169
|
-
### isErrorLike(value)
|
|
170
|
-
|
|
171
|
-
Predicate to determine whether a value looks like an error, even if it's not an instance of `Error`. It must have at least the `name`, `message`, and `stack` properties.
|
|
172
|
-
|
|
173
|
-
```js
|
|
174
|
-
import {isErrorLike} from 'serialize-error';
|
|
175
|
-
|
|
176
|
-
const error = new Error('🦄');
|
|
177
|
-
error.one = {two: {three: {}}};
|
|
178
|
-
|
|
179
|
-
isErrorLike({
|
|
180
|
-
name: 'DOMException',
|
|
181
|
-
message: 'It happened',
|
|
182
|
-
stack: 'at foo (index.js:2:9)',
|
|
183
|
-
});
|
|
184
|
-
//=> true
|
|
185
|
-
|
|
186
|
-
isErrorLike(new Error('🦄'));
|
|
187
|
-
//=> true
|
|
188
|
-
|
|
189
|
-
isErrorLike(serializeError(new Error('🦄'));
|
|
190
|
-
//=> true
|
|
191
|
-
|
|
192
|
-
isErrorLike({
|
|
193
|
-
name: 'Bluberricious pancakes',
|
|
194
|
-
stack: 12,
|
|
195
|
-
ingredients: 'Blueberry',
|
|
196
|
-
});
|
|
197
|
-
//=> false
|
|
198
|
-
```
|