@devp0nt/error0 1.0.0-next.4 → 1.0.0-next.41
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 +342 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +173 -415
- package/dist/esm/index.d.ts +173 -415
- package/dist/esm/index.js +268 -351
- package/dist/esm/index.js.map +1 -1
- package/package.json +45 -22
- package/src/index.test.ts +295 -478
- package/src/index.ts +566 -508
- package/dist/cjs/index.js +0 -435
- package/dist/cjs/index.js.map +0 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,400 +1,317 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
code;
|
|
10
|
-
httpStatus;
|
|
11
|
-
expected;
|
|
12
|
-
clientMessage;
|
|
13
|
-
anyMessage;
|
|
14
|
-
cause;
|
|
15
|
-
meta;
|
|
16
|
-
zodError;
|
|
17
|
-
axiosError;
|
|
18
|
-
static defaultMessage = "Unknown error";
|
|
19
|
-
static defaultCode;
|
|
20
|
-
static defaultHttpStatus;
|
|
21
|
-
static defaultExpected;
|
|
22
|
-
static defaultClientMessage;
|
|
23
|
-
static defaultMeta;
|
|
24
|
-
propsOriginal;
|
|
25
|
-
constructor(...args) {
|
|
26
|
-
const input = {};
|
|
27
|
-
if (args[0] instanceof Error) {
|
|
28
|
-
input.cause = args[0];
|
|
29
|
-
} else if (typeof args[0] === "object" && args[0] !== null) {
|
|
30
|
-
Object.assign(input, args[0]);
|
|
31
|
-
} else if (typeof args[0] === "string") {
|
|
32
|
-
input.message = args[0];
|
|
33
|
-
}
|
|
34
|
-
if (typeof args[1] === "object" && args[1] !== null) {
|
|
35
|
-
Object.assign(input, args[1]);
|
|
36
|
-
}
|
|
37
|
-
const safeInput = Error0._safeParseInput(input);
|
|
38
|
-
const message = safeInput.message || Error0.defaultMessage;
|
|
39
|
-
super(message);
|
|
40
|
-
Object.setPrototypeOf(this, this.constructor.prototype);
|
|
41
|
-
this.name = "Error0";
|
|
42
|
-
this.propsOriginal = this.constructor._getSelfGeneralProps({
|
|
43
|
-
error0Input: safeInput,
|
|
44
|
-
message,
|
|
45
|
-
stack: safeInput.stack || this.stack
|
|
46
|
-
});
|
|
47
|
-
const causesProps = this.constructor._getCausesPropsFromError0Props(
|
|
48
|
-
this.propsOriginal,
|
|
49
|
-
this.constructor.defaultMaxLevel
|
|
50
|
-
);
|
|
51
|
-
const propsFloated = this.constructor._getSelfPropsFloated(causesProps);
|
|
52
|
-
this.tag = propsFloated.tag;
|
|
53
|
-
this.code = propsFloated.code;
|
|
54
|
-
this.httpStatus = propsFloated.httpStatus;
|
|
55
|
-
this.expected = propsFloated.expected;
|
|
56
|
-
this.clientMessage = propsFloated.clientMessage;
|
|
57
|
-
this.cause = propsFloated.cause;
|
|
58
|
-
this.stack = propsFloated.stack;
|
|
59
|
-
this.meta = propsFloated.meta;
|
|
60
|
-
this.zodError = propsFloated.zodError;
|
|
61
|
-
this.axiosError = propsFloated.axiosError;
|
|
62
|
-
}
|
|
63
|
-
// settings
|
|
64
|
-
static defaultMaxLevel = 10;
|
|
65
|
-
// props
|
|
66
|
-
static _safeParseInput(error0Input) {
|
|
67
|
-
const result = {};
|
|
68
|
-
result.message = typeof error0Input.message === "string" ? error0Input.message : void 0;
|
|
69
|
-
result.tag = typeof error0Input.tag === "string" ? error0Input.tag : void 0;
|
|
70
|
-
result.code = typeof error0Input.code === "string" ? error0Input.code : void 0;
|
|
71
|
-
result.httpStatus = typeof error0Input.httpStatus === "number" || typeof error0Input.httpStatus === "string" ? error0Input.httpStatus : void 0;
|
|
72
|
-
result.expected = typeof error0Input.expected === "function" || typeof error0Input.expected === "boolean" ? error0Input.expected : void 0;
|
|
73
|
-
result.clientMessage = typeof error0Input.clientMessage === "string" ? error0Input.clientMessage : void 0;
|
|
74
|
-
result.cause = error0Input.cause;
|
|
75
|
-
result.stack = typeof error0Input.stack === "string" ? error0Input.stack : void 0;
|
|
76
|
-
result.meta = error0Input.meta instanceof Meta0 ? error0Input.meta : typeof error0Input.meta === "object" && error0Input.meta !== null ? error0Input.meta : void 0;
|
|
77
|
-
result.zodError = error0Input.zodError instanceof ZodError ? error0Input.zodError : void 0;
|
|
78
|
-
result.axiosError = isAxiosError(error0Input.axiosError) ? error0Input.axiosError : void 0;
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
static _getSelfGeneralProps({
|
|
82
|
-
error0Input,
|
|
83
|
-
message,
|
|
84
|
-
stack
|
|
85
|
-
}) {
|
|
86
|
-
const meta0 = Meta0.extend(error0Input.meta, this.defaultMeta);
|
|
87
|
-
const meta = meta0.getValue();
|
|
88
|
-
const finalTag = meta0.getFinalTag(error0Input.tag);
|
|
89
|
-
const clientMessage = error0Input.clientMessage || this.defaultClientMessage;
|
|
90
|
-
const result = {
|
|
91
|
-
message: error0Input.message || this.defaultMessage,
|
|
92
|
-
tag: finalTag,
|
|
93
|
-
code: error0Input.code || meta.code || this.defaultCode,
|
|
94
|
-
httpStatus: typeof error0Input.httpStatus === "number" ? error0Input.httpStatus : error0Input.httpStatus && typeof error0Input.httpStatus === "string" && error0Input.httpStatus in HttpStatusCode ? HttpStatusCode[error0Input.httpStatus] : meta.httpStatus || this.defaultHttpStatus,
|
|
95
|
-
expected: void 0,
|
|
96
|
-
clientMessage,
|
|
97
|
-
anyMessage: clientMessage || message,
|
|
98
|
-
cause: error0Input.cause,
|
|
99
|
-
stack: void 0,
|
|
100
|
-
meta,
|
|
101
|
-
zodError: error0Input.zodError,
|
|
102
|
-
axiosError: error0Input.axiosError
|
|
1
|
+
class ExtensionError0 {
|
|
2
|
+
_extension;
|
|
3
|
+
Infer = void 0;
|
|
4
|
+
constructor(extension) {
|
|
5
|
+
this._extension = {
|
|
6
|
+
props: { ...extension?.props ?? {} },
|
|
7
|
+
methods: { ...extension?.methods ?? {} },
|
|
8
|
+
refine: [...extension?.refine ?? []]
|
|
103
9
|
};
|
|
104
|
-
result.expected = this._normalizeSelfExpected(
|
|
105
|
-
result,
|
|
106
|
-
typeof error0Input.expected === "boolean" || typeof error0Input.expected === "function" ? error0Input.expected : meta.expected || this.defaultExpected
|
|
107
|
-
);
|
|
108
|
-
result.stack = this._removeConstructorStackPart(stack);
|
|
109
|
-
return result;
|
|
110
10
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const stack = this._mergeStack(causesProps[1]?.stack, causesProps[0]?.stack);
|
|
114
|
-
const closestTag = this._getClosestPropValue(causesProps, "tag");
|
|
115
|
-
const meta = this._getMergedMetaValue(causesProps);
|
|
116
|
-
const tag = Meta0.getFinalTag(meta, closestTag);
|
|
117
|
-
const propsFloated = {
|
|
118
|
-
message: this._getClosestPropValue(causesProps, "message"),
|
|
119
|
-
tag,
|
|
120
|
-
code: this._getClosestPropValue(causesProps, "code"),
|
|
121
|
-
httpStatus: this._getClosestPropValue(causesProps, "httpStatus"),
|
|
122
|
-
expected: this._isExpected(causesProps),
|
|
123
|
-
clientMessage: this._getClosestPropValue(causesProps, "clientMessage"),
|
|
124
|
-
cause,
|
|
125
|
-
stack,
|
|
126
|
-
anyMessage: causesProps[0].anyMessage,
|
|
127
|
-
meta,
|
|
128
|
-
zodError: this._getClosestPropValue(causesProps, "zodError"),
|
|
129
|
-
axiosError: this._getClosestPropValue(causesProps, "axiosError")
|
|
130
|
-
};
|
|
131
|
-
return propsFloated;
|
|
11
|
+
prop(key, value) {
|
|
12
|
+
return this.extend("prop", key, value);
|
|
132
13
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
return {
|
|
136
|
-
message: `Zod Validation Error: ${zodError.message}`
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
static _getExtraError0PropsByAxiosError(axiosError) {
|
|
140
|
-
return {
|
|
141
|
-
message: "Axios Error",
|
|
142
|
-
meta: {
|
|
143
|
-
axiosData: (() => {
|
|
144
|
-
try {
|
|
145
|
-
return JSON.stringify(axiosError.response?.data);
|
|
146
|
-
} catch {
|
|
147
|
-
return void 0;
|
|
148
|
-
}
|
|
149
|
-
})(),
|
|
150
|
-
axiosStatus: axiosError.response?.status
|
|
151
|
-
}
|
|
152
|
-
};
|
|
14
|
+
method(key, value) {
|
|
15
|
+
return this.extend("method", key, value);
|
|
153
16
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
Object.assign(error0Props, extraError0Props, { meta: metaValue });
|
|
17
|
+
refine(value) {
|
|
18
|
+
return this.extend("refine", value);
|
|
157
19
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (causeProps.expected === false) {
|
|
169
|
-
return false;
|
|
20
|
+
extend(kind, keyOrValue, value) {
|
|
21
|
+
const nextProps = { ...this._extension.props ?? {} };
|
|
22
|
+
const nextMethods = { ...this._extension.methods ?? {} };
|
|
23
|
+
const nextRefine = [
|
|
24
|
+
...this._extension.refine ?? []
|
|
25
|
+
];
|
|
26
|
+
if (kind === "prop") {
|
|
27
|
+
const key = keyOrValue;
|
|
28
|
+
if (value === void 0) {
|
|
29
|
+
throw new Error('ExtensionError0.extend("prop", key, value) requires value');
|
|
170
30
|
}
|
|
171
|
-
|
|
172
|
-
|
|
31
|
+
nextProps[key] = value;
|
|
32
|
+
} else if (kind === "method") {
|
|
33
|
+
const key = keyOrValue;
|
|
34
|
+
if (value === void 0) {
|
|
35
|
+
throw new Error('ExtensionError0.extend("method", key, value) requires value');
|
|
173
36
|
}
|
|
37
|
+
nextMethods[key] = value;
|
|
38
|
+
} else {
|
|
39
|
+
nextRefine.push(keyOrValue);
|
|
174
40
|
}
|
|
175
|
-
return
|
|
41
|
+
return new ExtensionError0({
|
|
42
|
+
props: nextProps,
|
|
43
|
+
methods: nextMethods,
|
|
44
|
+
refine: nextRefine
|
|
45
|
+
});
|
|
176
46
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
axiosError: void 0,
|
|
192
|
-
meta: {}
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
const message = "message" in error && typeof error.message === "string" ? error.message : void 0;
|
|
196
|
-
const clientMessage = "clientMessage" in error && typeof error.clientMessage === "string" ? error.clientMessage : defaults?.clientMessage || void 0;
|
|
197
|
-
const result = {
|
|
198
|
-
message,
|
|
199
|
-
code: "code" in error && typeof error.code === "string" ? error.code : defaults?.code || void 0,
|
|
200
|
-
clientMessage,
|
|
201
|
-
anyMessage: clientMessage || message || this.defaultMessage,
|
|
202
|
-
expected: void 0,
|
|
203
|
-
stack: "stack" in error && typeof error.stack === "string" ? error.stack : void 0,
|
|
204
|
-
tag: "tag" in error && typeof error.tag === "string" ? error.tag : defaults?.tag || void 0,
|
|
205
|
-
cause: "cause" in error ? error.cause : defaults?.cause || void 0,
|
|
206
|
-
meta: "meta" in error && typeof error.meta === "object" && error.meta !== null ? Meta0.getValue(error.meta) : Meta0.getValue(defaults?.meta) || {},
|
|
207
|
-
httpStatus: "httpStatus" in error && typeof error.httpStatus === "number" && error.httpStatus in HttpStatusCode ? error.httpStatus : typeof defaults?.httpStatus === "string" ? HttpStatusCode[defaults.httpStatus] : defaults?.httpStatus,
|
|
208
|
-
zodError: "zodError" in error && error.zodError instanceof ZodError ? error.zodError : error instanceof ZodError ? error : defaults?.zodError,
|
|
209
|
-
axiosError: "axiosError" in error && isAxiosError(error.axiosError) ? error.axiosError : isAxiosError(error) ? error : defaults?.axiosError
|
|
47
|
+
}
|
|
48
|
+
class Error0 extends Error {
|
|
49
|
+
static __extensionsMap;
|
|
50
|
+
static _extensions = [];
|
|
51
|
+
static _emptyExtension = {
|
|
52
|
+
props: {},
|
|
53
|
+
methods: {},
|
|
54
|
+
refine: []
|
|
55
|
+
};
|
|
56
|
+
static _getResolvedExtension() {
|
|
57
|
+
const resolved = {
|
|
58
|
+
props: {},
|
|
59
|
+
methods: {},
|
|
60
|
+
refine: []
|
|
210
61
|
};
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (result.zodError) {
|
|
216
|
-
this._assignError0Props(result, this._getExtraError0PropsByZodError(result.zodError));
|
|
62
|
+
for (const extension of this._extensions) {
|
|
63
|
+
Object.assign(resolved.props, extension.props ?? this._emptyExtension.props);
|
|
64
|
+
Object.assign(resolved.methods, extension.methods ?? this._emptyExtension.methods);
|
|
65
|
+
resolved.refine.push(...extension.refine ?? this._emptyExtension.refine);
|
|
217
66
|
}
|
|
218
|
-
|
|
219
|
-
this._assignError0Props(result, this._getExtraError0PropsByAxiosError(result.axiosError));
|
|
220
|
-
}
|
|
221
|
-
return result;
|
|
67
|
+
return resolved;
|
|
222
68
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
69
|
+
constructor(...args) {
|
|
70
|
+
const [first, second] = args;
|
|
71
|
+
const input = typeof first === "string" ? { message: first, ...second ?? {} } : first;
|
|
72
|
+
super(input.message, { cause: input.cause });
|
|
73
|
+
this.name = "Error0";
|
|
74
|
+
const ctor = this.constructor;
|
|
75
|
+
const extension = ctor._getResolvedExtension();
|
|
76
|
+
for (const [key, prop] of Object.entries(extension.props)) {
|
|
77
|
+
if (key in input) {
|
|
78
|
+
const ownValue = input[key];
|
|
79
|
+
this[key] = prop.init(ownValue);
|
|
80
|
+
} else {
|
|
81
|
+
Object.defineProperty(this, key, {
|
|
82
|
+
get: () => prop.resolve({ value: void 0, flow: this.flow(key), error: this }),
|
|
83
|
+
set: (value) => {
|
|
84
|
+
Object.defineProperty(this, key, {
|
|
85
|
+
value,
|
|
86
|
+
writable: true,
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true
|
|
93
|
+
});
|
|
94
|
+
}
|
|
234
95
|
}
|
|
235
|
-
return causesProps;
|
|
236
|
-
}
|
|
237
|
-
static _getCausesPropsFromError0Props(error0Props, maxLevel) {
|
|
238
|
-
return [error0Props, ...this._getCausesPropsFromUnknown(error0Props.cause, maxLevel - 1)];
|
|
239
96
|
}
|
|
240
|
-
static
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
97
|
+
static isSelfProperty = (object, key) => {
|
|
98
|
+
const d = Object.getOwnPropertyDescriptor(object, key);
|
|
99
|
+
if (!d) return false;
|
|
100
|
+
if (typeof d.get === "function" || typeof d.set === "function") {
|
|
101
|
+
if ("name" in object && object.name === "Error0") {
|
|
102
|
+
return false;
|
|
103
|
+
} else {
|
|
104
|
+
return true;
|
|
245
105
|
}
|
|
246
106
|
}
|
|
107
|
+
return true;
|
|
108
|
+
};
|
|
109
|
+
static own(error, key) {
|
|
110
|
+
if (this.isSelfProperty(error, key)) {
|
|
111
|
+
return error[key];
|
|
112
|
+
}
|
|
247
113
|
return void 0;
|
|
248
114
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
// ): NonNullable<TResult> | undefined {
|
|
253
|
-
// for (const causeProps of causesProps) {
|
|
254
|
-
// const result = getter(causeProps)
|
|
255
|
-
// if (isFilled(result)) {
|
|
256
|
-
// return result
|
|
257
|
-
// }
|
|
258
|
-
// }
|
|
259
|
-
// return undefined
|
|
260
|
-
// }
|
|
261
|
-
static _getFilledPropValues(causesProps, propKey) {
|
|
262
|
-
const values = [];
|
|
263
|
-
for (const causeProps of causesProps) {
|
|
264
|
-
const propValue = causeProps[propKey];
|
|
265
|
-
if (isFilled(propValue)) {
|
|
266
|
-
values.push(propValue);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
return values;
|
|
115
|
+
own(key) {
|
|
116
|
+
const ctor = this.constructor;
|
|
117
|
+
return ctor.own(this, key);
|
|
270
118
|
}
|
|
271
|
-
static
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
119
|
+
static flow(error, key) {
|
|
120
|
+
return this.causes(error, true).map((cause) => {
|
|
121
|
+
return this.own(cause, key);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
flow(key) {
|
|
125
|
+
const ctor = this.constructor;
|
|
126
|
+
return ctor.flow(this, key);
|
|
127
|
+
}
|
|
128
|
+
static causes(error, instancesOnly) {
|
|
129
|
+
const causes = [];
|
|
130
|
+
let current = error;
|
|
131
|
+
const maxDepth = 99;
|
|
132
|
+
const seen = /* @__PURE__ */ new Set();
|
|
133
|
+
for (let depth = 0; depth < maxDepth; depth += 1) {
|
|
134
|
+
if (seen.has(current)) {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
seen.add(current);
|
|
138
|
+
if (!instancesOnly || this.is(current)) {
|
|
139
|
+
causes.push(current);
|
|
140
|
+
}
|
|
141
|
+
if (!current || typeof current !== "object") {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
current = current.cause;
|
|
279
145
|
}
|
|
146
|
+
return causes;
|
|
280
147
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (
|
|
284
|
-
return
|
|
148
|
+
causes(instancesOnly) {
|
|
149
|
+
const ctor = this.constructor;
|
|
150
|
+
if (instancesOnly) {
|
|
151
|
+
return ctor.causes(this, true);
|
|
285
152
|
}
|
|
286
|
-
|
|
287
|
-
const removeAllLinesContains = (search) => {
|
|
288
|
-
lines = lines.filter((line) => !line.includes(search));
|
|
289
|
-
};
|
|
290
|
-
removeAllLinesContains("at new Error0");
|
|
291
|
-
removeAllLinesContains("at _toError0");
|
|
292
|
-
removeAllLinesContains("at Error0.from");
|
|
293
|
-
removeAllLinesContains("at Error0._toError0");
|
|
294
|
-
return lines.join("\n");
|
|
153
|
+
return ctor.causes(this);
|
|
295
154
|
}
|
|
296
|
-
static
|
|
297
|
-
return
|
|
155
|
+
static is(error) {
|
|
156
|
+
return error instanceof this;
|
|
298
157
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return error instanceof Error0;
|
|
158
|
+
static isSerialized(error) {
|
|
159
|
+
return !this.is(error) && typeof error === "object" && error !== null && "name" in error && error.name === "Error0";
|
|
302
160
|
}
|
|
303
|
-
static
|
|
304
|
-
if (error
|
|
305
|
-
return
|
|
161
|
+
static from(error) {
|
|
162
|
+
if (this.is(error)) {
|
|
163
|
+
return error;
|
|
306
164
|
}
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
return true;
|
|
310
|
-
}
|
|
165
|
+
if (this.isSerialized(error)) {
|
|
166
|
+
return this._fromSerialized(error);
|
|
311
167
|
}
|
|
312
|
-
return
|
|
168
|
+
return this._fromNonError0(error);
|
|
313
169
|
}
|
|
314
|
-
static
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
170
|
+
static _applyRefine(error) {
|
|
171
|
+
const extension = this._getResolvedExtension();
|
|
172
|
+
for (const refine of extension.refine) {
|
|
173
|
+
const refined = refine(error);
|
|
174
|
+
if (refined && typeof refined === "object") {
|
|
175
|
+
Object.assign(error, refined);
|
|
176
|
+
}
|
|
320
177
|
}
|
|
178
|
+
return error;
|
|
179
|
+
}
|
|
180
|
+
static _fromSerialized(error) {
|
|
181
|
+
const message = this._extractMessage(error);
|
|
321
182
|
if (typeof error !== "object" || error === null) {
|
|
322
|
-
return new
|
|
323
|
-
message: this.defaultMessage,
|
|
324
|
-
...inputOverride
|
|
325
|
-
});
|
|
183
|
+
return this._applyRefine(new this(message, { cause: error }));
|
|
326
184
|
}
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
185
|
+
const errorRecord = error;
|
|
186
|
+
const recreated = new this(message);
|
|
187
|
+
const extension = this._getResolvedExtension();
|
|
188
|
+
const propsEntries = Object.entries(extension.props);
|
|
189
|
+
for (const [key, prop] of propsEntries) {
|
|
190
|
+
if (!(key in errorRecord)) {
|
|
191
|
+
continue;
|
|
331
192
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return this._toError0(inputFromDataError0, inputOverride);
|
|
193
|
+
try {
|
|
194
|
+
const value = prop.deserialize({ value: errorRecord[key], serialized: errorRecord });
|
|
195
|
+
recreated[key] = value;
|
|
196
|
+
} catch {
|
|
337
197
|
}
|
|
338
198
|
}
|
|
339
|
-
|
|
199
|
+
const isStackInProps = propsEntries.some(([key]) => key === "stack");
|
|
200
|
+
if (typeof errorRecord.stack === "string" && !isStackInProps) {
|
|
201
|
+
recreated.stack = errorRecord.stack;
|
|
202
|
+
}
|
|
203
|
+
return recreated;
|
|
340
204
|
}
|
|
341
|
-
static
|
|
342
|
-
|
|
205
|
+
static _fromNonError0(error) {
|
|
206
|
+
const message = this._extractMessage(error);
|
|
207
|
+
return this._applyRefine(new this(message, { cause: error }));
|
|
343
208
|
}
|
|
344
|
-
static
|
|
345
|
-
|
|
346
|
-
return class Error0 extends parent {
|
|
347
|
-
static defaultMessage = props.defaultMessage ?? parent.defaultMessage;
|
|
348
|
-
static defaultCode = props.defaultCode ?? parent.defaultCode;
|
|
349
|
-
static defaultHttpStatus = props.defaultHttpStatus ?? parent.defaultHttpStatus;
|
|
350
|
-
static defaultExpected = props.defaultExpected ?? parent.defaultExpected;
|
|
351
|
-
static defaultClientMessage = props.defaultClientMessage ?? parent.defaultClientMessage;
|
|
352
|
-
static defaultMeta = Meta0.extend(props.defaultMeta, parent.defaultMeta);
|
|
353
|
-
};
|
|
209
|
+
static _extractMessage(error) {
|
|
210
|
+
return (typeof error === "string" ? error : typeof error === "object" && error !== null && "message" in error && typeof error.message === "string" ? error.message : void 0) || "Unknown error";
|
|
354
211
|
}
|
|
355
|
-
static
|
|
356
|
-
|
|
212
|
+
static _extendWithExtension(extension) {
|
|
213
|
+
const Base = this;
|
|
214
|
+
const Error0Extended = class Error0 extends Base {
|
|
215
|
+
};
|
|
216
|
+
Error0Extended._extensions = [...Base._extensions, extension];
|
|
217
|
+
const resolved = Error0Extended._getResolvedExtension();
|
|
218
|
+
for (const [key, method] of Object.entries(resolved.methods)) {
|
|
219
|
+
Object.defineProperty(Error0Extended.prototype, key, {
|
|
220
|
+
value: function(...args) {
|
|
221
|
+
return method(this, ...args);
|
|
222
|
+
},
|
|
223
|
+
writable: true,
|
|
224
|
+
enumerable: true,
|
|
225
|
+
configurable: true
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(Error0Extended, key, {
|
|
228
|
+
value: function(error, ...args) {
|
|
229
|
+
return method(this.from(error), ...args);
|
|
230
|
+
},
|
|
231
|
+
writable: true,
|
|
232
|
+
enumerable: true,
|
|
233
|
+
configurable: true
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return Error0Extended;
|
|
357
237
|
}
|
|
358
|
-
|
|
238
|
+
static _extensionFromBuilder(extension) {
|
|
239
|
+
const extensionRecord = extension;
|
|
359
240
|
return {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
httpStatus: this.httpStatus,
|
|
364
|
-
expected: this.expected,
|
|
365
|
-
clientMessage: this.clientMessage,
|
|
366
|
-
anyMessage: this.anyMessage,
|
|
367
|
-
cause: this.cause,
|
|
368
|
-
meta: Meta0.getValue(this.meta),
|
|
369
|
-
stack: this.stack,
|
|
370
|
-
__I_AM_ERROR_0: this.__I_AM_ERROR_0
|
|
241
|
+
props: { ...extensionRecord._extension.props ?? {} },
|
|
242
|
+
methods: { ...extensionRecord._extension.methods ?? {} },
|
|
243
|
+
refine: [...extensionRecord._extension.refine ?? []]
|
|
371
244
|
};
|
|
372
245
|
}
|
|
373
|
-
static
|
|
374
|
-
|
|
375
|
-
|
|
246
|
+
static prop(key, value) {
|
|
247
|
+
return this.extend("prop", key, value);
|
|
248
|
+
}
|
|
249
|
+
static method(key, value) {
|
|
250
|
+
return this.extend("method", key, value);
|
|
376
251
|
}
|
|
377
|
-
|
|
378
|
-
return
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
252
|
+
static refine(value) {
|
|
253
|
+
return this.extend("refine", value);
|
|
254
|
+
}
|
|
255
|
+
static extend(first, key, value) {
|
|
256
|
+
if (first instanceof ExtensionError0) {
|
|
257
|
+
return this._extendWithExtension(this._extensionFromBuilder(first));
|
|
258
|
+
}
|
|
259
|
+
if (first === "refine") {
|
|
260
|
+
if (typeof key !== "function") {
|
|
261
|
+
throw new Error('Error0.extend("refine", value) requires refine function');
|
|
386
262
|
}
|
|
387
|
-
|
|
263
|
+
return this._extendWithExtension({
|
|
264
|
+
refine: [key]
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
if (typeof key !== "string" || value === void 0) {
|
|
268
|
+
throw new Error("Error0.extend(kind, key, value) requires key and value");
|
|
269
|
+
}
|
|
270
|
+
if (first === "prop") {
|
|
271
|
+
return this._extendWithExtension({
|
|
272
|
+
props: { [key]: value }
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return this._extendWithExtension({
|
|
276
|
+
methods: { [key]: value }
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
static extension() {
|
|
280
|
+
return new ExtensionError0();
|
|
281
|
+
}
|
|
282
|
+
static serialize(error, isPublic = true) {
|
|
283
|
+
const error0 = this.from(error);
|
|
284
|
+
const json = {
|
|
285
|
+
name: error0.name,
|
|
286
|
+
message: error0.message
|
|
287
|
+
// we do not serialize causes, it is enough that we have floated props and refine helper
|
|
288
|
+
// cause: error0.cause,
|
|
289
|
+
};
|
|
290
|
+
const extension = this._getResolvedExtension();
|
|
291
|
+
const propsEntries = Object.entries(extension.props);
|
|
292
|
+
for (const [key, prop] of propsEntries) {
|
|
293
|
+
try {
|
|
294
|
+
const value = prop.resolve({ value: error0.own(key), flow: error0.flow(key), error: error0 });
|
|
295
|
+
const jsonValue = prop.serialize({ value, error: error0, isPublic });
|
|
296
|
+
if (jsonValue !== void 0) {
|
|
297
|
+
json[key] = jsonValue;
|
|
298
|
+
}
|
|
299
|
+
} catch {
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const isStackInProps = propsEntries.some(([key]) => key === "stack");
|
|
303
|
+
if (!isStackInProps && typeof error0.stack === "string") {
|
|
304
|
+
json.stack = error0.stack;
|
|
305
|
+
}
|
|
306
|
+
return Object.fromEntries(Object.entries(json).filter(([, value]) => value !== void 0));
|
|
307
|
+
}
|
|
308
|
+
serialize(isPublic = true) {
|
|
309
|
+
const ctor = this.constructor;
|
|
310
|
+
return ctor.serialize(this, isPublic);
|
|
388
311
|
}
|
|
389
312
|
}
|
|
390
|
-
const e0s = {
|
|
391
|
-
Default: Error0,
|
|
392
|
-
Expected: Error0.extend({
|
|
393
|
-
defaultExpected: true
|
|
394
|
-
})
|
|
395
|
-
};
|
|
396
313
|
export {
|
|
397
314
|
Error0,
|
|
398
|
-
|
|
315
|
+
ExtensionError0
|
|
399
316
|
};
|
|
400
317
|
//# sourceMappingURL=index.js.map
|