@devp0nt/error0 1.0.0-next.52 → 1.0.0-next.54
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 +109 -105
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +44 -85
- package/dist/cjs/plugins/code.d.cts +40 -2
- package/dist/cjs/plugins/expected.d.cts +53 -11
- package/dist/cjs/plugins/meta.d.cts +40 -2
- package/dist/cjs/plugins/stack-merge.cjs +3 -3
- package/dist/cjs/plugins/stack-merge.cjs.map +1 -1
- package/dist/cjs/plugins/status.d.cts +40 -2
- package/dist/cjs/plugins/tags.cjs +13 -14
- package/dist/cjs/plugins/tags.cjs.map +1 -1
- package/dist/cjs/plugins/tags.d.cts +73 -5
- package/dist/esm/index.d.ts +44 -85
- package/dist/esm/index.js +109 -105
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugins/code.d.ts +40 -2
- package/dist/esm/plugins/expected.d.ts +53 -11
- package/dist/esm/plugins/meta.d.ts +40 -2
- package/dist/esm/plugins/stack-merge.js +3 -3
- package/dist/esm/plugins/stack-merge.js.map +1 -1
- package/dist/esm/plugins/status.d.ts +40 -2
- package/dist/esm/plugins/tags.d.ts +73 -5
- package/dist/esm/plugins/tags.js +13 -14
- package/dist/esm/plugins/tags.js.map +1 -1
- package/package.json +4 -2
- package/src/index.test.ts +187 -5
- package/src/index.ts +327 -311
- package/src/plugins/stack-merge.ts +2 -3
- package/src/plugins/tags.test.ts +1 -3
- package/src/plugins/tags.ts +28 -16
package/src/index.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
// Проверить как в браузере такая ошибка выводится
|
|
2
|
+
|
|
3
|
+
// ? стек плагин переименовать в просто стек плагин и там добавить опцию для мержа и не мержа, чтобы можно было изПаблик там легко определять
|
|
4
|
+
// ? В эрор0 добавить тоже срезку тчк сервер после которой для клиента всё обрежется, потому резолв всегда может быть андефайнед
|
|
5
|
+
// ? В эррор0 добавить вайт/бан плагин
|
|
6
|
+
|
|
1
7
|
type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false
|
|
2
8
|
type NormalizeUnknownToUndefined<T> = IsUnknown<T> extends true ? undefined : T
|
|
3
9
|
type IsOnlyUndefined<T> = [Exclude<T, undefined>] extends [never] ? true : false
|
|
@@ -54,7 +60,7 @@ type ErrorPluginPropOptionsWithoutInit<
|
|
|
54
60
|
> = ErrorPluginPropOptionsBase<TOutputValue, TError, TResolveValue> & {
|
|
55
61
|
init?: undefined
|
|
56
62
|
}
|
|
57
|
-
|
|
63
|
+
type ErrorPluginPropOptions<
|
|
58
64
|
TInputValue = undefined,
|
|
59
65
|
TOutputValue = unknown,
|
|
60
66
|
TError extends Error0 = Error0,
|
|
@@ -62,49 +68,83 @@ export type ErrorPluginPropOptions<
|
|
|
62
68
|
> =
|
|
63
69
|
| ErrorPluginPropOptionsWithInit<TInputValue, TOutputValue, TError, TResolveValue>
|
|
64
70
|
| ErrorPluginPropOptionsWithoutInit<TOutputValue, TError, TResolveValue>
|
|
65
|
-
|
|
71
|
+
|
|
72
|
+
type ErrorPluginPropOptionsBaseDefinition<
|
|
73
|
+
TOutputValue,
|
|
74
|
+
TError extends Error0,
|
|
75
|
+
TResolveValue extends TOutputValue | undefined,
|
|
76
|
+
> = {
|
|
77
|
+
resolve?: ((options: ErrorPluginPropOptionsResolveOptions<TOutputValue, TError>) => TResolveValue) | boolean
|
|
78
|
+
serialize?: ErrorPluginPropSerialize<TOutputValue, TError, TResolveValue>
|
|
79
|
+
deserialize?: ErrorPluginPropDeserialize<TOutputValue>
|
|
80
|
+
}
|
|
81
|
+
type ErrorPluginPropOptionsWithInitDefinition<
|
|
82
|
+
TInputValue,
|
|
83
|
+
TOutputValue,
|
|
84
|
+
TError extends Error0,
|
|
85
|
+
TResolveValue extends TOutputValue | undefined,
|
|
86
|
+
> = ErrorPluginPropOptionsBaseDefinition<TOutputValue, TError, TResolveValue> & {
|
|
87
|
+
init: ErrorPluginPropInit<TInputValue, TOutputValue>
|
|
88
|
+
}
|
|
89
|
+
type ErrorPluginPropOptionsWithoutInitDefinition<
|
|
90
|
+
TOutputValue,
|
|
91
|
+
TError extends Error0,
|
|
92
|
+
TResolveValue extends TOutputValue | undefined,
|
|
93
|
+
> = ErrorPluginPropOptionsBaseDefinition<TOutputValue, TError, TResolveValue> & {
|
|
94
|
+
init?: undefined
|
|
95
|
+
}
|
|
96
|
+
type ErrorPluginPropOptionsDefinition<
|
|
97
|
+
TInputValue = undefined,
|
|
98
|
+
TOutputValue = unknown,
|
|
99
|
+
TError extends Error0 = Error0,
|
|
100
|
+
TResolveValue extends TOutputValue | undefined = TOutputValue | undefined,
|
|
101
|
+
> =
|
|
102
|
+
| ErrorPluginPropOptionsWithInitDefinition<TInputValue, TOutputValue, TError, TResolveValue>
|
|
103
|
+
| ErrorPluginPropOptionsWithoutInitDefinition<TOutputValue, TError, TResolveValue>
|
|
104
|
+
|
|
105
|
+
type ErrorPluginMethodFn<TOutputValue, TArgs extends unknown[] = unknown[], TError extends Error0 = Error0> = (
|
|
66
106
|
error: TError,
|
|
67
107
|
...args: TArgs
|
|
68
108
|
) => TOutputValue
|
|
69
109
|
type ErrorPluginAnyMethodFn = (error: any, ...args: any[]) => any
|
|
70
|
-
|
|
71
|
-
|
|
110
|
+
type ErrorPluginAdaptResult<TOutputProps extends Record<string, unknown>> = Partial<TOutputProps> | undefined
|
|
111
|
+
type ErrorPluginAdaptFn<
|
|
72
112
|
TError extends Error0 = Error0,
|
|
73
113
|
TOutputProps extends Record<string, unknown> = Record<never, never>,
|
|
74
114
|
> = ((error: TError) => void) | ((error: TError) => ErrorPluginAdaptResult<TOutputProps>)
|
|
75
|
-
|
|
115
|
+
type ErrorPluginStackSerialize<TError extends Error0> = (options: {
|
|
76
116
|
value: string | undefined
|
|
77
117
|
error: TError
|
|
78
118
|
isPublic: boolean
|
|
79
119
|
}) => unknown
|
|
80
|
-
|
|
81
|
-
|
|
120
|
+
type ErrorPluginStack<TError extends Error0 = Error0> = { serialize: ErrorPluginStackSerialize<TError> }
|
|
121
|
+
type ErrorPluginCauseSerialize<TError extends Error0> = (options: {
|
|
82
122
|
cause: unknown
|
|
83
123
|
error: TError
|
|
84
124
|
isPublic: boolean
|
|
85
125
|
is: (cause: unknown) => boolean
|
|
86
126
|
serialize: (cause: unknown) => Record<string, unknown>
|
|
87
127
|
}) => unknown
|
|
88
|
-
|
|
128
|
+
type ErrorPluginCauseDeserialize = (options: {
|
|
89
129
|
cause: unknown
|
|
90
130
|
error: Record<string, unknown>
|
|
91
131
|
isSerialized: (serializedCause: unknown) => boolean
|
|
92
132
|
fromSerialized: (serializedCause: unknown) => Error0
|
|
93
133
|
}) => unknown
|
|
94
|
-
|
|
134
|
+
type ErrorPluginCause<TError extends Error0 = Error0> = {
|
|
95
135
|
serialize: ErrorPluginCauseSerialize<TError>
|
|
96
136
|
deserialize: ErrorPluginCauseDeserialize
|
|
97
137
|
}
|
|
98
|
-
|
|
138
|
+
type ErrorPluginMessageSerialize<TError extends Error0> = (options: {
|
|
99
139
|
value: string
|
|
100
140
|
error: TError
|
|
101
141
|
isPublic: boolean
|
|
102
142
|
}) => unknown
|
|
103
|
-
|
|
143
|
+
type ErrorPluginMessage<TError extends Error0 = Error0> = { serialize: ErrorPluginMessageSerialize<TError> }
|
|
104
144
|
type ErrorMethodRecord = { fn: ErrorPluginAnyMethodFn }
|
|
105
145
|
|
|
106
|
-
|
|
107
|
-
|
|
146
|
+
type ErrorPluginProps = { [key: string]: ErrorPluginPropOptions<any, any> }
|
|
147
|
+
type ErrorPluginMethods = { [key: string]: ErrorPluginAnyMethodFn }
|
|
108
148
|
|
|
109
149
|
export type ErrorPlugin<
|
|
110
150
|
TProps extends ErrorPluginProps = Record<never, never>,
|
|
@@ -134,12 +174,12 @@ type PluginOutputProps<TProps extends ErrorPluginProps> = {
|
|
|
134
174
|
? TResolveValue
|
|
135
175
|
: never
|
|
136
176
|
}
|
|
137
|
-
|
|
177
|
+
type ErrorPluginsMap = {
|
|
138
178
|
props: Record<string, { init: unknown; output: unknown; resolve: unknown }>
|
|
139
179
|
methods: Record<string, ErrorMethodRecord>
|
|
140
180
|
}
|
|
141
|
-
|
|
142
|
-
|
|
181
|
+
type IsEmptyObject<T> = keyof T extends never ? true : false
|
|
182
|
+
type ErrorInputBase = {
|
|
143
183
|
cause?: unknown
|
|
144
184
|
}
|
|
145
185
|
type ErrorInputPluginProps<TPluginsMap extends ErrorPluginsMap> = {
|
|
@@ -147,7 +187,7 @@ type ErrorInputPluginProps<TPluginsMap extends ErrorPluginsMap> = {
|
|
|
147
187
|
? never
|
|
148
188
|
: TKey]?: TPluginsMap['props'][TKey]['init']
|
|
149
189
|
}
|
|
150
|
-
|
|
190
|
+
type ErrorInput<TPluginsMap extends ErrorPluginsMap> =
|
|
151
191
|
IsEmptyObject<TPluginsMap['props']> extends true
|
|
152
192
|
? ErrorInputBase
|
|
153
193
|
: ErrorInputBase & ErrorInputPluginProps<TPluginsMap>
|
|
@@ -158,87 +198,95 @@ type ErrorResolvedProps<TPluginsMap extends ErrorPluginsMap> = {
|
|
|
158
198
|
type ErrorOwnProps<TPluginsMap extends ErrorPluginsMap> = {
|
|
159
199
|
[TKey in keyof TPluginsMap['props']]: TPluginsMap['props'][TKey]['output'] | undefined
|
|
160
200
|
}
|
|
161
|
-
type ErrorOwnMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
162
|
-
own: {
|
|
163
|
-
(): ErrorOwnProps<TPluginsMap>
|
|
164
|
-
<TKey extends keyof TPluginsMap['props'] & string>(key: TKey): ErrorOwnProps<TPluginsMap>[TKey]
|
|
165
|
-
}
|
|
166
|
-
flow: <TKey extends keyof TPluginsMap['props'] & string>(key: TKey) => Array<ErrorOwnProps<TPluginsMap>[TKey]>
|
|
167
|
-
}
|
|
168
201
|
type ErrorResolveMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
169
202
|
resolve: () => ErrorResolvedProps<TPluginsMap>
|
|
170
203
|
}
|
|
171
|
-
type
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
(...args: TArgs1): TOutput1
|
|
179
|
-
(...args: TArgs2): TOutput2
|
|
180
|
-
(...args: TArgs3): TOutput3
|
|
181
|
-
(...args: TArgs4): TOutput4
|
|
182
|
-
}
|
|
183
|
-
: TMethod extends {
|
|
184
|
-
(error: any, ...args: infer TArgs1): infer TOutput1
|
|
185
|
-
(error: any, ...args: infer TArgs2): infer TOutput2
|
|
186
|
-
(error: any, ...args: infer TArgs3): infer TOutput3
|
|
187
|
-
}
|
|
188
|
-
? {
|
|
189
|
-
(...args: TArgs1): TOutput1
|
|
190
|
-
(...args: TArgs2): TOutput2
|
|
191
|
-
(...args: TArgs3): TOutput3
|
|
192
|
-
}
|
|
193
|
-
: TMethod extends {
|
|
194
|
-
(error: any, ...args: infer TArgs1): infer TOutput1
|
|
195
|
-
(error: any, ...args: infer TArgs2): infer TOutput2
|
|
196
|
-
}
|
|
197
|
-
? {
|
|
198
|
-
(...args: TArgs1): TOutput1
|
|
199
|
-
(...args: TArgs2): TOutput2
|
|
200
|
-
}
|
|
201
|
-
: TMethod extends (error: any, ...args: infer TArgs) => infer TOutput
|
|
202
|
-
? (...args: TArgs) => TOutput
|
|
203
|
-
: never
|
|
204
|
-
type BindStaticMethod<TMethod> = TMethod extends {
|
|
205
|
-
(error: any, ...args: infer TArgs1): infer TOutput1
|
|
206
|
-
(error: any, ...args: infer TArgs2): infer TOutput2
|
|
207
|
-
(error: any, ...args: infer TArgs3): infer TOutput3
|
|
208
|
-
(error: any, ...args: infer TArgs4): infer TOutput4
|
|
204
|
+
type Error0ResolvedInstance<TPluginsMap extends ErrorPluginsMap> = Error0 &
|
|
205
|
+
ErrorResolved<TPluginsMap> &
|
|
206
|
+
ErrorResolveMethods<TPluginsMap>
|
|
207
|
+
type ErrorOwnMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
208
|
+
own?: Partial<ErrorOwnProps<TPluginsMap>>
|
|
209
|
+
flow: <TKey extends keyof TPluginsMap['props'] & string>(key: TKey) => Array<ErrorOwnProps<TPluginsMap>[TKey]>
|
|
210
|
+
assign: (props: Partial<ErrorOwnProps<TPluginsMap>>) => Error0ResolvedInstance<TPluginsMap>
|
|
209
211
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
212
|
+
export type InstanceError0<TPluginsMap extends ErrorPluginsMap> = Error0ResolvedInstance<TPluginsMap> &
|
|
213
|
+
ErrorOwnMethods<TPluginsMap> & { readonly __pluginsMap?: TPluginsMap }
|
|
214
|
+
// type BindInstanceMethod<TMethod> = TMethod extends {
|
|
215
|
+
// (error: any, ...args: infer TArgs1): infer TOutput1
|
|
216
|
+
// (error: any, ...args: infer TArgs2): infer TOutput2
|
|
217
|
+
// (error: any, ...args: infer TArgs3): infer TOutput3
|
|
218
|
+
// (error: any, ...args: infer TArgs4): infer TOutput4
|
|
219
|
+
// }
|
|
220
|
+
// ? {
|
|
221
|
+
// (...args: TArgs1): TOutput1
|
|
222
|
+
// (...args: TArgs2): TOutput2
|
|
223
|
+
// (...args: TArgs3): TOutput3
|
|
224
|
+
// (...args: TArgs4): TOutput4
|
|
225
|
+
// }
|
|
226
|
+
// : TMethod extends {
|
|
227
|
+
// (error: any, ...args: infer TArgs1): infer TOutput1
|
|
228
|
+
// (error: any, ...args: infer TArgs2): infer TOutput2
|
|
229
|
+
// (error: any, ...args: infer TArgs3): infer TOutput3
|
|
230
|
+
// }
|
|
231
|
+
// ? {
|
|
232
|
+
// (...args: TArgs1): TOutput1
|
|
233
|
+
// (...args: TArgs2): TOutput2
|
|
234
|
+
// (...args: TArgs3): TOutput3
|
|
235
|
+
// }
|
|
236
|
+
// : TMethod extends {
|
|
237
|
+
// (error: any, ...args: infer TArgs1): infer TOutput1
|
|
238
|
+
// (error: any, ...args: infer TArgs2): infer TOutput2
|
|
239
|
+
// }
|
|
240
|
+
// ? {
|
|
241
|
+
// (...args: TArgs1): TOutput1
|
|
242
|
+
// (...args: TArgs2): TOutput2
|
|
243
|
+
// }
|
|
244
|
+
// : TMethod extends (error: any, ...args: infer TArgs) => infer TOutput
|
|
245
|
+
// ? (...args: TArgs) => TOutput
|
|
246
|
+
// : never
|
|
247
|
+
// type BindStaticMethod<TMethod> = TMethod extends {
|
|
248
|
+
// (error: any, ...args: infer TArgs1): infer TOutput1
|
|
249
|
+
// (error: any, ...args: infer TArgs2): infer TOutput2
|
|
250
|
+
// (error: any, ...args: infer TArgs3): infer TOutput3
|
|
251
|
+
// (error: any, ...args: infer TArgs4): infer TOutput4
|
|
252
|
+
// }
|
|
253
|
+
// ? {
|
|
254
|
+
// (error: unknown, ...args: TArgs1): TOutput1
|
|
255
|
+
// (error: unknown, ...args: TArgs2): TOutput2
|
|
256
|
+
// (error: unknown, ...args: TArgs3): TOutput3
|
|
257
|
+
// (error: unknown, ...args: TArgs4): TOutput4
|
|
258
|
+
// }
|
|
259
|
+
// : TMethod extends {
|
|
260
|
+
// (error: any, ...args: infer TArgs1): infer TOutput1
|
|
261
|
+
// (error: any, ...args: infer TArgs2): infer TOutput2
|
|
262
|
+
// (error: any, ...args: infer TArgs3): infer TOutput3
|
|
263
|
+
// }
|
|
264
|
+
// ? {
|
|
265
|
+
// (error: unknown, ...args: TArgs1): TOutput1
|
|
266
|
+
// (error: unknown, ...args: TArgs2): TOutput2
|
|
267
|
+
// (error: unknown, ...args: TArgs3): TOutput3
|
|
268
|
+
// }
|
|
269
|
+
// : TMethod extends {
|
|
270
|
+
// (error: any, ...args: infer TArgs1): infer TOutput1
|
|
271
|
+
// (error: any, ...args: infer TArgs2): infer TOutput2
|
|
272
|
+
// }
|
|
273
|
+
// ? {
|
|
274
|
+
// (error: unknown, ...args: TArgs1): TOutput1
|
|
275
|
+
// (error: unknown, ...args: TArgs2): TOutput2
|
|
276
|
+
// }
|
|
277
|
+
// : TMethod extends (error: any, ...args: infer TArgs) => infer TOutput
|
|
278
|
+
// ? (error: unknown, ...args: TArgs) => TOutput
|
|
279
|
+
// : never
|
|
280
|
+
type BindInstanceMethod<TMethod> = TMethod extends (error: any, ...args: infer TArgs) => infer TOutput
|
|
281
|
+
? (...args: TArgs) => TOutput
|
|
282
|
+
: never
|
|
283
|
+
type BindStaticMethod<TMethod> = TMethod extends (error: any, ...args: infer TArgs) => infer TOutput
|
|
284
|
+
? (error: unknown, ...args: TArgs) => TOutput
|
|
285
|
+
: never
|
|
237
286
|
type ErrorMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
238
287
|
[TKey in keyof TPluginsMap['methods']]: BindInstanceMethod<TPluginsMap['methods'][TKey]['fn']>
|
|
239
288
|
}
|
|
240
|
-
|
|
241
|
-
ErrorMethods<TPluginsMap>
|
|
289
|
+
type ErrorResolved<TPluginsMap extends ErrorPluginsMap> = ErrorResolvedProps<TPluginsMap> & ErrorMethods<TPluginsMap>
|
|
242
290
|
|
|
243
291
|
type ErrorStaticMethods<TPluginsMap extends ErrorPluginsMap> = {
|
|
244
292
|
[TKey in keyof TPluginsMap['methods']]: BindStaticMethod<TPluginsMap['methods'][TKey]['fn']>
|
|
@@ -263,6 +311,29 @@ type ErrorPluginResolved = {
|
|
|
263
311
|
const RESERVED_STACK_PROP_ERROR = 'Error0: "stack" is a reserved prop key. Use .stack(...) plugin API instead'
|
|
264
312
|
const RESERVED_MESSAGE_PROP_ERROR = 'Error0: "message" is a reserved prop key. Use .message(...) plugin API instead'
|
|
265
313
|
|
|
314
|
+
const fromPropOptionsDefinition = (
|
|
315
|
+
options: ErrorPluginPropOptionsDefinition<any, any, any, any>,
|
|
316
|
+
): ErrorPluginPropOptions<any, any, any, any> => {
|
|
317
|
+
let resolver: ErrorPluginPropOptions<unknown>['resolve']
|
|
318
|
+
if (!options.resolve) {
|
|
319
|
+
resolver = (options: ErrorPluginPropOptionsResolveOptions<any, any>) => options.own
|
|
320
|
+
} else if (options.resolve === true) {
|
|
321
|
+
resolver = (options: ErrorPluginPropOptionsResolveOptions<any, any>) => options.flow.find((v) => v !== undefined)
|
|
322
|
+
} else if (typeof options.resolve === 'function') {
|
|
323
|
+
resolver = options.resolve
|
|
324
|
+
} else {
|
|
325
|
+
throw new Error('Invalid resolve option')
|
|
326
|
+
}
|
|
327
|
+
const serializer: ErrorPluginPropOptions<unknown>['serialize'] = options.serialize ?? false
|
|
328
|
+
const deserializer: ErrorPluginPropOptions<unknown>['deserialize'] = options.deserialize ?? false
|
|
329
|
+
return {
|
|
330
|
+
...options,
|
|
331
|
+
resolve: resolver,
|
|
332
|
+
serialize: serializer,
|
|
333
|
+
deserialize: deserializer,
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
266
337
|
type PluginPropsMapOf<TPlugin extends ErrorPlugin> = {
|
|
267
338
|
[TKey in keyof NonNullable<TPlugin['props']>]: NonNullable<TPlugin['props']>[TKey] extends ErrorPluginPropOptions<
|
|
268
339
|
any,
|
|
@@ -323,16 +394,10 @@ type PluginsMapFromParts<
|
|
|
323
394
|
TProps extends ErrorPluginProps,
|
|
324
395
|
TMethods extends ErrorPluginMethods,
|
|
325
396
|
> = ErrorPluginsMapOfPlugin<ErrorPlugin<TProps, TMethods>>
|
|
326
|
-
type ErrorInstanceOfMap<TMap extends ErrorPluginsMap> =
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
type BuilderError0<TProps extends ErrorPluginProps, TMethods extends ErrorPluginMethods> = Error0 &
|
|
331
|
-
ErrorResolved<PluginsMapFromParts<TProps, TMethods>> &
|
|
332
|
-
ErrorOwnMethods<PluginsMapFromParts<TProps, TMethods>> &
|
|
333
|
-
ErrorResolveMethods<PluginsMapFromParts<TProps, TMethods>> & {
|
|
334
|
-
readonly __pluginsMap?: PluginsMapFromParts<TProps, TMethods>
|
|
335
|
-
}
|
|
397
|
+
type ErrorInstanceOfMap<TMap extends ErrorPluginsMap> = InstanceError0<TMap>
|
|
398
|
+
type BuilderError0<TProps extends ErrorPluginProps, TMethods extends ErrorPluginMethods> = InstanceError0<
|
|
399
|
+
PluginsMapFromParts<TProps, TMethods>
|
|
400
|
+
>
|
|
336
401
|
|
|
337
402
|
type PluginOfBuilder<TBuilder> =
|
|
338
403
|
TBuilder extends PluginError0<infer TProps, infer TMethods> ? ErrorPlugin<TProps, TMethods> : never
|
|
@@ -366,7 +431,7 @@ export class PluginError0<
|
|
|
366
431
|
TResolveValue extends TOutputValue | undefined = TOutputValue | undefined,
|
|
367
432
|
>(
|
|
368
433
|
key: TKey,
|
|
369
|
-
value:
|
|
434
|
+
value: ErrorPluginPropOptionsDefinition<TInputValue, TOutputValue, BuilderError0<TProps, TMethods>, TResolveValue>,
|
|
370
435
|
): PluginError0<AddPropToPluginProps<TProps, TKey, TInputValue, TOutputValue, TResolveValue>, TMethods> {
|
|
371
436
|
return this.use('prop', key, value)
|
|
372
437
|
}
|
|
@@ -404,7 +469,7 @@ export class PluginError0<
|
|
|
404
469
|
>(
|
|
405
470
|
kind: 'prop',
|
|
406
471
|
key: TKey,
|
|
407
|
-
value:
|
|
472
|
+
value: ErrorPluginPropOptionsDefinition<TInputValue, TOutputValue, BuilderError0<TProps, TMethods>, TResolveValue>,
|
|
408
473
|
): PluginError0<AddPropToPluginProps<TProps, TKey, TInputValue, TOutputValue, TResolveValue>, TMethods>
|
|
409
474
|
use<TKey extends string, TMethod extends (error: BuilderError0<TProps, TMethods>, ...args: any[]) => any>(
|
|
410
475
|
kind: 'method',
|
|
@@ -421,7 +486,7 @@ export class PluginError0<
|
|
|
421
486
|
use(
|
|
422
487
|
kind: 'prop' | 'method' | 'adapt' | 'stack' | 'cause' | 'message',
|
|
423
488
|
keyOrValue: unknown,
|
|
424
|
-
value?:
|
|
489
|
+
value?: ErrorPluginPropOptionsDefinition<unknown, unknown, any> | ErrorPluginMethodFn<unknown, unknown[], any>,
|
|
425
490
|
): PluginError0<any, any> {
|
|
426
491
|
const nextProps: ErrorPluginProps = { ...(this._plugin.props ?? {}) }
|
|
427
492
|
const nextMethods: ErrorPluginMethods = { ...(this._plugin.methods ?? {}) }
|
|
@@ -440,7 +505,7 @@ export class PluginError0<
|
|
|
440
505
|
if (value === undefined) {
|
|
441
506
|
throw new Error('PluginError0.use("prop", key, value) requires value')
|
|
442
507
|
}
|
|
443
|
-
nextProps[key] = value as
|
|
508
|
+
nextProps[key] = fromPropOptionsDefinition(value as ErrorPluginPropOptionsDefinition<any, any>)
|
|
444
509
|
} else if (kind === 'method') {
|
|
445
510
|
const key = keyOrValue as string
|
|
446
511
|
if (value === undefined) {
|
|
@@ -467,41 +532,27 @@ export class PluginError0<
|
|
|
467
532
|
}
|
|
468
533
|
}
|
|
469
534
|
|
|
470
|
-
const OWN_SYMBOL: unique symbol = Symbol('Error0.own')
|
|
471
535
|
type ErrorOwnStore = Record<string, unknown>
|
|
472
536
|
|
|
473
537
|
export type ClassError0<TPluginsMap extends ErrorPluginsMap = EmptyPluginsMap> = {
|
|
474
538
|
MAX_CAUSES_DEPTH: number
|
|
475
|
-
new (
|
|
476
|
-
|
|
477
|
-
input?: ErrorInput<TPluginsMap>,
|
|
478
|
-
): Error0 &
|
|
479
|
-
ErrorResolved<TPluginsMap> &
|
|
480
|
-
ErrorOwnMethods<TPluginsMap> &
|
|
481
|
-
ErrorResolveMethods<TPluginsMap> & { readonly __pluginsMap?: TPluginsMap }
|
|
482
|
-
new (
|
|
483
|
-
input: { message: string } & ErrorInput<TPluginsMap>,
|
|
484
|
-
): Error0 &
|
|
485
|
-
ErrorResolved<TPluginsMap> &
|
|
486
|
-
ErrorOwnMethods<TPluginsMap> &
|
|
487
|
-
ErrorResolveMethods<TPluginsMap> & { readonly __pluginsMap?: TPluginsMap }
|
|
539
|
+
new (message: string, input?: ErrorInput<TPluginsMap>): InstanceError0<TPluginsMap>
|
|
540
|
+
new (input: { message: string } & ErrorInput<TPluginsMap>): InstanceError0<TPluginsMap>
|
|
488
541
|
readonly __pluginsMap?: TPluginsMap
|
|
489
|
-
from: (
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
round: (
|
|
493
|
-
error: unknown,
|
|
494
|
-
isPublic?: boolean,
|
|
495
|
-
) => Error0 & ErrorResolved<TPluginsMap> & ErrorOwnMethods<TPluginsMap> & ErrorResolveMethods<TPluginsMap>
|
|
542
|
+
from: <TThis extends ClassError0<any>>(this: TThis, error: unknown) => InstanceType<TThis>
|
|
543
|
+
round: <TThis extends ClassError0<any>>(this: TThis, error: unknown, isPublic?: boolean) => InstanceType<TThis>
|
|
544
|
+
// flat: <TThis extends ClassError0<any>>(this: TThis, error: unknown, keepCauses?: boolean) => InstanceType<TThis>
|
|
496
545
|
causes: {
|
|
497
546
|
(error: unknown, instancesOnly?: false): unknown[]
|
|
498
|
-
(
|
|
499
|
-
error: unknown,
|
|
500
|
-
instancesOnly: true,
|
|
501
|
-
): Array<Error0 & ErrorResolved<TPluginsMap> & ErrorOwnMethods<TPluginsMap> & ErrorResolveMethods<TPluginsMap>>
|
|
547
|
+
(error: unknown, instancesOnly: true): Array<InstanceError0<TPluginsMap>>
|
|
502
548
|
}
|
|
503
549
|
resolve: (error: unknown) => ErrorResolvedProps<TPluginsMap>
|
|
504
550
|
serialize: (error: unknown, isPublic?: boolean) => Record<string, unknown>
|
|
551
|
+
assign: <TThis extends ClassError0<any>>(
|
|
552
|
+
this: TThis,
|
|
553
|
+
error: unknown,
|
|
554
|
+
props: Partial<ErrorOwnProps<PluginsMapOf<TThis>>>,
|
|
555
|
+
) => InstanceError0<PluginsMapOf<TThis>>
|
|
505
556
|
own: {
|
|
506
557
|
(error: object): ErrorOwnProps<TPluginsMap>
|
|
507
558
|
<TKey extends keyof TPluginsMap['props'] & string>(error: object, key: TKey): ErrorOwnProps<TPluginsMap>[TKey]
|
|
@@ -510,24 +561,6 @@ export type ClassError0<TPluginsMap extends ErrorPluginsMap = EmptyPluginsMap> =
|
|
|
510
561
|
error: object,
|
|
511
562
|
key: TKey,
|
|
512
563
|
) => Array<ErrorOwnProps<TPluginsMap>[TKey]>
|
|
513
|
-
// prop: <
|
|
514
|
-
// TKey extends string,
|
|
515
|
-
// TInputValue = undefined,
|
|
516
|
-
// TOutputValue = unknown,
|
|
517
|
-
// TResolveValue extends TOutputValue | undefined = TOutputValue | undefined,
|
|
518
|
-
// >(
|
|
519
|
-
// key: TKey,
|
|
520
|
-
// value: ErrorPluginPropOptions<TInputValue, TOutputValue, ErrorInstanceOfMap<TPluginsMap>, TResolveValue>,
|
|
521
|
-
// ) => ClassError0<ExtendErrorPluginsMapWithProp<TPluginsMap, TKey, TInputValue, TOutputValue, TResolveValue>>
|
|
522
|
-
// method: <TKey extends string, TMethod extends (error: ErrorInstanceOfMap<TPluginsMap>, ...args: any[]) => any>(
|
|
523
|
-
// key: TKey,
|
|
524
|
-
// value: TMethod,
|
|
525
|
-
// ) => ClassError0<ExtendErrorPluginsMapWithMethod<TPluginsMap, TKey, TMethod>>
|
|
526
|
-
// adapt: (
|
|
527
|
-
// value: ErrorPluginAdaptFn<ErrorInstanceOfMap<TPluginsMap>, ErrorResolvedProps<TPluginsMap>>,
|
|
528
|
-
// ) => ClassError0<TPluginsMap>
|
|
529
|
-
// stack: (value: ErrorPluginStack<ErrorInstanceOfMap<TPluginsMap>>) => ClassError0<TPluginsMap>
|
|
530
|
-
// cause: (value: ErrorPluginCause<ErrorInstanceOfMap<TPluginsMap>>) => ClassError0<TPluginsMap>
|
|
531
564
|
use: {
|
|
532
565
|
<TBuilder extends PluginError0>(
|
|
533
566
|
plugin: TBuilder,
|
|
@@ -560,10 +593,11 @@ export type ClassError0<TPluginsMap extends ErrorPluginsMap = EmptyPluginsMap> =
|
|
|
560
593
|
|
|
561
594
|
export class Error0 extends Error {
|
|
562
595
|
static readonly __pluginsMap?: EmptyPluginsMap
|
|
563
|
-
readonly __pluginsMap?: EmptyPluginsMap
|
|
596
|
+
declare readonly __pluginsMap?: EmptyPluginsMap
|
|
564
597
|
static MAX_CAUSES_DEPTH = 99
|
|
565
598
|
protected static _plugins: ErrorPlugin[] = []
|
|
566
599
|
protected static _resolvedPlugin?: ErrorPluginResolved
|
|
600
|
+
declare own?: ErrorOwnStore
|
|
567
601
|
|
|
568
602
|
private static readonly _emptyPlugin: ErrorPluginResolved = {
|
|
569
603
|
props: {},
|
|
@@ -669,63 +703,43 @@ export class Error0 extends Error {
|
|
|
669
703
|
|
|
670
704
|
const ctor = this.constructor as typeof Error0
|
|
671
705
|
const plugin = ctor._getResolvedPlugin()
|
|
672
|
-
const ownStore = Object.create(null) as ErrorOwnStore
|
|
673
|
-
Object.defineProperty(this, OWN_SYMBOL, { value: ownStore, writable: true, enumerable: false, configurable: true })
|
|
706
|
+
// const ownStore = Object.create(null) as ErrorOwnStore
|
|
707
|
+
// Object.defineProperty(this, OWN_SYMBOL, { value: ownStore, writable: true, enumerable: false, configurable: true })
|
|
674
708
|
|
|
675
709
|
for (const [key, prop] of plugin.propEntries) {
|
|
676
710
|
if (key === 'stack') {
|
|
677
711
|
continue
|
|
678
712
|
}
|
|
679
713
|
Object.defineProperty(this, key, {
|
|
680
|
-
get: () =>
|
|
681
|
-
prop.resolve({
|
|
682
|
-
own: ownStore[key],
|
|
683
|
-
flow: this.flow(key as never),
|
|
684
|
-
error: this,
|
|
685
|
-
}),
|
|
714
|
+
get: () => this._resolveByKey(key, plugin),
|
|
686
715
|
set: (value) => {
|
|
687
|
-
|
|
716
|
+
this.assign({ [key]: value } as never)
|
|
688
717
|
},
|
|
689
718
|
enumerable: true,
|
|
690
719
|
configurable: true,
|
|
691
720
|
})
|
|
692
721
|
if (key in input) {
|
|
693
|
-
const
|
|
694
|
-
|
|
722
|
+
const inputValue = (input as Record<string, unknown>)[key]
|
|
723
|
+
const ownValue = typeof prop.init === 'function' ? prop.init(inputValue) : inputValue
|
|
724
|
+
this.assign({ [key]: ownValue } as never)
|
|
695
725
|
}
|
|
696
726
|
}
|
|
727
|
+
Error0.fixStack(input.cause)
|
|
697
728
|
}
|
|
698
729
|
|
|
699
|
-
private static
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
}
|
|
713
|
-
return !!Object.getOwnPropertyDescriptor(object, key)
|
|
714
|
-
}
|
|
715
|
-
private static _ownByKey(error: object, key: string): unknown {
|
|
716
|
-
const ownStore = this._getOwnStore(error)
|
|
717
|
-
if (ownStore) {
|
|
718
|
-
return ownStore[key]
|
|
719
|
-
}
|
|
720
|
-
return (error as Record<string, unknown>)[key]
|
|
721
|
-
}
|
|
722
|
-
private static _flowByKey(error: object, key: string): unknown[] {
|
|
723
|
-
const causes = this.causes(error, true)
|
|
724
|
-
const values = new Array<unknown>(causes.length)
|
|
725
|
-
for (let i = 0; i < causes.length; i += 1) {
|
|
726
|
-
values[i] = this._ownByKey(causes[i], key)
|
|
727
|
-
}
|
|
728
|
-
return values
|
|
730
|
+
private static fixStack(cause: unknown): void {
|
|
731
|
+
try {
|
|
732
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
733
|
+
let nextCause = cause
|
|
734
|
+
let depth = 0
|
|
735
|
+
const maxDepth = 99
|
|
736
|
+
while (nextCause && depth < maxDepth) {
|
|
737
|
+
;(globalThis as any).__ERROR0_FIX_STACKTRACE__(nextCause)
|
|
738
|
+
nextCause = (nextCause as any).cause
|
|
739
|
+
depth++
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
} catch {}
|
|
729
743
|
}
|
|
730
744
|
|
|
731
745
|
static own<TThis extends typeof Error0>(this: TThis, error: unknown): ErrorOwnProps<PluginsMapOf<TThis>>
|
|
@@ -737,26 +751,9 @@ export class Error0 extends Error {
|
|
|
737
751
|
static own(error: unknown, key?: string): unknown {
|
|
738
752
|
const error0 = this.from(error)
|
|
739
753
|
if (key === undefined) {
|
|
740
|
-
|
|
741
|
-
const plugin = this._getResolvedPlugin()
|
|
742
|
-
for (const ownKey of plugin.propKeys) {
|
|
743
|
-
ownValues[ownKey] = this._ownByKey(error0, ownKey)
|
|
744
|
-
}
|
|
745
|
-
return ownValues
|
|
754
|
+
return error0.own ?? {}
|
|
746
755
|
}
|
|
747
|
-
return
|
|
748
|
-
}
|
|
749
|
-
own<TThis extends Error0>(this: TThis): ErrorOwnProps<PluginsMapOfInstance<TThis>>
|
|
750
|
-
own<TThis extends Error0, TKey extends keyof PluginsMapOfInstance<TThis>['props'] & string>(
|
|
751
|
-
this: TThis,
|
|
752
|
-
key: TKey,
|
|
753
|
-
): ErrorOwnProps<PluginsMapOfInstance<TThis>>[TKey]
|
|
754
|
-
own(key?: string): unknown {
|
|
755
|
-
const ctor = this.constructor as typeof Error0
|
|
756
|
-
if (key === undefined) {
|
|
757
|
-
return ctor.own(this)
|
|
758
|
-
}
|
|
759
|
-
return ctor._ownByKey(this, key)
|
|
756
|
+
return error0.own?.[key]
|
|
760
757
|
}
|
|
761
758
|
|
|
762
759
|
static flow<TThis extends typeof Error0, TKey extends keyof PluginsMapOf<TThis>['props'] & string>(
|
|
@@ -766,57 +763,68 @@ export class Error0 extends Error {
|
|
|
766
763
|
): Array<ErrorOwnProps<PluginsMapOf<TThis>>[TKey]>
|
|
767
764
|
static flow(error: unknown, key: string): unknown[] {
|
|
768
765
|
const error0 = this.from(error)
|
|
769
|
-
return
|
|
766
|
+
return error0.flow(key as never)
|
|
770
767
|
}
|
|
771
768
|
flow<TThis extends Error0, TKey extends keyof PluginsMapOfInstance<TThis>['props'] & string>(
|
|
772
769
|
this: TThis,
|
|
773
770
|
key: TKey,
|
|
774
771
|
): Array<ErrorOwnProps<PluginsMapOfInstance<TThis>>[TKey]>
|
|
775
772
|
flow(key: string): unknown[] {
|
|
776
|
-
const
|
|
777
|
-
|
|
773
|
+
const causes = this.causes(true)
|
|
774
|
+
const values = new Array<unknown>(causes.length)
|
|
775
|
+
for (let i = 0; i < causes.length; i += 1) {
|
|
776
|
+
values[i] = causes[i].own?.[key]
|
|
777
|
+
}
|
|
778
|
+
return values
|
|
778
779
|
}
|
|
779
780
|
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
get flow() {
|
|
787
|
-
return error.flow(key as never)
|
|
788
|
-
},
|
|
789
|
-
error,
|
|
790
|
-
}
|
|
791
|
-
const prop = plugin.props[key]
|
|
792
|
-
const resolver = prop.resolve
|
|
793
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
794
|
-
if (!resolver) {
|
|
795
|
-
return (error as any)[key]
|
|
796
|
-
}
|
|
797
|
-
return resolver(options as ErrorPluginPropOptionsResolveOptions<any, any>)
|
|
798
|
-
} catch {
|
|
799
|
-
// eslint-disable-next-line no-console
|
|
800
|
-
console.error(`Error0: failed to resolve property ${key}`, error)
|
|
801
|
-
return undefined
|
|
781
|
+
private readonly _resolveByKeyCache = new Map<string, unknown>()
|
|
782
|
+
private _resolveByKey(key: string, plugin: ErrorPluginResolved): unknown {
|
|
783
|
+
// eslint-disable-next-line consistent-this, @typescript-eslint/no-this-alias
|
|
784
|
+
const error = this
|
|
785
|
+
if (this._resolveByKeyCache.has(key)) {
|
|
786
|
+
return this._resolveByKeyCache.get(key)
|
|
802
787
|
}
|
|
788
|
+
const value = (() => {
|
|
789
|
+
try {
|
|
790
|
+
const options = {
|
|
791
|
+
get flow() {
|
|
792
|
+
return error.flow(key as never)
|
|
793
|
+
},
|
|
794
|
+
own: error.own?.[key],
|
|
795
|
+
error,
|
|
796
|
+
}
|
|
797
|
+
const prop = plugin.props[key]
|
|
798
|
+
const resolver = prop.resolve
|
|
799
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
800
|
+
if (!resolver) {
|
|
801
|
+
return (error as any)[key]
|
|
802
|
+
}
|
|
803
|
+
return resolver(options as ErrorPluginPropOptionsResolveOptions<any, any>)
|
|
804
|
+
} catch {
|
|
805
|
+
// eslint-disable-next-line no-console
|
|
806
|
+
console.error(`Error0: failed to resolve property ${key}`, error)
|
|
807
|
+
return undefined
|
|
808
|
+
}
|
|
809
|
+
})()
|
|
810
|
+
this._resolveByKeyCache.set(key, value)
|
|
811
|
+
return value
|
|
803
812
|
}
|
|
804
813
|
|
|
805
814
|
static resolve<TThis extends typeof Error0>(this: TThis, error: unknown): ErrorResolvedProps<PluginsMapOf<TThis>>
|
|
806
815
|
static resolve(error: unknown): Record<string, unknown>
|
|
807
816
|
static resolve(error: unknown): Record<string, unknown> {
|
|
808
|
-
|
|
809
|
-
const resolved: Record<string, unknown> = {}
|
|
810
|
-
const plugin = this._getResolvedPlugin()
|
|
811
|
-
for (const key of plugin.propKeys) {
|
|
812
|
-
resolved[key] = this._resolveByKey(error0, key, plugin)
|
|
813
|
-
}
|
|
814
|
-
return resolved
|
|
817
|
+
return this.from(error).resolve()
|
|
815
818
|
}
|
|
816
819
|
resolve<TThis extends Error0>(this: TThis): ErrorResolvedProps<PluginsMapOfInstance<TThis>>
|
|
817
820
|
resolve(): Record<string, unknown> {
|
|
818
821
|
const ctor = this.constructor as typeof Error0
|
|
819
|
-
|
|
822
|
+
const plugin = ctor._getResolvedPlugin()
|
|
823
|
+
const resolved: Record<string, unknown> = {}
|
|
824
|
+
for (const key of plugin.propKeys) {
|
|
825
|
+
resolved[key] = this._resolveByKey(key, plugin)
|
|
826
|
+
}
|
|
827
|
+
return resolved
|
|
820
828
|
}
|
|
821
829
|
|
|
822
830
|
static causes(error: unknown, instancesOnly?: false): unknown[]
|
|
@@ -860,6 +868,7 @@ export class Error0 extends Error {
|
|
|
860
868
|
return !this.is(error) && typeof error === 'object' && error !== null && 'name' in error && error.name === 'Error0'
|
|
861
869
|
}
|
|
862
870
|
|
|
871
|
+
static from<TThis extends typeof Error0>(this: TThis, error: unknown): InstanceType<TThis>
|
|
863
872
|
static from(error: unknown): Error0 {
|
|
864
873
|
if (this.is(error)) {
|
|
865
874
|
return error
|
|
@@ -870,8 +879,61 @@ export class Error0 extends Error {
|
|
|
870
879
|
return this._fromNonError0(error)
|
|
871
880
|
}
|
|
872
881
|
|
|
882
|
+
static round<TThis extends typeof Error0>(this: TThis, error: unknown, isPublic?: boolean): InstanceType<TThis>
|
|
873
883
|
static round(error: unknown, isPublic = false): Error0 {
|
|
874
|
-
return this.from(
|
|
884
|
+
return this.from(error).round(isPublic)
|
|
885
|
+
}
|
|
886
|
+
round<TThis extends Error0>(this: TThis, isPublic = true): TThis {
|
|
887
|
+
const ctor = this.constructor as typeof Error0
|
|
888
|
+
return ctor.from(this.serialize(isPublic)) as TThis
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// static flat<TThis extends typeof Error0>(this: TThis, error: unknown, keepCauses?: boolean): InstanceType<TThis>
|
|
892
|
+
// static flat(error: unknown, keepCauses = false): Error0 {
|
|
893
|
+
// return this.from(error).flat(keepCauses)
|
|
894
|
+
// }
|
|
895
|
+
// flat<TThis extends Error0>(this: TThis, keepCauses = false): TThis {
|
|
896
|
+
// const ctor = this.constructor as typeof Error0
|
|
897
|
+
// // eslint-disable-next-line new-cap
|
|
898
|
+
// const error = new ctor(this.message) as TThis
|
|
899
|
+
// if (keepCauses) {
|
|
900
|
+
// error.cause = this.cause
|
|
901
|
+
// }
|
|
902
|
+
// error.assign({
|
|
903
|
+
// // ...this.own,
|
|
904
|
+
// ...this.resolve(),
|
|
905
|
+
// } as never)
|
|
906
|
+
// error.stack =
|
|
907
|
+
// this.causes()
|
|
908
|
+
// .map((cause, index) => {
|
|
909
|
+
// return cause instanceof Error && cause.stack && typeof cause.stack === 'string'
|
|
910
|
+
// ? index === 0
|
|
911
|
+
// ? cause.stack
|
|
912
|
+
// : cause.stack.split('\n').slice(1).join('\n')
|
|
913
|
+
// : undefined
|
|
914
|
+
// })
|
|
915
|
+
// .join('\n') || undefined
|
|
916
|
+
// return error
|
|
917
|
+
// }
|
|
918
|
+
|
|
919
|
+
static assign<TThis extends typeof Error0>(
|
|
920
|
+
this: TThis,
|
|
921
|
+
error: unknown,
|
|
922
|
+
props: Partial<ErrorOwnProps<PluginsMapOf<TThis>>>,
|
|
923
|
+
): InstanceType<TThis>
|
|
924
|
+
static assign(error: unknown, props: Record<string, unknown>): Error0 {
|
|
925
|
+
const error0 = this.from(error)
|
|
926
|
+
return error0.assign(props)
|
|
927
|
+
}
|
|
928
|
+
assign<TThis extends Error0>(this: TThis, props: Partial<ErrorOwnProps<PluginsMapOfInstance<TThis>>>): TThis
|
|
929
|
+
assign(props: Record<string, unknown>): this {
|
|
930
|
+
this.own = Object.assign(this.own ?? {}, props)
|
|
931
|
+
this._resolveByKeyCache.clear()
|
|
932
|
+
// const values = Object.values(props)
|
|
933
|
+
// if (values.every((value) => value === undefined)) {
|
|
934
|
+
// this.own = undefined
|
|
935
|
+
// }
|
|
936
|
+
return this
|
|
875
937
|
}
|
|
876
938
|
|
|
877
939
|
private static _applyAdapt(error: Error0): Error0 {
|
|
@@ -879,7 +941,7 @@ export class Error0 extends Error {
|
|
|
879
941
|
for (const adapt of plugin.adapt) {
|
|
880
942
|
const adapted = adapt(error as any)
|
|
881
943
|
if (adapted && typeof adapted === 'object') {
|
|
882
|
-
|
|
944
|
+
error.assign(adapted)
|
|
883
945
|
}
|
|
884
946
|
}
|
|
885
947
|
return error
|
|
@@ -988,49 +1050,6 @@ export class Error0 extends Error {
|
|
|
988
1050
|
}
|
|
989
1051
|
}
|
|
990
1052
|
|
|
991
|
-
// static prop<
|
|
992
|
-
// TThis extends typeof Error0,
|
|
993
|
-
// TKey extends string,
|
|
994
|
-
// TInputValue = undefined,
|
|
995
|
-
// TOutputValue = unknown,
|
|
996
|
-
// TResolveValue extends TOutputValue | undefined = TOutputValue | undefined,
|
|
997
|
-
// >(
|
|
998
|
-
// this: TThis,
|
|
999
|
-
// key: TKey,
|
|
1000
|
-
// value: ErrorPluginPropOptions<TInputValue, TOutputValue, ErrorInstanceOfMap<PluginsMapOf<TThis>>, TResolveValue>,
|
|
1001
|
-
// ): ClassError0<ExtendErrorPluginsMapWithProp<PluginsMapOf<TThis>, TKey, TInputValue, TOutputValue, TResolveValue>> {
|
|
1002
|
-
// return this.use('prop', key, value)
|
|
1003
|
-
// }
|
|
1004
|
-
|
|
1005
|
-
// static method<TThis extends typeof Error0, TKey extends string, TMethod extends (error: ErrorInstanceOfMap<PluginsMapOf<TThis>>, ...args: any[]) => any>(
|
|
1006
|
-
// this: TThis,
|
|
1007
|
-
// key: TKey,
|
|
1008
|
-
// value: TMethod,
|
|
1009
|
-
// ): ClassError0<ExtendErrorPluginsMapWithMethod<PluginsMapOf<TThis>, TKey, TMethod>> {
|
|
1010
|
-
// return this.use('method', key, value)
|
|
1011
|
-
// }
|
|
1012
|
-
|
|
1013
|
-
// static adapt<TThis extends typeof Error0>(
|
|
1014
|
-
// this: TThis,
|
|
1015
|
-
// value: ErrorPluginAdaptFn<ErrorInstanceOfMap<PluginsMapOf<TThis>>, ErrorResolvedProps<PluginsMapOf<TThis>>>,
|
|
1016
|
-
// ): ClassError0<PluginsMapOf<TThis>> {
|
|
1017
|
-
// return this.use('adapt', value)
|
|
1018
|
-
// }
|
|
1019
|
-
|
|
1020
|
-
// static stack<TThis extends typeof Error0>(
|
|
1021
|
-
// this: TThis,
|
|
1022
|
-
// value: ErrorPluginStack<ErrorInstanceOfMap<PluginsMapOf<TThis>>>,
|
|
1023
|
-
// ): ClassError0<PluginsMapOf<TThis>> {
|
|
1024
|
-
// return this.use('stack', value)
|
|
1025
|
-
// }
|
|
1026
|
-
|
|
1027
|
-
// static cause<TThis extends typeof Error0>(
|
|
1028
|
-
// this: TThis,
|
|
1029
|
-
// value: ErrorPluginCause<ErrorInstanceOfMap<PluginsMapOf<TThis>>>,
|
|
1030
|
-
// ): ClassError0<PluginsMapOf<TThis>> {
|
|
1031
|
-
// return this.use('cause', value)
|
|
1032
|
-
// }
|
|
1033
|
-
|
|
1034
1053
|
static use<TThis extends typeof Error0, TBuilder extends PluginError0>(
|
|
1035
1054
|
this: TThis,
|
|
1036
1055
|
plugin: TBuilder,
|
|
@@ -1045,7 +1064,12 @@ export class Error0 extends Error {
|
|
|
1045
1064
|
this: TThis,
|
|
1046
1065
|
kind: 'prop',
|
|
1047
1066
|
key: TKey,
|
|
1048
|
-
value:
|
|
1067
|
+
value: ErrorPluginPropOptionsDefinition<
|
|
1068
|
+
TInputValue,
|
|
1069
|
+
TOutputValue,
|
|
1070
|
+
ErrorInstanceOfMap<PluginsMapOf<TThis>>,
|
|
1071
|
+
TResolveValue
|
|
1072
|
+
>,
|
|
1049
1073
|
): ClassError0<ExtendErrorPluginsMapWithProp<PluginsMapOf<TThis>, TKey, TInputValue, TOutputValue, TResolveValue>>
|
|
1050
1074
|
static use<
|
|
1051
1075
|
TThis extends typeof Error0,
|
|
@@ -1081,8 +1105,8 @@ export class Error0 extends Error {
|
|
|
1081
1105
|
this: typeof Error0,
|
|
1082
1106
|
first: PluginError0 | 'prop' | 'method' | 'adapt' | 'stack' | 'cause' | 'message',
|
|
1083
1107
|
key?: unknown,
|
|
1084
|
-
value?:
|
|
1085
|
-
): ClassError0 {
|
|
1108
|
+
value?: ErrorPluginPropOptionsDefinition<unknown> | ErrorPluginMethodFn<unknown>,
|
|
1109
|
+
): ClassError0<any> {
|
|
1086
1110
|
if (first instanceof PluginError0) {
|
|
1087
1111
|
return this._useWithPlugin(this._pluginFromBuilder(first))
|
|
1088
1112
|
}
|
|
@@ -1144,7 +1168,7 @@ export class Error0 extends Error {
|
|
|
1144
1168
|
throw new Error(RESERVED_MESSAGE_PROP_ERROR)
|
|
1145
1169
|
}
|
|
1146
1170
|
return this._useWithPlugin({
|
|
1147
|
-
props: { [key]: value as
|
|
1171
|
+
props: { [key]: fromPropOptionsDefinition(value as ErrorPluginPropOptionsDefinition<any, any>) },
|
|
1148
1172
|
})
|
|
1149
1173
|
}
|
|
1150
1174
|
return this._useWithPlugin({
|
|
@@ -1157,10 +1181,14 @@ export class Error0 extends Error {
|
|
|
1157
1181
|
}
|
|
1158
1182
|
|
|
1159
1183
|
static serialize(error: unknown, isPublic = true): Record<string, unknown> {
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1184
|
+
return this.from(error).serialize(isPublic)
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
serialize(isPublic = true): Record<string, unknown> {
|
|
1188
|
+
// eslint-disable-next-line consistent-this, @typescript-eslint/no-this-alias
|
|
1189
|
+
const error0 = this
|
|
1190
|
+
const ctor = error0.constructor as typeof Error0
|
|
1191
|
+
const plugin = ctor._getResolvedPlugin()
|
|
1164
1192
|
const messagePlugin = plugin.message
|
|
1165
1193
|
let serializedMessage: unknown = error0.message
|
|
1166
1194
|
try {
|
|
@@ -1185,15 +1213,13 @@ export class Error0 extends Error {
|
|
|
1185
1213
|
}
|
|
1186
1214
|
try {
|
|
1187
1215
|
const options = {
|
|
1188
|
-
get own() {
|
|
1189
|
-
return error0.own(key as never)
|
|
1190
|
-
},
|
|
1191
1216
|
get flow() {
|
|
1192
1217
|
return error0.flow(key as never)
|
|
1193
1218
|
},
|
|
1194
1219
|
get resolved() {
|
|
1195
|
-
return
|
|
1220
|
+
return error0._resolveByKey(key, plugin)
|
|
1196
1221
|
},
|
|
1222
|
+
own: error0.own?.[key],
|
|
1197
1223
|
error: error0,
|
|
1198
1224
|
isPublic,
|
|
1199
1225
|
}
|
|
@@ -1228,8 +1254,8 @@ export class Error0 extends Error {
|
|
|
1228
1254
|
cause: (error0 as { cause?: unknown }).cause,
|
|
1229
1255
|
error: error0,
|
|
1230
1256
|
isPublic,
|
|
1231
|
-
is: (cause) =>
|
|
1232
|
-
serialize: (cause) =>
|
|
1257
|
+
is: (cause) => ctor.is(cause),
|
|
1258
|
+
serialize: (cause) => ctor.serialize(cause, isPublic),
|
|
1233
1259
|
})
|
|
1234
1260
|
if (serializedCause !== undefined) {
|
|
1235
1261
|
json.cause = serializedCause
|
|
@@ -1241,14 +1267,4 @@ export class Error0 extends Error {
|
|
|
1241
1267
|
}
|
|
1242
1268
|
return json
|
|
1243
1269
|
}
|
|
1244
|
-
|
|
1245
|
-
serialize(isPublic = true): Record<string, unknown> {
|
|
1246
|
-
const ctor = this.constructor as typeof Error0
|
|
1247
|
-
return ctor.serialize(this, isPublic)
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
round<TThis extends Error0>(this: TThis, isPublic = true): TThis {
|
|
1251
|
-
const ctor = this.constructor as typeof Error0
|
|
1252
|
-
return ctor.round(this, isPublic) as TThis
|
|
1253
|
-
}
|
|
1254
1270
|
}
|