@devp0nt/error0 1.0.0-next.5 → 1.0.0-next.51
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/dist/cjs/index.cjs +614 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +345 -414
- package/dist/cjs/plugins/cause.cjs +63 -0
- package/dist/cjs/plugins/cause.cjs.map +1 -0
- package/dist/cjs/plugins/cause.d.cts +15 -0
- package/dist/cjs/plugins/code.cjs +46 -0
- package/dist/cjs/plugins/code.cjs.map +1 -0
- package/dist/cjs/plugins/code.d.cts +8 -0
- package/dist/cjs/plugins/expected.cjs +67 -0
- package/dist/cjs/plugins/expected.cjs.map +1 -0
- package/dist/cjs/plugins/expected.d.cts +37 -0
- package/dist/cjs/plugins/message-merge.cjs +39 -0
- package/dist/cjs/plugins/message-merge.cjs.map +1 -0
- package/dist/cjs/plugins/message-merge.d.cts +8 -0
- package/dist/cjs/plugins/meta.cjs +78 -0
- package/dist/cjs/plugins/meta.cjs.map +1 -0
- package/dist/cjs/plugins/meta.d.cts +7 -0
- package/dist/cjs/plugins/stack-merge.cjs +42 -0
- package/dist/cjs/plugins/stack-merge.cjs.map +1 -0
- package/dist/cjs/plugins/stack-merge.d.cts +8 -0
- package/dist/cjs/plugins/status.cjs +60 -0
- package/dist/cjs/plugins/status.cjs.map +1 -0
- package/dist/cjs/plugins/status.d.cts +9 -0
- package/dist/cjs/plugins/tags.cjs +73 -0
- package/dist/cjs/plugins/tags.cjs.map +1 -0
- package/dist/cjs/plugins/tags.d.cts +12 -0
- package/dist/esm/index.d.ts +345 -414
- package/dist/esm/index.js +530 -341
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugins/cause.d.ts +15 -0
- package/dist/esm/plugins/cause.js +39 -0
- package/dist/esm/plugins/cause.js.map +1 -0
- package/dist/esm/plugins/code.d.ts +8 -0
- package/dist/esm/plugins/code.js +22 -0
- package/dist/esm/plugins/code.js.map +1 -0
- package/dist/esm/plugins/expected.d.ts +37 -0
- package/dist/esm/plugins/expected.js +43 -0
- package/dist/esm/plugins/expected.js.map +1 -0
- package/dist/esm/plugins/message-merge.d.ts +8 -0
- package/dist/esm/plugins/message-merge.js +15 -0
- package/dist/esm/plugins/message-merge.js.map +1 -0
- package/dist/esm/plugins/meta.d.ts +7 -0
- package/dist/esm/plugins/meta.js +54 -0
- package/dist/esm/plugins/meta.js.map +1 -0
- package/dist/esm/plugins/stack-merge.d.ts +8 -0
- package/dist/esm/plugins/stack-merge.js +18 -0
- package/dist/esm/plugins/stack-merge.js.map +1 -0
- package/dist/esm/plugins/status.d.ts +9 -0
- package/dist/esm/plugins/status.js +36 -0
- package/dist/esm/plugins/status.js.map +1 -0
- package/dist/esm/plugins/tags.d.ts +12 -0
- package/dist/esm/plugins/tags.js +49 -0
- package/dist/esm/plugins/tags.js.map +1 -0
- package/package.json +53 -23
- package/src/index.test.ts +696 -452
- package/src/index.ts +1178 -502
- package/src/plugins/cause.test.ts +106 -0
- package/src/plugins/cause.ts +45 -0
- package/src/plugins/code.test.ts +27 -0
- package/src/plugins/code.ts +20 -0
- package/src/plugins/expected.test.ts +66 -0
- package/src/plugins/expected.ts +48 -0
- package/src/plugins/message-merge.test.ts +32 -0
- package/src/plugins/message-merge.ts +19 -0
- package/src/plugins/meta.test.ts +32 -0
- package/src/plugins/meta.ts +59 -0
- package/src/plugins/stack-merge.test.ts +57 -0
- package/src/plugins/stack-merge.ts +20 -0
- package/src/plugins/status.test.ts +54 -0
- package/src/plugins/status.ts +35 -0
- package/src/plugins/tags.test.ts +74 -0
- package/src/plugins/tags.ts +51 -0
- package/dist/cjs/index.js +0 -435
- package/dist/cjs/index.js.map +0 -1
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
Error0: () => Error0,
|
|
22
|
+
PluginError0: () => PluginError0
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(index_exports);
|
|
25
|
+
const RESERVED_STACK_PROP_ERROR = 'Error0: "stack" is a reserved prop key. Use .stack(...) plugin API instead';
|
|
26
|
+
const RESERVED_MESSAGE_PROP_ERROR = 'Error0: "message" is a reserved prop key. Use .message(...) plugin API instead';
|
|
27
|
+
class PluginError0 {
|
|
28
|
+
_plugin;
|
|
29
|
+
Infer = void 0;
|
|
30
|
+
constructor(plugin) {
|
|
31
|
+
this._plugin = {
|
|
32
|
+
props: { ...plugin?.props ?? {} },
|
|
33
|
+
methods: { ...plugin?.methods ?? {} },
|
|
34
|
+
adapt: [...plugin?.adapt ?? []],
|
|
35
|
+
stack: plugin?.stack,
|
|
36
|
+
cause: plugin?.cause,
|
|
37
|
+
message: plugin?.message
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
prop(key, value) {
|
|
41
|
+
return this.use("prop", key, value);
|
|
42
|
+
}
|
|
43
|
+
method(key, value) {
|
|
44
|
+
return this.use("method", key, value);
|
|
45
|
+
}
|
|
46
|
+
adapt(value) {
|
|
47
|
+
return this.use("adapt", value);
|
|
48
|
+
}
|
|
49
|
+
stack(value) {
|
|
50
|
+
return this.use("stack", value);
|
|
51
|
+
}
|
|
52
|
+
cause(value) {
|
|
53
|
+
return this.use("cause", value);
|
|
54
|
+
}
|
|
55
|
+
message(value) {
|
|
56
|
+
return this.use("message", value);
|
|
57
|
+
}
|
|
58
|
+
use(kind, keyOrValue, value) {
|
|
59
|
+
const nextProps = { ...this._plugin.props ?? {} };
|
|
60
|
+
const nextMethods = { ...this._plugin.methods ?? {} };
|
|
61
|
+
const nextAdapt = [...this._plugin.adapt ?? []];
|
|
62
|
+
let nextStack = this._plugin.stack;
|
|
63
|
+
let nextCause = this._plugin.cause;
|
|
64
|
+
let nextMessage = this._plugin.message;
|
|
65
|
+
if (kind === "prop") {
|
|
66
|
+
const key = keyOrValue;
|
|
67
|
+
if (key === "stack") {
|
|
68
|
+
throw new Error(RESERVED_STACK_PROP_ERROR);
|
|
69
|
+
}
|
|
70
|
+
if (key === "message") {
|
|
71
|
+
throw new Error(RESERVED_MESSAGE_PROP_ERROR);
|
|
72
|
+
}
|
|
73
|
+
if (value === void 0) {
|
|
74
|
+
throw new Error('PluginError0.use("prop", key, value) requires value');
|
|
75
|
+
}
|
|
76
|
+
nextProps[key] = value;
|
|
77
|
+
} else if (kind === "method") {
|
|
78
|
+
const key = keyOrValue;
|
|
79
|
+
if (value === void 0) {
|
|
80
|
+
throw new Error('PluginError0.use("method", key, value) requires value');
|
|
81
|
+
}
|
|
82
|
+
nextMethods[key] = value;
|
|
83
|
+
} else if (kind === "adapt") {
|
|
84
|
+
nextAdapt.push(keyOrValue);
|
|
85
|
+
} else if (kind === "stack") {
|
|
86
|
+
nextStack = keyOrValue;
|
|
87
|
+
} else if (kind === "cause") {
|
|
88
|
+
nextCause = keyOrValue;
|
|
89
|
+
} else {
|
|
90
|
+
nextMessage = keyOrValue;
|
|
91
|
+
}
|
|
92
|
+
return new PluginError0({
|
|
93
|
+
props: nextProps,
|
|
94
|
+
methods: nextMethods,
|
|
95
|
+
adapt: nextAdapt,
|
|
96
|
+
stack: nextStack,
|
|
97
|
+
cause: nextCause,
|
|
98
|
+
message: nextMessage
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const OWN_SYMBOL = Symbol("Error0.own");
|
|
103
|
+
class Error0 extends Error {
|
|
104
|
+
static __pluginsMap;
|
|
105
|
+
__pluginsMap;
|
|
106
|
+
static MAX_CAUSES_DEPTH = 99;
|
|
107
|
+
static _plugins = [];
|
|
108
|
+
static _resolvedPlugin;
|
|
109
|
+
static _emptyPlugin = {
|
|
110
|
+
props: {},
|
|
111
|
+
methods: {},
|
|
112
|
+
adapt: [],
|
|
113
|
+
stack: void 0,
|
|
114
|
+
cause: void 0,
|
|
115
|
+
message: void 0,
|
|
116
|
+
propKeys: [],
|
|
117
|
+
propEntries: [],
|
|
118
|
+
methodEntries: []
|
|
119
|
+
};
|
|
120
|
+
static _indexResolvedPlugin(resolved) {
|
|
121
|
+
return {
|
|
122
|
+
...resolved,
|
|
123
|
+
propKeys: Object.keys(resolved.props),
|
|
124
|
+
propEntries: Object.entries(resolved.props),
|
|
125
|
+
methodEntries: Object.entries(resolved.methods)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
static _applyPlugin(resolved, plugin) {
|
|
129
|
+
if (plugin.props && "stack" in plugin.props) {
|
|
130
|
+
throw new Error(RESERVED_STACK_PROP_ERROR);
|
|
131
|
+
}
|
|
132
|
+
if (plugin.props && "message" in plugin.props) {
|
|
133
|
+
throw new Error(RESERVED_MESSAGE_PROP_ERROR);
|
|
134
|
+
}
|
|
135
|
+
Object.assign(resolved.props, plugin.props ?? this._emptyPlugin.props);
|
|
136
|
+
Object.assign(resolved.methods, plugin.methods ?? this._emptyPlugin.methods);
|
|
137
|
+
resolved.adapt.push(...plugin.adapt ?? this._emptyPlugin.adapt);
|
|
138
|
+
if (typeof plugin.stack !== "undefined") {
|
|
139
|
+
resolved.stack = plugin.stack;
|
|
140
|
+
}
|
|
141
|
+
if (typeof plugin.cause !== "undefined") {
|
|
142
|
+
resolved.cause = plugin.cause;
|
|
143
|
+
}
|
|
144
|
+
if (typeof plugin.message !== "undefined") {
|
|
145
|
+
resolved.message = plugin.message;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
static _mergeResolvedPlugin(base, plugin) {
|
|
149
|
+
const merged = {
|
|
150
|
+
props: { ...base.props },
|
|
151
|
+
methods: { ...base.methods },
|
|
152
|
+
adapt: [...base.adapt],
|
|
153
|
+
stack: base.stack,
|
|
154
|
+
cause: base.cause,
|
|
155
|
+
message: base.message
|
|
156
|
+
};
|
|
157
|
+
this._applyPlugin(merged, plugin);
|
|
158
|
+
return this._indexResolvedPlugin(merged);
|
|
159
|
+
}
|
|
160
|
+
static _getResolvedPlugin() {
|
|
161
|
+
if (Object.prototype.hasOwnProperty.call(this, "_resolvedPlugin") && this._resolvedPlugin) {
|
|
162
|
+
return this._resolvedPlugin;
|
|
163
|
+
}
|
|
164
|
+
const resolved = {
|
|
165
|
+
props: {},
|
|
166
|
+
methods: {},
|
|
167
|
+
adapt: [],
|
|
168
|
+
propKeys: [],
|
|
169
|
+
propEntries: [],
|
|
170
|
+
methodEntries: []
|
|
171
|
+
};
|
|
172
|
+
for (const plugin of this._plugins) {
|
|
173
|
+
this._applyPlugin(resolved, plugin);
|
|
174
|
+
}
|
|
175
|
+
const indexed = this._indexResolvedPlugin(resolved);
|
|
176
|
+
Object.defineProperty(this, "_resolvedPlugin", {
|
|
177
|
+
value: indexed,
|
|
178
|
+
writable: true,
|
|
179
|
+
enumerable: false,
|
|
180
|
+
configurable: true
|
|
181
|
+
});
|
|
182
|
+
return indexed;
|
|
183
|
+
}
|
|
184
|
+
constructor(...args) {
|
|
185
|
+
const [first, second] = args;
|
|
186
|
+
const input = typeof first === "string" ? { message: first, ...second ?? {} } : first;
|
|
187
|
+
super(input.message, { cause: input.cause });
|
|
188
|
+
this.name = "Error0";
|
|
189
|
+
const ctor = this.constructor;
|
|
190
|
+
const plugin = ctor._getResolvedPlugin();
|
|
191
|
+
const ownStore = /* @__PURE__ */ Object.create(null);
|
|
192
|
+
Object.defineProperty(this, OWN_SYMBOL, { value: ownStore, writable: true, enumerable: false, configurable: true });
|
|
193
|
+
for (const [key, prop] of plugin.propEntries) {
|
|
194
|
+
if (key === "stack") {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
Object.defineProperty(this, key, {
|
|
198
|
+
get: () => prop.resolve({
|
|
199
|
+
own: ownStore[key],
|
|
200
|
+
flow: this.flow(key),
|
|
201
|
+
error: this
|
|
202
|
+
}),
|
|
203
|
+
set: (value) => {
|
|
204
|
+
ownStore[key] = value;
|
|
205
|
+
},
|
|
206
|
+
enumerable: true,
|
|
207
|
+
configurable: true
|
|
208
|
+
});
|
|
209
|
+
if (key in input) {
|
|
210
|
+
const ownValue = input[key];
|
|
211
|
+
ownStore[key] = typeof prop.init === "function" ? prop.init(ownValue) : ownValue;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
static _getOwnStore(object) {
|
|
216
|
+
const record = object;
|
|
217
|
+
const existing = record[OWN_SYMBOL];
|
|
218
|
+
if (existing && typeof existing === "object") {
|
|
219
|
+
return existing;
|
|
220
|
+
}
|
|
221
|
+
return void 0;
|
|
222
|
+
}
|
|
223
|
+
static isOwnProperty = (object, key) => {
|
|
224
|
+
const ownStore = this._getOwnStore(object);
|
|
225
|
+
if (ownStore) {
|
|
226
|
+
return Object.prototype.hasOwnProperty.call(ownStore, key);
|
|
227
|
+
}
|
|
228
|
+
return !!Object.getOwnPropertyDescriptor(object, key);
|
|
229
|
+
};
|
|
230
|
+
static _ownByKey(error, key) {
|
|
231
|
+
const ownStore = this._getOwnStore(error);
|
|
232
|
+
if (ownStore) {
|
|
233
|
+
return ownStore[key];
|
|
234
|
+
}
|
|
235
|
+
return error[key];
|
|
236
|
+
}
|
|
237
|
+
static _flowByKey(error, key) {
|
|
238
|
+
const causes = this.causes(error, true);
|
|
239
|
+
const values = new Array(causes.length);
|
|
240
|
+
for (let i = 0; i < causes.length; i += 1) {
|
|
241
|
+
values[i] = this._ownByKey(causes[i], key);
|
|
242
|
+
}
|
|
243
|
+
return values;
|
|
244
|
+
}
|
|
245
|
+
static own(error, key) {
|
|
246
|
+
const error0 = this.from(error);
|
|
247
|
+
if (key === void 0) {
|
|
248
|
+
const ownValues = {};
|
|
249
|
+
const plugin = this._getResolvedPlugin();
|
|
250
|
+
for (const ownKey of plugin.propKeys) {
|
|
251
|
+
ownValues[ownKey] = this._ownByKey(error0, ownKey);
|
|
252
|
+
}
|
|
253
|
+
return ownValues;
|
|
254
|
+
}
|
|
255
|
+
return this._ownByKey(error0, key);
|
|
256
|
+
}
|
|
257
|
+
own(key) {
|
|
258
|
+
const ctor = this.constructor;
|
|
259
|
+
if (key === void 0) {
|
|
260
|
+
return ctor.own(this);
|
|
261
|
+
}
|
|
262
|
+
return ctor._ownByKey(this, key);
|
|
263
|
+
}
|
|
264
|
+
static flow(error, key) {
|
|
265
|
+
const error0 = this.from(error);
|
|
266
|
+
return this._flowByKey(error0, key);
|
|
267
|
+
}
|
|
268
|
+
flow(key) {
|
|
269
|
+
const ctor = this.constructor;
|
|
270
|
+
return ctor._flowByKey(this, key);
|
|
271
|
+
}
|
|
272
|
+
static _resolveByKey(error, key, plugin) {
|
|
273
|
+
try {
|
|
274
|
+
const options = {
|
|
275
|
+
get own() {
|
|
276
|
+
return error.own(key);
|
|
277
|
+
},
|
|
278
|
+
get flow() {
|
|
279
|
+
return error.flow(key);
|
|
280
|
+
},
|
|
281
|
+
error
|
|
282
|
+
};
|
|
283
|
+
const prop = plugin.props[key];
|
|
284
|
+
const resolver = prop.resolve;
|
|
285
|
+
if (!resolver) {
|
|
286
|
+
return error[key];
|
|
287
|
+
}
|
|
288
|
+
return resolver(options);
|
|
289
|
+
} catch {
|
|
290
|
+
console.error(`Error0: failed to resolve property ${key}`, error);
|
|
291
|
+
return void 0;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
static resolve(error) {
|
|
295
|
+
const error0 = this.from(error);
|
|
296
|
+
const resolved = {};
|
|
297
|
+
const plugin = this._getResolvedPlugin();
|
|
298
|
+
for (const key of plugin.propKeys) {
|
|
299
|
+
resolved[key] = this._resolveByKey(error0, key, plugin);
|
|
300
|
+
}
|
|
301
|
+
return resolved;
|
|
302
|
+
}
|
|
303
|
+
resolve() {
|
|
304
|
+
const ctor = this.constructor;
|
|
305
|
+
return ctor.resolve(this);
|
|
306
|
+
}
|
|
307
|
+
static causes(error, instancesOnly) {
|
|
308
|
+
const causes = [];
|
|
309
|
+
let current = error;
|
|
310
|
+
const seen = /* @__PURE__ */ new Set();
|
|
311
|
+
let depth = 0;
|
|
312
|
+
while (depth < this.MAX_CAUSES_DEPTH) {
|
|
313
|
+
if (seen.has(current)) {
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
seen.add(current);
|
|
317
|
+
if (!instancesOnly || this.is(current)) {
|
|
318
|
+
causes.push(current);
|
|
319
|
+
}
|
|
320
|
+
if (!current || typeof current !== "object") {
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
current = current.cause;
|
|
324
|
+
depth += 1;
|
|
325
|
+
}
|
|
326
|
+
return causes;
|
|
327
|
+
}
|
|
328
|
+
causes(instancesOnly) {
|
|
329
|
+
const ctor = this.constructor;
|
|
330
|
+
if (instancesOnly) {
|
|
331
|
+
return ctor.causes(this, true);
|
|
332
|
+
}
|
|
333
|
+
return ctor.causes(this);
|
|
334
|
+
}
|
|
335
|
+
static is(error) {
|
|
336
|
+
return error instanceof this;
|
|
337
|
+
}
|
|
338
|
+
static isSerialized(error) {
|
|
339
|
+
return !this.is(error) && typeof error === "object" && error !== null && "name" in error && error.name === "Error0";
|
|
340
|
+
}
|
|
341
|
+
static from(error) {
|
|
342
|
+
if (this.is(error)) {
|
|
343
|
+
return error;
|
|
344
|
+
}
|
|
345
|
+
if (this.isSerialized(error)) {
|
|
346
|
+
return this._fromSerialized(error);
|
|
347
|
+
}
|
|
348
|
+
return this._fromNonError0(error);
|
|
349
|
+
}
|
|
350
|
+
static round(error, isPublic = false) {
|
|
351
|
+
return this.from(this.serialize(error, isPublic));
|
|
352
|
+
}
|
|
353
|
+
static _applyAdapt(error) {
|
|
354
|
+
const plugin = this._getResolvedPlugin();
|
|
355
|
+
for (const adapt of plugin.adapt) {
|
|
356
|
+
const adapted = adapt(error);
|
|
357
|
+
if (adapted && typeof adapted === "object") {
|
|
358
|
+
Object.assign(error, adapted);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return error;
|
|
362
|
+
}
|
|
363
|
+
static _fromSerialized(error) {
|
|
364
|
+
const message = this._extractMessage(error);
|
|
365
|
+
if (typeof error !== "object" || error === null) {
|
|
366
|
+
return this._applyAdapt(new this(message, { cause: error }));
|
|
367
|
+
}
|
|
368
|
+
const errorRecord = error;
|
|
369
|
+
const recreated = new this(message);
|
|
370
|
+
const plugin = this._getResolvedPlugin();
|
|
371
|
+
for (const [key, prop] of plugin.propEntries) {
|
|
372
|
+
if (prop.deserialize === false) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (!(key in errorRecord)) {
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
try {
|
|
379
|
+
const value = prop.deserialize({ value: errorRecord[key], record: errorRecord });
|
|
380
|
+
recreated[key] = value;
|
|
381
|
+
} catch {
|
|
382
|
+
console.error(`Error0: failed to deserialize property ${key}`, errorRecord);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if ("stack" in errorRecord) {
|
|
386
|
+
try {
|
|
387
|
+
if (typeof errorRecord.stack === "string") {
|
|
388
|
+
recreated.stack = errorRecord.stack;
|
|
389
|
+
}
|
|
390
|
+
} catch {
|
|
391
|
+
console.error("Error0: failed to deserialize stack", errorRecord);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const causePlugin = plugin.cause;
|
|
395
|
+
if (causePlugin && "cause" in errorRecord) {
|
|
396
|
+
try {
|
|
397
|
+
;
|
|
398
|
+
recreated.cause = causePlugin.deserialize({
|
|
399
|
+
cause: errorRecord.cause,
|
|
400
|
+
error: errorRecord,
|
|
401
|
+
isSerialized: (serializedCause) => this.isSerialized(serializedCause),
|
|
402
|
+
fromSerialized: (serializedCause) => this._fromSerialized(serializedCause)
|
|
403
|
+
});
|
|
404
|
+
} catch {
|
|
405
|
+
console.error("Error0: failed to deserialize cause", errorRecord);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return recreated;
|
|
409
|
+
}
|
|
410
|
+
static _fromNonError0(error) {
|
|
411
|
+
const message = this._extractMessage(error);
|
|
412
|
+
return this._applyAdapt(new this(message, { cause: error }));
|
|
413
|
+
}
|
|
414
|
+
static _extractMessage(error) {
|
|
415
|
+
return (typeof error === "string" ? error : typeof error === "object" && error !== null && "message" in error && typeof error.message === "string" ? error.message : void 0) || "Unknown error";
|
|
416
|
+
}
|
|
417
|
+
static _useWithPlugin(plugin) {
|
|
418
|
+
const Base = this;
|
|
419
|
+
const Error0Extended = class Error0 extends Base {
|
|
420
|
+
};
|
|
421
|
+
Error0Extended._plugins = [...Base._plugins, plugin];
|
|
422
|
+
const resolved = this._mergeResolvedPlugin(Base._getResolvedPlugin(), plugin);
|
|
423
|
+
Error0Extended._resolvedPlugin = resolved;
|
|
424
|
+
for (const [key, method] of resolved.methodEntries) {
|
|
425
|
+
Object.defineProperty(Error0Extended.prototype, key, {
|
|
426
|
+
value: function(...args) {
|
|
427
|
+
return method(this, ...args);
|
|
428
|
+
},
|
|
429
|
+
writable: true,
|
|
430
|
+
enumerable: true,
|
|
431
|
+
configurable: true
|
|
432
|
+
});
|
|
433
|
+
Object.defineProperty(Error0Extended, key, {
|
|
434
|
+
value: function(error, ...args) {
|
|
435
|
+
return method(this.from(error), ...args);
|
|
436
|
+
},
|
|
437
|
+
writable: true,
|
|
438
|
+
enumerable: true,
|
|
439
|
+
configurable: true
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
return Error0Extended;
|
|
443
|
+
}
|
|
444
|
+
static _pluginFromBuilder(plugin) {
|
|
445
|
+
const pluginRecord = plugin;
|
|
446
|
+
return {
|
|
447
|
+
props: { ...pluginRecord._plugin.props ?? {} },
|
|
448
|
+
methods: { ...pluginRecord._plugin.methods ?? {} },
|
|
449
|
+
adapt: [...pluginRecord._plugin.adapt ?? []],
|
|
450
|
+
stack: pluginRecord._plugin.stack,
|
|
451
|
+
cause: pluginRecord._plugin.cause,
|
|
452
|
+
message: pluginRecord._plugin.message
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
static use(first, key, value) {
|
|
456
|
+
if (first instanceof PluginError0) {
|
|
457
|
+
return this._useWithPlugin(this._pluginFromBuilder(first));
|
|
458
|
+
}
|
|
459
|
+
if (first === "stack") {
|
|
460
|
+
if (typeof key === "undefined") {
|
|
461
|
+
throw new Error('Error0.use("stack", value) requires stack plugin value');
|
|
462
|
+
}
|
|
463
|
+
if (typeof key !== "object" || key === null || typeof key.serialize !== "function") {
|
|
464
|
+
throw new Error('Error0.use("stack", value) expects { serialize: function }');
|
|
465
|
+
}
|
|
466
|
+
return this._useWithPlugin({
|
|
467
|
+
stack: key
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
if (first === "cause") {
|
|
471
|
+
if (typeof key === "undefined") {
|
|
472
|
+
throw new Error('Error0.use("cause", value) requires cause plugin value');
|
|
473
|
+
}
|
|
474
|
+
if (typeof key !== "object" || key === null || typeof key.serialize !== "function" || typeof key.deserialize !== "function") {
|
|
475
|
+
throw new Error('Error0.use("cause", value) expects { serialize: function, deserialize: function }');
|
|
476
|
+
}
|
|
477
|
+
return this._useWithPlugin({
|
|
478
|
+
cause: key
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
if (first === "message") {
|
|
482
|
+
if (typeof key === "undefined") {
|
|
483
|
+
throw new Error('Error0.use("message", value) requires message plugin value');
|
|
484
|
+
}
|
|
485
|
+
if (typeof key !== "object" || key === null || typeof key.serialize !== "function") {
|
|
486
|
+
throw new Error('Error0.use("message", value) expects { serialize: function }');
|
|
487
|
+
}
|
|
488
|
+
return this._useWithPlugin({
|
|
489
|
+
message: key
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
if (first === "adapt") {
|
|
493
|
+
if (typeof key !== "function") {
|
|
494
|
+
throw new Error('Error0.use("adapt", value) requires adapt function');
|
|
495
|
+
}
|
|
496
|
+
return this._useWithPlugin({
|
|
497
|
+
adapt: [key]
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
if (typeof key !== "string" || value === void 0) {
|
|
501
|
+
throw new Error("Error0.use(kind, key, value) requires key and value");
|
|
502
|
+
}
|
|
503
|
+
if (first === "prop") {
|
|
504
|
+
if (key === "stack") {
|
|
505
|
+
throw new Error(RESERVED_STACK_PROP_ERROR);
|
|
506
|
+
}
|
|
507
|
+
if (key === "message") {
|
|
508
|
+
throw new Error(RESERVED_MESSAGE_PROP_ERROR);
|
|
509
|
+
}
|
|
510
|
+
return this._useWithPlugin({
|
|
511
|
+
props: { [key]: value }
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
return this._useWithPlugin({
|
|
515
|
+
methods: { [key]: value }
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
static plugin() {
|
|
519
|
+
return new PluginError0();
|
|
520
|
+
}
|
|
521
|
+
static serialize(error, isPublic = true) {
|
|
522
|
+
const error0 = this.from(error);
|
|
523
|
+
const plugin = this._getResolvedPlugin();
|
|
524
|
+
const resolveByKey = (targetError, key, targetPlugin) => this._resolveByKey(targetError, key, targetPlugin);
|
|
525
|
+
const messagePlugin = plugin.message;
|
|
526
|
+
let serializedMessage = error0.message;
|
|
527
|
+
try {
|
|
528
|
+
if (messagePlugin) {
|
|
529
|
+
serializedMessage = messagePlugin.serialize({ value: error0.message, error: error0, isPublic });
|
|
530
|
+
}
|
|
531
|
+
} catch {
|
|
532
|
+
console.error("Error0: failed to serialize message", error0);
|
|
533
|
+
serializedMessage = error0.message;
|
|
534
|
+
}
|
|
535
|
+
const json = {
|
|
536
|
+
name: error0.name
|
|
537
|
+
};
|
|
538
|
+
if (serializedMessage !== void 0) {
|
|
539
|
+
json.message = serializedMessage;
|
|
540
|
+
}
|
|
541
|
+
for (const [key, prop] of plugin.propEntries) {
|
|
542
|
+
if (prop.serialize === false) {
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
try {
|
|
546
|
+
const options = {
|
|
547
|
+
get own() {
|
|
548
|
+
return error0.own(key);
|
|
549
|
+
},
|
|
550
|
+
get flow() {
|
|
551
|
+
return error0.flow(key);
|
|
552
|
+
},
|
|
553
|
+
get resolved() {
|
|
554
|
+
return resolveByKey(error0, key, plugin);
|
|
555
|
+
},
|
|
556
|
+
error: error0,
|
|
557
|
+
isPublic
|
|
558
|
+
};
|
|
559
|
+
const jsonValue = prop.serialize(options);
|
|
560
|
+
if (jsonValue !== void 0) {
|
|
561
|
+
json[key] = jsonValue;
|
|
562
|
+
}
|
|
563
|
+
} catch {
|
|
564
|
+
console.error(`Error0: failed to serialize property ${key}`, error0);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
const stackPlugin = plugin.stack;
|
|
568
|
+
try {
|
|
569
|
+
let serializedStack;
|
|
570
|
+
if (stackPlugin) {
|
|
571
|
+
serializedStack = stackPlugin.serialize({ value: error0.stack, error: error0, isPublic });
|
|
572
|
+
} else {
|
|
573
|
+
serializedStack = isPublic ? void 0 : error0.stack;
|
|
574
|
+
}
|
|
575
|
+
if (serializedStack !== void 0) {
|
|
576
|
+
json.stack = serializedStack;
|
|
577
|
+
}
|
|
578
|
+
} catch {
|
|
579
|
+
console.error("Error0: failed to serialize stack", error0);
|
|
580
|
+
}
|
|
581
|
+
const causePlugin = plugin.cause;
|
|
582
|
+
if (causePlugin?.serialize) {
|
|
583
|
+
try {
|
|
584
|
+
const serializedCause = causePlugin.serialize({
|
|
585
|
+
cause: error0.cause,
|
|
586
|
+
error: error0,
|
|
587
|
+
isPublic,
|
|
588
|
+
is: (cause) => this.is(cause),
|
|
589
|
+
serialize: (cause) => this.serialize(cause, isPublic)
|
|
590
|
+
});
|
|
591
|
+
if (serializedCause !== void 0) {
|
|
592
|
+
json.cause = serializedCause;
|
|
593
|
+
}
|
|
594
|
+
} catch {
|
|
595
|
+
console.error("Error0: failed to serialize cause", error0);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return json;
|
|
599
|
+
}
|
|
600
|
+
serialize(isPublic = true) {
|
|
601
|
+
const ctor = this.constructor;
|
|
602
|
+
return ctor.serialize(this, isPublic);
|
|
603
|
+
}
|
|
604
|
+
round(isPublic = true) {
|
|
605
|
+
const ctor = this.constructor;
|
|
606
|
+
return ctor.round(this, isPublic);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
610
|
+
0 && (module.exports = {
|
|
611
|
+
Error0,
|
|
612
|
+
PluginError0
|
|
613
|
+
});
|
|
614
|
+
//# sourceMappingURL=index.cjs.map
|