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