@devp0nt/error0 1.0.0-next.1 → 1.0.0-next.11

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/src/index.ts CHANGED
@@ -1,81 +1,69 @@
1
- import { Meta0 } from '@devp0nt/meta0'
1
+ import { Meta0, meta0PluginTag } from '@devp0nt/meta0'
2
2
  import { type AxiosError, HttpStatusCode, isAxiosError } from 'axios'
3
3
  import get from 'lodash/get.js'
4
4
  import { ZodError } from 'zod'
5
5
 
6
+ // TODO: Зод, аксиос, это всё плагины
7
+ // TODO: В эррор0 добавить ориджинал
6
8
  // TODO: store tags as array from all causes
7
9
  // TODO: not use self stack if toError0
8
10
  // TODO: fix default message in extended error0, should be used in constuctor of Error0
9
11
  // TODO: remove defaults prop from getPropsFromUnknown
10
12
  // TODO: code has enum type, fn to check if code exists
11
13
 
12
- export interface Error0Input {
13
- message?: string
14
- tag?: string
15
- code?: string
16
- httpStatus?: HttpStatusCode | HttpStatusCodeString
17
- expected?: boolean | ExpectedFn
18
- clientMessage?: string
19
- cause?: Error0Cause
20
- stack?: string
21
- meta?: Meta0.Meta0OrValueTypeNullish
22
- zodError?: ZodError
23
- axiosError?: AxiosError
14
+ const isFilled = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined && value !== ''
15
+ const toStringOrUndefined = (value: unknown): string | undefined => {
16
+ return typeof value === 'string' ? value : undefined
24
17
  }
25
-
26
- interface Error0GeneralProps {
27
- message: Error0Input['message']
28
- tag: Error0Input['tag']
29
- code: Error0Input['code']
30
- httpStatus: number | undefined
31
- expected: boolean | undefined
32
- clientMessage: Error0Input['clientMessage']
33
- anyMessage: string | undefined
34
- cause: Error0Input['cause']
35
- stack: Error['stack']
36
- meta: Meta0.ValueType
37
- zodError?: ZodError
38
- axiosError?: AxiosError
18
+ const toNumberOrUndefined = (value: unknown): number | undefined => {
19
+ if (typeof value === 'number') {
20
+ return value
21
+ }
22
+ const number = Number(value)
23
+ if (typeof value === 'string' && !Number.isNaN(number)) {
24
+ return number
25
+ }
26
+ return undefined
27
+ }
28
+ const toBooleanOrUndefined = (value: unknown): boolean | undefined => {
29
+ if (typeof value === 'boolean') {
30
+ return value
31
+ }
32
+ return undefined
39
33
  }
40
-
41
- type HttpStatusCodeString = keyof typeof HttpStatusCode
42
- type Error0Cause = Error | Error0 | unknown
43
- type ExpectedFn = (error: Error0GeneralProps) => boolean | undefined
44
-
45
- const isFilled = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined && value !== ''
46
34
 
47
35
  export class Error0 extends Error {
48
36
  public readonly __I_AM_ERROR_0: true = true
49
37
 
50
- public readonly tag?: Error0GeneralProps['tag']
51
- public readonly code?: Error0GeneralProps['code']
52
- public readonly httpStatus?: Error0GeneralProps['httpStatus']
53
- public readonly expected?: Error0GeneralProps['expected']
54
- public readonly clientMessage?: Error0GeneralProps['clientMessage']
55
- public readonly anyMessage?: Error0GeneralProps['anyMessage']
56
- public override readonly cause?: Error0GeneralProps['cause']
38
+ public readonly tag?: Error0.GeneralProps['tag']
39
+ public readonly code?: Error0.GeneralProps['code']
40
+ public readonly httpStatus?: Error0.GeneralProps['httpStatus']
41
+ public readonly expected?: Error0.GeneralProps['expected']
42
+ public readonly clientMessage?: Error0.GeneralProps['clientMessage']
43
+ public readonly anyMessage?: Error0.GeneralProps['anyMessage']
44
+ public override readonly cause?: Error0.GeneralProps['cause']
57
45
  public readonly meta?: Meta0.Meta0OrValueTypeNullish
58
- public readonly zodError?: Error0GeneralProps['zodError']
59
- public readonly axiosError?: Error0GeneralProps['axiosError']
46
+ public readonly zodError?: Error0.GeneralProps['zodError']
47
+ public readonly axiosError?: Error0.GeneralProps['axiosError']
60
48
 
61
49
  static defaultMessage = 'Unknown error'
62
- static defaultCode?: Error0GeneralProps['code']
63
- static defaultHttpStatus?: Error0GeneralProps['httpStatus']
64
- static defaultExpected?: Error0GeneralProps['expected']
65
- static defaultClientMessage?: Error0GeneralProps['clientMessage']
50
+ static defaultCode?: Error0.GeneralProps['code']
51
+ static defaultHttpStatus?: Error0.GeneralProps['httpStatus']
52
+ static defaultExpected?: Error0.GeneralProps['expected']
53
+ static defaultClientMessage?: Error0.GeneralProps['clientMessage']
66
54
  static defaultMeta?: Meta0.Meta0OrValueTypeNullish
67
55
 
68
- public readonly propsOriginal: Error0GeneralProps
56
+ public readonly propsOriginal: Error0.GeneralProps
69
57
 
70
58
  constructor(message: string)
71
- constructor(input: Error0Input)
72
- constructor(message: string, input: Error0Input)
59
+ constructor(input: Error0.Input)
60
+ constructor(message: string, input: Error0.Input)
73
61
  constructor(error: Error)
74
- constructor(error: Error, input: Error0Input)
62
+ constructor(error: Error, input: Error0.Input)
75
63
  constructor(value: unknown)
76
- constructor(value: unknown, input: Error0Input)
64
+ constructor(value: unknown, input: Error0.Input)
77
65
  constructor(...args: unknown[]) {
78
- const input: Partial<Error0Input> = {}
66
+ const input: Partial<Error0.Input> = {}
79
67
  if (args[0] instanceof Error) {
80
68
  input.cause = args[0]
81
69
  } else if (typeof args[0] === 'object' && args[0] !== null) {
@@ -121,8 +109,8 @@ export class Error0 extends Error {
121
109
 
122
110
  // props
123
111
 
124
- public static _safeParseInput(error0Input: Record<string, unknown>): Error0Input {
125
- const result: Error0Input = {}
112
+ public static _safeParseInput(error0Input: Record<string, unknown>): Error0.Input {
113
+ const result: Error0.Input = {}
126
114
  result.message = typeof error0Input.message === 'string' ? error0Input.message : undefined
127
115
  result.tag = typeof error0Input.tag === 'string' ? error0Input.tag : undefined
128
116
  result.code = typeof error0Input.code === 'string' ? error0Input.code : undefined
@@ -159,19 +147,27 @@ export class Error0 extends Error {
159
147
  message,
160
148
  stack,
161
149
  }: {
162
- error0Input: Error0Input
150
+ error0Input: Error0.Input
163
151
  message: string
164
- stack: Error0GeneralProps['stack']
165
- }): Error0GeneralProps {
152
+ stack: Error0.GeneralProps['stack']
153
+ }): Error0.GeneralProps {
166
154
  // const meta = Meta0.merge(error0Input.meta0, error0Input.meta).value
167
- const meta0 = Meta0.extend(error0Input.meta, this.defaultMeta)
155
+
156
+ // const meta0 = Meta0.extend(error0Input.meta, this.defaultMeta)
157
+ // const defaultMetaValue =
158
+ // this.defaultMeta && typeof this.defaultMeta === 'object' && 'getValue' in this.defaultMeta
159
+ // ? (this.defaultMeta as any).getValue()
160
+ // : this.defaultMeta
161
+
162
+ const meta0 = Meta0.extend(this.defaultMeta, error0Input.meta)
168
163
  const meta = meta0.getValue()
169
- const finalTag = meta0.getFinalTag(error0Input.tag)
164
+ const finalTag = meta0PluginTag.public.getFullTag(meta0, error0Input.tag)
165
+ delete meta.tagPrefix
170
166
  const clientMessage = error0Input.clientMessage || this.defaultClientMessage
171
- const result: Error0GeneralProps = {
167
+ const result: Error0.GeneralProps = {
172
168
  message: error0Input.message || this.defaultMessage,
173
169
  tag: finalTag,
174
- code: error0Input.code || meta.code || this.defaultCode,
170
+ code: error0Input.code || toStringOrUndefined(meta.code) || this.defaultCode,
175
171
  httpStatus:
176
172
  typeof error0Input.httpStatus === 'number'
177
173
  ? error0Input.httpStatus
@@ -179,7 +175,7 @@ export class Error0 extends Error {
179
175
  typeof error0Input.httpStatus === 'string' &&
180
176
  error0Input.httpStatus in HttpStatusCode
181
177
  ? HttpStatusCode[error0Input.httpStatus]
182
- : meta.httpStatus || this.defaultHttpStatus,
178
+ : toNumberOrUndefined(meta.httpStatus) || this.defaultHttpStatus,
183
179
  expected: undefined,
184
180
  clientMessage,
185
181
  anyMessage: clientMessage || message,
@@ -193,19 +189,19 @@ export class Error0 extends Error {
193
189
  result,
194
190
  typeof error0Input.expected === 'boolean' || typeof error0Input.expected === 'function'
195
191
  ? error0Input.expected
196
- : meta.expected || this.defaultExpected,
192
+ : toBooleanOrUndefined(meta.expected) || this.defaultExpected,
197
193
  )
198
194
  result.stack = this._removeConstructorStackPart(stack)
199
195
  return result
200
196
  }
201
197
 
202
- public static _getSelfPropsFloated(causesProps: Error0GeneralProps[]): Error0GeneralProps {
198
+ public static _getSelfPropsFloated(causesProps: Error0.GeneralProps[]): Error0.GeneralProps {
203
199
  const cause = this._getClosestPropValue(causesProps, 'cause')
204
200
  const stack = this._mergeStack(causesProps[1]?.stack, causesProps[0]?.stack)
205
201
  const closestTag = this._getClosestPropValue(causesProps, 'tag')
206
202
  const meta = this._getMergedMetaValue(causesProps)
207
- const tag = Meta0.getFinalTag(meta, closestTag)
208
- const propsFloated: Error0GeneralProps = {
203
+ const tag = meta0PluginTag.public.getFullTag(meta, closestTag)
204
+ const propsFloated: Error0.GeneralProps = {
209
205
  message: this._getClosestPropValue(causesProps, 'message'),
210
206
  tag,
211
207
  code: this._getClosestPropValue(causesProps, 'code'),
@@ -224,13 +220,13 @@ export class Error0 extends Error {
224
220
 
225
221
  // sepcial
226
222
 
227
- public static _getExtraError0PropsByZodError(zodError: ZodError): Partial<Error0GeneralProps> {
223
+ public static _getExtraError0PropsByZodError(zodError: ZodError): Partial<Error0.GeneralProps> {
228
224
  return {
229
225
  message: `Zod Validation Error: ${zodError.message}`,
230
226
  }
231
227
  }
232
228
 
233
- public static _getExtraError0PropsByAxiosError(axiosError: AxiosError): Partial<Error0GeneralProps> {
229
+ public static _getExtraError0PropsByAxiosError(axiosError: AxiosError): Partial<Error0.GeneralProps> {
234
230
  return {
235
231
  message: 'Axios Error',
236
232
  meta: {
@@ -247,8 +243,8 @@ export class Error0 extends Error {
247
243
  }
248
244
 
249
245
  public static _assignError0Props(
250
- error0Props: Error0GeneralProps,
251
- extraError0Props: Partial<Error0GeneralProps>,
246
+ error0Props: Error0.GeneralProps,
247
+ extraError0Props: Partial<Error0.GeneralProps>,
252
248
  ): void {
253
249
  const metaValue = Meta0.mergeValues(error0Props.meta, extraError0Props.meta)
254
250
  Object.assign(error0Props, extraError0Props, { meta: metaValue })
@@ -257,8 +253,8 @@ export class Error0 extends Error {
257
253
  // expected
258
254
 
259
255
  public static _normalizeSelfExpected(
260
- error0Props: Error0GeneralProps,
261
- expectedProvided: Error0Input['expected'],
256
+ error0Props: Error0.GeneralProps,
257
+ expectedProvided: Error0.Input['expected'],
262
258
  ): boolean | undefined {
263
259
  if (typeof expectedProvided === 'function') {
264
260
  return expectedProvided(error0Props)
@@ -266,7 +262,7 @@ export class Error0 extends Error {
266
262
  return expectedProvided
267
263
  }
268
264
 
269
- public static _isExpected(causesProps: Error0GeneralProps[]): boolean {
265
+ public static _isExpected(causesProps: Error0.GeneralProps[]): boolean {
270
266
  let hasExpectedTrue = false
271
267
  for (const causeProps of causesProps) {
272
268
  if (causeProps.expected === false) {
@@ -281,7 +277,7 @@ export class Error0 extends Error {
281
277
 
282
278
  // getters
283
279
 
284
- public static _getPropsFromUnknown(error: unknown, defaults?: Error0Input): Error0GeneralProps {
280
+ public static _getPropsFromUnknown(error: unknown, defaults?: Error0.Input): Error0.GeneralProps {
285
281
  if (typeof error !== 'object' || error === null) {
286
282
  return {
287
283
  message: undefined,
@@ -303,7 +299,7 @@ export class Error0 extends Error {
303
299
  'clientMessage' in error && typeof error.clientMessage === 'string'
304
300
  ? error.clientMessage
305
301
  : defaults?.clientMessage || undefined
306
- const result: Error0GeneralProps = {
302
+ const result: Error0.GeneralProps = {
307
303
  message,
308
304
  code: 'code' in error && typeof error.code === 'string' ? error.code : defaults?.code || undefined,
309
305
  clientMessage,
@@ -338,7 +334,7 @@ export class Error0 extends Error {
338
334
  result.expected = this._normalizeSelfExpected(
339
335
  result,
340
336
  'expected' in error && (typeof error.expected === 'boolean' || typeof error.expected === 'function')
341
- ? (error.expected as ExpectedFn)
337
+ ? (error.expected as Error0.ExpectedFn)
342
338
  : defaults?.expected || undefined,
343
339
  )
344
340
  if (result.zodError) {
@@ -350,12 +346,12 @@ export class Error0 extends Error {
350
346
  return result
351
347
  }
352
348
 
353
- public static _getCausesPropsFromUnknown(error: unknown, maxLevel: number): Error0GeneralProps[] {
349
+ public static _getCausesPropsFromUnknown(error: unknown, maxLevel: number): Error0.GeneralProps[] {
354
350
  if (!error) {
355
351
  return []
356
352
  }
357
353
  const causeProps = this._getPropsFromUnknown(error)
358
- const causesProps: Error0GeneralProps[] = [causeProps]
354
+ const causesProps: Error0.GeneralProps[] = [causeProps]
359
355
  if (!causeProps.cause) {
360
356
  return causesProps
361
357
  }
@@ -366,28 +362,28 @@ export class Error0 extends Error {
366
362
  }
367
363
 
368
364
  public static _getCausesPropsFromError0Props(
369
- error0Props: Error0GeneralProps,
365
+ error0Props: Error0.GeneralProps,
370
366
  maxLevel: number,
371
- ): Error0GeneralProps[] {
367
+ ): Error0.GeneralProps[] {
372
368
  return [error0Props, ...this._getCausesPropsFromUnknown(error0Props.cause, maxLevel - 1)]
373
369
  }
374
370
 
375
- public static _getClosestPropValue<TPropKey extends keyof Error0GeneralProps>(
376
- causesProps: Error0GeneralProps[],
371
+ public static _getClosestPropValue<TPropKey extends keyof Error0.GeneralProps>(
372
+ causesProps: Error0.GeneralProps[],
377
373
  propKey: TPropKey,
378
- ): NonNullable<Error0GeneralProps[TPropKey]> | undefined {
374
+ ): NonNullable<Error0.GeneralProps[TPropKey]> | undefined {
379
375
  for (const causeProps of causesProps) {
380
376
  const propValue = causeProps[propKey]
381
377
  if (isFilled(propValue)) {
382
- return propValue as NonNullable<Error0GeneralProps[TPropKey]>
378
+ return propValue as NonNullable<Error0.GeneralProps[TPropKey]>
383
379
  }
384
380
  }
385
381
  return undefined
386
382
  }
387
383
 
388
384
  // private static getClosestByGetter<TResult>(
389
- // causesProps: Error0GeneralProps[],
390
- // getter: (props: Error0GeneralProps) => TResult,
385
+ // causesProps: Error0.GeneralProps[],
386
+ // getter: (props: Error0.GeneralProps) => TResult,
391
387
  // ): NonNullable<TResult> | undefined {
392
388
  // for (const causeProps of causesProps) {
393
389
  // const result = getter(causeProps)
@@ -398,21 +394,21 @@ export class Error0 extends Error {
398
394
  // return undefined
399
395
  // }
400
396
 
401
- public static _getFilledPropValues<TPropKey extends keyof Error0Input>(
402
- causesProps: Error0GeneralProps[],
397
+ public static _getFilledPropValues<TPropKey extends keyof Error0.Input>(
398
+ causesProps: Error0.GeneralProps[],
403
399
  propKey: TPropKey,
404
- ): NonNullable<Error0GeneralProps[TPropKey]>[] {
405
- const values: NonNullable<Error0GeneralProps[TPropKey]>[] = []
400
+ ): NonNullable<Error0.GeneralProps[TPropKey]>[] {
401
+ const values: NonNullable<Error0.GeneralProps[TPropKey]>[] = []
406
402
  for (const causeProps of causesProps) {
407
403
  const propValue = causeProps[propKey]
408
404
  if (isFilled(propValue)) {
409
- values.push(propValue as NonNullable<Error0GeneralProps[TPropKey]>)
405
+ values.push(propValue as NonNullable<Error0.GeneralProps[TPropKey]>)
410
406
  }
411
407
  }
412
408
  return values
413
409
  }
414
410
 
415
- public static _getMergedMetaValue(causesProps: Error0GeneralProps[]): Meta0.ValueType {
411
+ public static _getMergedMetaValue(causesProps: Error0.GeneralProps[]): Meta0.ValueType {
416
412
  const metas = this._getFilledPropValues(causesProps, 'meta')
417
413
  if (metas.length === 0) {
418
414
  return {}
@@ -425,7 +421,7 @@ export class Error0 extends Error {
425
421
 
426
422
  // stack
427
423
 
428
- public static _removeConstructorStackPart(stack: Error0GeneralProps['stack']): Error0GeneralProps['stack'] {
424
+ public static _removeConstructorStackPart(stack: Error0.GeneralProps['stack']): Error0.GeneralProps['stack'] {
429
425
  if (!stack) {
430
426
  return stack
431
427
  }
@@ -441,9 +437,9 @@ export class Error0 extends Error {
441
437
  }
442
438
 
443
439
  public static _mergeStack(
444
- prevStack: Error0GeneralProps['stack'],
445
- nextStack: Error0GeneralProps['stack'],
446
- ): Error0GeneralProps['stack'] {
440
+ prevStack: Error0.GeneralProps['stack'],
441
+ nextStack: Error0.GeneralProps['stack'],
442
+ ): Error0.GeneralProps['stack'] {
447
443
  return [nextStack, prevStack].filter(Boolean).join('\n\n') || undefined
448
444
  }
449
445
 
@@ -467,7 +463,7 @@ export class Error0 extends Error {
467
463
  return false
468
464
  }
469
465
 
470
- public static _toError0(error: unknown, inputOverride: Error0Input = {}): Error0 {
466
+ public static _toError0(error: unknown, inputOverride: Error0.Input = {}): Error0 {
471
467
  if (error instanceof Error0) {
472
468
  return error
473
469
  }
@@ -500,16 +496,16 @@ export class Error0 extends Error {
500
496
  return new Error0(this._getPropsFromUnknown(error, inputOverride))
501
497
  }
502
498
 
503
- static from(error: unknown, inputOverride?: Error0Input): Error0 {
499
+ static from(error: unknown, inputOverride?: Error0.Input): Error0 {
504
500
  return this._toError0(error, inputOverride)
505
501
  }
506
502
 
507
503
  static extend(props: {
508
- defaultMessage?: Error0GeneralProps['message']
509
- defaultCode?: Error0GeneralProps['code']
510
- defaultHttpStatus?: Error0GeneralProps['httpStatus']
511
- defaultExpected?: Error0GeneralProps['expected']
512
- defaultClientMessage?: Error0GeneralProps['clientMessage']
504
+ defaultMessage?: Error0.GeneralProps['message']
505
+ defaultCode?: Error0.GeneralProps['code']
506
+ defaultHttpStatus?: Error0.GeneralProps['httpStatus']
507
+ defaultExpected?: Error0.GeneralProps['expected']
508
+ defaultClientMessage?: Error0.GeneralProps['clientMessage']
513
509
  defaultMeta?: Meta0.Meta0OrValueTypeNullish
514
510
  }) {
515
511
  const parent = this
@@ -519,18 +515,18 @@ export class Error0 extends Error {
519
515
  static override defaultHttpStatus = props.defaultHttpStatus ?? parent.defaultHttpStatus
520
516
  static override defaultExpected = props.defaultExpected ?? parent.defaultExpected
521
517
  static override defaultClientMessage = props.defaultClientMessage ?? parent.defaultClientMessage
522
- static override defaultMeta = Meta0.extend(props.defaultMeta, parent.defaultMeta)
518
+ static override defaultMeta = Meta0.extend(parent.defaultMeta, props.defaultMeta)
523
519
  }
524
520
  }
525
521
 
526
522
  static extendCollection<T extends Record<string, typeof Error0>>(
527
523
  classes: T,
528
524
  props: {
529
- defaultMessage?: Error0GeneralProps['message']
530
- defaultCode?: Error0GeneralProps['code']
531
- defaultHttpStatus?: Error0GeneralProps['httpStatus']
532
- defaultExpected?: Error0GeneralProps['expected']
533
- defaultClientMessage?: Error0GeneralProps['clientMessage']
525
+ defaultMessage?: Error0.GeneralProps['message']
526
+ defaultCode?: Error0.GeneralProps['code']
527
+ defaultHttpStatus?: Error0.GeneralProps['httpStatus']
528
+ defaultExpected?: Error0.GeneralProps['expected']
529
+ defaultClientMessage?: Error0.GeneralProps['clientMessage']
534
530
  defaultMeta?: Meta0.Meta0OrValueTypeNullish
535
531
  },
536
532
  ): T {
@@ -552,7 +548,7 @@ export class Error0 extends Error {
552
548
  __I_AM_ERROR_0: this.__I_AM_ERROR_0,
553
549
  }
554
550
  }
555
- static toJSON(error: unknown, inputOverride?: Error0Input) {
551
+ static toJSON(error: unknown, inputOverride?: Error0.Input) {
556
552
  const error0 = this.from(error, inputOverride)
557
553
  return error0.toJSON()
558
554
  }
@@ -572,13 +568,38 @@ export class Error0 extends Error {
572
568
  }
573
569
 
574
570
  export namespace Error0 {
571
+ export interface Input {
572
+ message?: string
573
+ tag?: string
574
+ code?: string
575
+ httpStatus?: HttpStatusCode | HttpStatusCodeString
576
+ expected?: boolean | Error0.ExpectedFn
577
+ clientMessage?: string
578
+ cause?: Error0Cause
579
+ stack?: string
580
+ meta?: Meta0.Meta0OrValueTypeNullish
581
+ zodError?: ZodError
582
+ axiosError?: AxiosError
583
+ }
584
+
585
+ export interface GeneralProps {
586
+ message: Input['message']
587
+ tag: Input['tag']
588
+ code: Input['code']
589
+ httpStatus: number | undefined
590
+ expected: boolean | undefined
591
+ clientMessage: Input['clientMessage']
592
+ anyMessage: string | undefined
593
+ cause: Input['cause']
594
+ stack: Error['stack']
595
+ meta: Meta0.ValueType
596
+ zodError?: ZodError
597
+ axiosError?: AxiosError
598
+ }
599
+
600
+ export type HttpStatusCodeString = keyof typeof HttpStatusCode
601
+ export type Error0Cause = Error | Error0 | unknown
602
+ export type ExpectedFn = (error: GeneralProps) => boolean | undefined
575
603
  export type JSON = ReturnType<Error0['toJSON']>
576
604
  export type Collection = Record<string, typeof Error0>
577
605
  }
578
-
579
- export const e0s = {
580
- Default: Error0,
581
- Expected: Error0.extend({
582
- defaultExpected: true,
583
- }),
584
- } satisfies Error0.Collection