@devp0nt/error0 1.0.0-next.37 → 1.0.0-next.39

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