@common.js/serialize-error 11.0.0
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 +5 -0
- package/error-constructors.d.ts +8 -0
- package/error-constructors.js +33 -0
- package/index.d.ts +171 -0
- package/index.js +460 -0
- package/license +9 -0
- package/package.json +31 -0
- package/readme.md +198 -0
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @common.js/serialize-error
|
|
2
|
+
|
|
3
|
+
The [serialize-error](https://www.npmjs.com/package/serialize-error) package exported as CommonJS modules.
|
|
4
|
+
|
|
5
|
+
Exported from [serialize-error@11.0.0](https://www.npmjs.com/package/serialize-error/v/11.0.0) using https://github.com/etienne-martin/common.js.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Map of error constructors to recreate from the serialize `name` property. If the name is not found in this map, the errors will be deserialized as simple `Error` instances.
|
|
3
|
+
|
|
4
|
+
Warning: Only simple and standard error constructors are supported, like `new MyCustomError(name)`. 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.
|
|
5
|
+
*/
|
|
6
|
+
declare const errorConstructors: Map<string, ErrorConstructor>;
|
|
7
|
+
|
|
8
|
+
export default errorConstructors;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var list = [
|
|
12
|
+
// Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
|
|
13
|
+
EvalError,
|
|
14
|
+
RangeError,
|
|
15
|
+
ReferenceError,
|
|
16
|
+
SyntaxError,
|
|
17
|
+
TypeError,
|
|
18
|
+
URIError,
|
|
19
|
+
// Built-in errors
|
|
20
|
+
globalThis.DOMException,
|
|
21
|
+
// Node-specific errors
|
|
22
|
+
// https://nodejs.org/api/errors.html
|
|
23
|
+
globalThis.AssertionError,
|
|
24
|
+
globalThis.SystemError,
|
|
25
|
+
]// Non-native Errors are used with `globalThis` because they might be missing. This filter drops them when undefined.
|
|
26
|
+
.filter(Boolean).map(function(constructor) {
|
|
27
|
+
return [
|
|
28
|
+
constructor.name,
|
|
29
|
+
constructor
|
|
30
|
+
];
|
|
31
|
+
});
|
|
32
|
+
var errorConstructors = new Map(list);
|
|
33
|
+
var _default = errorConstructors;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {Primitive, JsonObject} from 'type-fest';
|
|
2
|
+
|
|
3
|
+
export {default as errorConstructors} from './error-constructors.js';
|
|
4
|
+
|
|
5
|
+
export type ErrorObject = {
|
|
6
|
+
name?: string;
|
|
7
|
+
message?: string;
|
|
8
|
+
stack?: string;
|
|
9
|
+
cause?: unknown;
|
|
10
|
+
code?: string;
|
|
11
|
+
} & JsonObject;
|
|
12
|
+
|
|
13
|
+
export type ErrorLike = {
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
name: string;
|
|
16
|
+
message: string;
|
|
17
|
+
stack: string;
|
|
18
|
+
cause?: unknown;
|
|
19
|
+
code?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export interface Options {
|
|
23
|
+
/**
|
|
24
|
+
The maximum depth of properties to preserve when serializing/deserializing.
|
|
25
|
+
|
|
26
|
+
@default Number.POSITIVE_INFINITY
|
|
27
|
+
|
|
28
|
+
@example
|
|
29
|
+
```
|
|
30
|
+
import {serializeError} from 'serialize-error';
|
|
31
|
+
|
|
32
|
+
const error = new Error('🦄');
|
|
33
|
+
error.one = {two: {three: {}}};
|
|
34
|
+
|
|
35
|
+
console.log(serializeError(error, {maxDepth: 1}));
|
|
36
|
+
//=> {name: 'Error', message: '…', one: {}}
|
|
37
|
+
|
|
38
|
+
console.log(serializeError(error, {maxDepth: 2}));
|
|
39
|
+
//=> {name: 'Error', message: '…', one: { two: {}}}
|
|
40
|
+
```
|
|
41
|
+
*/
|
|
42
|
+
readonly maxDepth?: number;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
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()` but you prefer to not use it.
|
|
46
|
+
|
|
47
|
+
@default true
|
|
48
|
+
*/
|
|
49
|
+
readonly useToJSON?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
Serialize an `Error` object into a plain object.
|
|
54
|
+
|
|
55
|
+
- Non-error values are passed through.
|
|
56
|
+
- Custom properties are preserved.
|
|
57
|
+
- Buffer properties are replaced with `[object Buffer]`.
|
|
58
|
+
- Circular references are handled.
|
|
59
|
+
- If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
|
|
60
|
+
- It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.
|
|
61
|
+
|
|
62
|
+
@example
|
|
63
|
+
```
|
|
64
|
+
import {serializeError} from 'serialize-error';
|
|
65
|
+
|
|
66
|
+
const error = new Error('🦄');
|
|
67
|
+
|
|
68
|
+
console.log(error);
|
|
69
|
+
//=> [Error: 🦄]
|
|
70
|
+
|
|
71
|
+
console.log(serializeError(error));
|
|
72
|
+
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
@example
|
|
76
|
+
```
|
|
77
|
+
import {serializeError} from 'serialize-error';
|
|
78
|
+
|
|
79
|
+
class ErrorWithDate extends Error {
|
|
80
|
+
constructor() {
|
|
81
|
+
super();
|
|
82
|
+
this.date = new Date();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const error = new ErrorWithDate();
|
|
87
|
+
|
|
88
|
+
console.log(serializeError(error));
|
|
89
|
+
//=> {date: '1970-01-01T00:00:00.000Z', name, message, stack}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
@example
|
|
93
|
+
```
|
|
94
|
+
import {serializeError} from 'serialize-error';
|
|
95
|
+
|
|
96
|
+
const error = new Error('Unicorn');
|
|
97
|
+
|
|
98
|
+
error.horn = {
|
|
99
|
+
toJSON() {
|
|
100
|
+
return 'x';
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
serializeError(error);
|
|
105
|
+
// => {horn: 'x', name, message, stack}
|
|
106
|
+
```
|
|
107
|
+
*/
|
|
108
|
+
export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
|
|
109
|
+
? ErrorType
|
|
110
|
+
: ErrorObject;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
Deserialize a plain object or any value into an `Error` object.
|
|
114
|
+
|
|
115
|
+
- `Error` objects are passed through.
|
|
116
|
+
- Objects that have at least a `message` property are interpreted as errors.
|
|
117
|
+
- All other values are wrapped in a `NonError` error.
|
|
118
|
+
- Custom properties are preserved.
|
|
119
|
+
- Non-enumerable properties are kept non-enumerable (name, message, stack, cause).
|
|
120
|
+
- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
|
|
121
|
+
- Circular references are handled.
|
|
122
|
+
- Native error constructors are preserved (TypeError, DOMException, etc) and more can be added.
|
|
123
|
+
|
|
124
|
+
@example
|
|
125
|
+
```
|
|
126
|
+
import {deserializeError} from 'serialize-error';
|
|
127
|
+
|
|
128
|
+
const error = deserializeError({
|
|
129
|
+
message: 'aaa',
|
|
130
|
+
stack: 'at <anonymous>:1:13'
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
console.log(error);
|
|
134
|
+
// Error: aaa
|
|
135
|
+
// at <anonymous>:1:13
|
|
136
|
+
```
|
|
137
|
+
*/
|
|
138
|
+
export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
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.
|
|
142
|
+
|
|
143
|
+
@example
|
|
144
|
+
```
|
|
145
|
+
import {isErrorLike} from 'serialize-error';
|
|
146
|
+
|
|
147
|
+
const error = new Error('🦄');
|
|
148
|
+
error.one = {two: {three: {}}};
|
|
149
|
+
|
|
150
|
+
isErrorLike({
|
|
151
|
+
name: 'DOMException',
|
|
152
|
+
message: 'It happened',
|
|
153
|
+
stack: 'at foo (index.js:2:9)',
|
|
154
|
+
});
|
|
155
|
+
//=> true
|
|
156
|
+
|
|
157
|
+
isErrorLike(new Error('🦄'));
|
|
158
|
+
//=> true
|
|
159
|
+
|
|
160
|
+
isErrorLike(serializeError(new Error('🦄'));
|
|
161
|
+
//=> true
|
|
162
|
+
|
|
163
|
+
isErrorLike({
|
|
164
|
+
name: 'Bluberricious pancakes',
|
|
165
|
+
stack: 12,
|
|
166
|
+
ingredients: 'Blueberry',
|
|
167
|
+
});
|
|
168
|
+
//=> false
|
|
169
|
+
```
|
|
170
|
+
*/
|
|
171
|
+
export function isErrorLike(value: unknown): value is ErrorLike;
|
package/index.js
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
NonError: function() {
|
|
13
|
+
return NonError;
|
|
14
|
+
},
|
|
15
|
+
serializeError: function() {
|
|
16
|
+
return serializeError;
|
|
17
|
+
},
|
|
18
|
+
deserializeError: function() {
|
|
19
|
+
return deserializeError;
|
|
20
|
+
},
|
|
21
|
+
isErrorLike: function() {
|
|
22
|
+
return isErrorLike;
|
|
23
|
+
},
|
|
24
|
+
errorConstructors: function() {
|
|
25
|
+
return _errorConstructorsJs.default;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
var _errorConstructorsJs = /*#__PURE__*/ _interopRequireDefault(require("./error-constructors.js"));
|
|
29
|
+
function _arrayLikeToArray(arr, len) {
|
|
30
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
31
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
32
|
+
return arr2;
|
|
33
|
+
}
|
|
34
|
+
function _arrayWithHoles(arr) {
|
|
35
|
+
if (Array.isArray(arr)) return arr;
|
|
36
|
+
}
|
|
37
|
+
function _arrayWithoutHoles(arr) {
|
|
38
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
39
|
+
}
|
|
40
|
+
function _assertThisInitialized(self) {
|
|
41
|
+
if (self === void 0) {
|
|
42
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
43
|
+
}
|
|
44
|
+
return self;
|
|
45
|
+
}
|
|
46
|
+
function _classCallCheck(instance, Constructor) {
|
|
47
|
+
if (!(instance instanceof Constructor)) {
|
|
48
|
+
throw new TypeError("Cannot call a class as a function");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function isNativeReflectConstruct() {
|
|
52
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
53
|
+
if (Reflect.construct.sham) return false;
|
|
54
|
+
if (typeof Proxy === "function") return true;
|
|
55
|
+
try {
|
|
56
|
+
Date.prototype.toString.call(Reflect.construct(Date, [], function() {}));
|
|
57
|
+
return true;
|
|
58
|
+
} catch (e) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function _construct(Parent, args, Class) {
|
|
63
|
+
if (isNativeReflectConstruct()) {
|
|
64
|
+
_construct = Reflect.construct;
|
|
65
|
+
} else {
|
|
66
|
+
_construct = function _construct(Parent, args, Class) {
|
|
67
|
+
var a = [
|
|
68
|
+
null
|
|
69
|
+
];
|
|
70
|
+
a.push.apply(a, args);
|
|
71
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
72
|
+
var instance = new Constructor();
|
|
73
|
+
if (Class) _setPrototypeOf(instance, Class.prototype);
|
|
74
|
+
return instance;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return _construct.apply(null, arguments);
|
|
78
|
+
}
|
|
79
|
+
function _defineProperties(target, props) {
|
|
80
|
+
for(var i = 0; i < props.length; i++){
|
|
81
|
+
var descriptor = props[i];
|
|
82
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
83
|
+
descriptor.configurable = true;
|
|
84
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
85
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
89
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
90
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
91
|
+
return Constructor;
|
|
92
|
+
}
|
|
93
|
+
function _defineProperty(obj, key, value) {
|
|
94
|
+
if (key in obj) {
|
|
95
|
+
Object.defineProperty(obj, key, {
|
|
96
|
+
value: value,
|
|
97
|
+
enumerable: true,
|
|
98
|
+
configurable: true,
|
|
99
|
+
writable: true
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
obj[key] = value;
|
|
103
|
+
}
|
|
104
|
+
return obj;
|
|
105
|
+
}
|
|
106
|
+
function _getPrototypeOf(o) {
|
|
107
|
+
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
108
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
109
|
+
};
|
|
110
|
+
return _getPrototypeOf(o);
|
|
111
|
+
}
|
|
112
|
+
function _inherits(subClass, superClass) {
|
|
113
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
114
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
115
|
+
}
|
|
116
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
117
|
+
constructor: {
|
|
118
|
+
value: subClass,
|
|
119
|
+
writable: true,
|
|
120
|
+
configurable: true
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
if (superClass) _setPrototypeOf(subClass, superClass);
|
|
124
|
+
}
|
|
125
|
+
function _instanceof(left, right) {
|
|
126
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
127
|
+
return !!right[Symbol.hasInstance](left);
|
|
128
|
+
} else {
|
|
129
|
+
return left instanceof right;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function _interopRequireDefault(obj) {
|
|
133
|
+
return obj && obj.__esModule ? obj : {
|
|
134
|
+
default: obj
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function _isNativeFunction(fn) {
|
|
138
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
139
|
+
}
|
|
140
|
+
function _iterableToArray(iter) {
|
|
141
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
142
|
+
}
|
|
143
|
+
function _iterableToArrayLimit(arr, i) {
|
|
144
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
145
|
+
if (_i == null) return;
|
|
146
|
+
var _arr = [];
|
|
147
|
+
var _n = true;
|
|
148
|
+
var _d = false;
|
|
149
|
+
var _s, _e;
|
|
150
|
+
try {
|
|
151
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
152
|
+
_arr.push(_s.value);
|
|
153
|
+
if (i && _arr.length === i) break;
|
|
154
|
+
}
|
|
155
|
+
} catch (err) {
|
|
156
|
+
_d = true;
|
|
157
|
+
_e = err;
|
|
158
|
+
} finally{
|
|
159
|
+
try {
|
|
160
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
161
|
+
} finally{
|
|
162
|
+
if (_d) throw _e;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return _arr;
|
|
166
|
+
}
|
|
167
|
+
function _nonIterableRest() {
|
|
168
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
169
|
+
}
|
|
170
|
+
function _nonIterableSpread() {
|
|
171
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
172
|
+
}
|
|
173
|
+
function _possibleConstructorReturn(self, call) {
|
|
174
|
+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
|
|
175
|
+
return call;
|
|
176
|
+
}
|
|
177
|
+
return _assertThisInitialized(self);
|
|
178
|
+
}
|
|
179
|
+
function _setPrototypeOf(o, p) {
|
|
180
|
+
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
181
|
+
o.__proto__ = p;
|
|
182
|
+
return o;
|
|
183
|
+
};
|
|
184
|
+
return _setPrototypeOf(o, p);
|
|
185
|
+
}
|
|
186
|
+
function _slicedToArray(arr, i) {
|
|
187
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
188
|
+
}
|
|
189
|
+
function _toConsumableArray(arr) {
|
|
190
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
191
|
+
}
|
|
192
|
+
var _typeof = function(obj) {
|
|
193
|
+
"@swc/helpers - typeof";
|
|
194
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
195
|
+
};
|
|
196
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
197
|
+
if (!o) return;
|
|
198
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
199
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
200
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
201
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
202
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
203
|
+
}
|
|
204
|
+
function _wrapNativeSuper(Class) {
|
|
205
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
206
|
+
_wrapNativeSuper = function _wrapNativeSuper(Class) {
|
|
207
|
+
if (Class === null || !_isNativeFunction(Class)) return Class;
|
|
208
|
+
if (typeof Class !== "function") {
|
|
209
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
210
|
+
}
|
|
211
|
+
if (typeof _cache !== "undefined") {
|
|
212
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
213
|
+
_cache.set(Class, Wrapper);
|
|
214
|
+
}
|
|
215
|
+
function Wrapper() {
|
|
216
|
+
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
|
217
|
+
}
|
|
218
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
219
|
+
constructor: {
|
|
220
|
+
value: Wrapper,
|
|
221
|
+
enumerable: false,
|
|
222
|
+
writable: true,
|
|
223
|
+
configurable: true
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
return _setPrototypeOf(Wrapper, Class);
|
|
227
|
+
};
|
|
228
|
+
return _wrapNativeSuper(Class);
|
|
229
|
+
}
|
|
230
|
+
function _isNativeReflectConstruct() {
|
|
231
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
232
|
+
if (Reflect.construct.sham) return false;
|
|
233
|
+
if (typeof Proxy === "function") return true;
|
|
234
|
+
try {
|
|
235
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
236
|
+
return true;
|
|
237
|
+
} catch (e) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function _createSuper(Derived) {
|
|
242
|
+
var hasNativeReflectConstruct = _isNativeReflectConstruct();
|
|
243
|
+
return function _createSuperInternal() {
|
|
244
|
+
var Super = _getPrototypeOf(Derived), result;
|
|
245
|
+
if (hasNativeReflectConstruct) {
|
|
246
|
+
var NewTarget = _getPrototypeOf(this).constructor;
|
|
247
|
+
result = Reflect.construct(Super, arguments, NewTarget);
|
|
248
|
+
} else {
|
|
249
|
+
result = Super.apply(this, arguments);
|
|
250
|
+
}
|
|
251
|
+
return _possibleConstructorReturn(this, result);
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
var NonError = /*#__PURE__*/ function(Error1) {
|
|
255
|
+
"use strict";
|
|
256
|
+
_inherits(NonError, Error1);
|
|
257
|
+
var _super = _createSuper(NonError);
|
|
258
|
+
function NonError(message) {
|
|
259
|
+
_classCallCheck(this, NonError);
|
|
260
|
+
var _this;
|
|
261
|
+
_this = _super.call(this, NonError._prepareSuperMessage(message));
|
|
262
|
+
_defineProperty(_assertThisInitialized(_this), "name", "NonError");
|
|
263
|
+
return _this;
|
|
264
|
+
}
|
|
265
|
+
_createClass(NonError, null, [
|
|
266
|
+
{
|
|
267
|
+
key: "_prepareSuperMessage",
|
|
268
|
+
value: function _prepareSuperMessage(message) {
|
|
269
|
+
try {
|
|
270
|
+
return JSON.stringify(message);
|
|
271
|
+
} catch (e) {
|
|
272
|
+
return String(message);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
]);
|
|
277
|
+
return NonError;
|
|
278
|
+
}(_wrapNativeSuper(Error));
|
|
279
|
+
var commonProperties = [
|
|
280
|
+
{
|
|
281
|
+
property: "name",
|
|
282
|
+
enumerable: false
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
property: "message",
|
|
286
|
+
enumerable: false
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
property: "stack",
|
|
290
|
+
enumerable: false
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
property: "code",
|
|
294
|
+
enumerable: true
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
property: "cause",
|
|
298
|
+
enumerable: false
|
|
299
|
+
},
|
|
300
|
+
];
|
|
301
|
+
var toJsonWasCalled = Symbol(".toJSON was called");
|
|
302
|
+
var toJSON = function(from) {
|
|
303
|
+
from[toJsonWasCalled] = true;
|
|
304
|
+
var json = from.toJSON();
|
|
305
|
+
delete from[toJsonWasCalled];
|
|
306
|
+
return json;
|
|
307
|
+
};
|
|
308
|
+
var ref;
|
|
309
|
+
var getErrorConstructor = function(name) {
|
|
310
|
+
return (ref = _errorConstructorsJs.default.get(name)) !== null && ref !== void 0 ? ref : Error;
|
|
311
|
+
};
|
|
312
|
+
// eslint-disable-next-line complexity
|
|
313
|
+
var destroyCircular = function(param) {
|
|
314
|
+
var from = param.from, seen = param.seen, to = param.to, forceEnumerable = param.forceEnumerable, maxDepth = param.maxDepth, depth = param.depth, useToJSON = param.useToJSON, serialize = param.serialize;
|
|
315
|
+
if (!to) {
|
|
316
|
+
if (Array.isArray(from)) {
|
|
317
|
+
to = [];
|
|
318
|
+
} else if (!serialize && isErrorLike(from)) {
|
|
319
|
+
var _$Error = getErrorConstructor(from.name);
|
|
320
|
+
to = new _$Error();
|
|
321
|
+
} else {
|
|
322
|
+
to = {};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
seen.push(from);
|
|
326
|
+
if (depth >= maxDepth) {
|
|
327
|
+
return to;
|
|
328
|
+
}
|
|
329
|
+
if (useToJSON && typeof from.toJSON === "function" && from[toJsonWasCalled] !== true) {
|
|
330
|
+
return toJSON(from);
|
|
331
|
+
}
|
|
332
|
+
var continueDestroyCircular = function(value) {
|
|
333
|
+
return destroyCircular({
|
|
334
|
+
from: value,
|
|
335
|
+
seen: _toConsumableArray(seen),
|
|
336
|
+
forceEnumerable: forceEnumerable,
|
|
337
|
+
maxDepth: maxDepth,
|
|
338
|
+
depth: depth,
|
|
339
|
+
useToJSON: useToJSON,
|
|
340
|
+
serialize: serialize
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
344
|
+
try {
|
|
345
|
+
for(var _iterator = Object.entries(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
346
|
+
var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
|
|
347
|
+
// eslint-disable-next-line node/prefer-global/buffer
|
|
348
|
+
if (typeof Buffer === "function" && Buffer.isBuffer(value)) {
|
|
349
|
+
to[key] = "[object Buffer]";
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
// TODO: Use `stream.isReadable()` when targeting Node.js 18.
|
|
353
|
+
if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
|
|
354
|
+
to[key] = "[object Stream]";
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
if (typeof value === "function") {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (!value || typeof value !== "object") {
|
|
361
|
+
to[key] = value;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (!seen.includes(from[key])) {
|
|
365
|
+
depth++;
|
|
366
|
+
to[key] = continueDestroyCircular(from[key]);
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
to[key] = "[Circular]";
|
|
370
|
+
}
|
|
371
|
+
} catch (err) {
|
|
372
|
+
_didIteratorError = true;
|
|
373
|
+
_iteratorError = err;
|
|
374
|
+
} finally{
|
|
375
|
+
try {
|
|
376
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
377
|
+
_iterator.return();
|
|
378
|
+
}
|
|
379
|
+
} finally{
|
|
380
|
+
if (_didIteratorError) {
|
|
381
|
+
throw _iteratorError;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
386
|
+
try {
|
|
387
|
+
for(var _iterator1 = commonProperties[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
388
|
+
var _value1 = _step1.value, property = _value1.property, enumerable = _value1.enumerable;
|
|
389
|
+
if (typeof from[property] !== "undefined" && from[property] !== null) {
|
|
390
|
+
Object.defineProperty(to, property, {
|
|
391
|
+
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
|
|
392
|
+
enumerable: forceEnumerable ? true : enumerable,
|
|
393
|
+
configurable: true,
|
|
394
|
+
writable: true
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
} catch (err) {
|
|
399
|
+
_didIteratorError1 = true;
|
|
400
|
+
_iteratorError1 = err;
|
|
401
|
+
} finally{
|
|
402
|
+
try {
|
|
403
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
404
|
+
_iterator1.return();
|
|
405
|
+
}
|
|
406
|
+
} finally{
|
|
407
|
+
if (_didIteratorError1) {
|
|
408
|
+
throw _iteratorError1;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return to;
|
|
413
|
+
};
|
|
414
|
+
function serializeError(value) {
|
|
415
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
416
|
+
var _maxDepth = options.maxDepth, maxDepth = _maxDepth === void 0 ? Number.POSITIVE_INFINITY : _maxDepth, _useToJSON = options.useToJSON, useToJSON = _useToJSON === void 0 ? true : _useToJSON;
|
|
417
|
+
if (typeof value === "object" && value !== null) {
|
|
418
|
+
return destroyCircular({
|
|
419
|
+
from: value,
|
|
420
|
+
seen: [],
|
|
421
|
+
forceEnumerable: true,
|
|
422
|
+
maxDepth: maxDepth,
|
|
423
|
+
depth: 0,
|
|
424
|
+
useToJSON: useToJSON,
|
|
425
|
+
serialize: true
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
// People sometimes throw things besides Error objects…
|
|
429
|
+
if (typeof value === "function") {
|
|
430
|
+
var _name;
|
|
431
|
+
// `JSON.stringify()` discards functions. We do too, unless a function is thrown directly.
|
|
432
|
+
return "[Function: ".concat((_name = value.name) !== null && _name !== void 0 ? _name : "anonymous", "]");
|
|
433
|
+
}
|
|
434
|
+
return value;
|
|
435
|
+
}
|
|
436
|
+
function deserializeError(value) {
|
|
437
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
438
|
+
var _maxDepth = options.maxDepth, maxDepth = _maxDepth === void 0 ? Number.POSITIVE_INFINITY : _maxDepth;
|
|
439
|
+
if (_instanceof(value, Error)) {
|
|
440
|
+
return value;
|
|
441
|
+
}
|
|
442
|
+
if (isMinimumViableSerializedError(value)) {
|
|
443
|
+
var _$Error = getErrorConstructor(value.name);
|
|
444
|
+
return destroyCircular({
|
|
445
|
+
from: value,
|
|
446
|
+
seen: [],
|
|
447
|
+
to: new _$Error(),
|
|
448
|
+
maxDepth: maxDepth,
|
|
449
|
+
depth: 0,
|
|
450
|
+
serialize: false
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
return new NonError(value);
|
|
454
|
+
}
|
|
455
|
+
function isErrorLike(value) {
|
|
456
|
+
return Boolean(value) && typeof value === "object" && "name" in value && "message" in value && "stack" in value;
|
|
457
|
+
}
|
|
458
|
+
function isMinimumViableSerializedError(value) {
|
|
459
|
+
return Boolean(value) && typeof value === "object" && "message" in value && !Array.isArray(value);
|
|
460
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/serialize-error",
|
|
3
|
+
"version": "11.0.0",
|
|
4
|
+
"description": "Serialize/deserialize an error into a plain object",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "etienne-martin/common.js",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=14.16"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "xo && ava && tsd"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"error-constructors.js",
|
|
19
|
+
"error-constructors.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"type-fest": "^2.12.2"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"ava": "^4.2.0",
|
|
26
|
+
"tsd": "^0.20.0",
|
|
27
|
+
"xo": "^0.48.0"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
30
|
+
"main": "./index.js"
|
|
31
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
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
|
+
```
|