@devp0nt/error0 1.0.0-next.51 → 1.0.0-next.53

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.
@@ -0,0 +1,45 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { Error0 } from '../index.js'
3
+ import { flatOriginalPlugin } from './flat-original.js'
4
+
5
+ describe('flatOriginalPlugin', () => {
6
+ it('without plugin original error comes to cause', () => {
7
+ const usualError = new Error('another error')
8
+ const error = Error0.from(usualError)
9
+ expect(error).toBeInstanceOf(Error0)
10
+ expect(error).toBeInstanceOf(Error)
11
+ expect(error.name).toBe('Error0')
12
+ expect(error.cause).not.toBeInstanceOf(Error0)
13
+ expect(error.cause).toBeInstanceOf(Error)
14
+ expect(error.cause).toBe(usualError)
15
+ expect((error.cause as any).name).toBe('Error')
16
+ expect(error.causes()).toEqual([error, usualError])
17
+ })
18
+
19
+ it('with plugin original error becomes error0 itself', () => {
20
+ const usualError = new Error('another error')
21
+ const AppError = Error0.use(flatOriginalPlugin())
22
+ const error = AppError.from(usualError)
23
+ expect(error).toBeInstanceOf(AppError)
24
+ expect(error).toBeInstanceOf(Error0)
25
+ expect(error).toBeInstanceOf(Error)
26
+ expect(error.message).toBe(usualError.message)
27
+ expect(error.stack).toBe(usualError.stack)
28
+ expect(error.name).toBe('Error0')
29
+ expect(error.cause).toBeUndefined()
30
+ })
31
+
32
+ it('with plugin original error becomes error0 itself but keep it own causes', () => {
33
+ const causeError = new Error('cause error')
34
+ const usualError = new Error('another error', { cause: causeError })
35
+ const AppError = Error0.use(flatOriginalPlugin())
36
+ const error = AppError.from(usualError)
37
+ expect(error).toBeInstanceOf(AppError)
38
+ expect(error).toBeInstanceOf(Error0)
39
+ expect(error).toBeInstanceOf(Error)
40
+ expect(error.message).toBe(usualError.message)
41
+ expect(error.stack).toBe(usualError.stack)
42
+ expect(error.name).toBe('Error0')
43
+ expect(error.causes()).toEqual([error, causeError])
44
+ })
45
+ })
@@ -0,0 +1,12 @@
1
+ import { Error0 } from '../index.js'
2
+
3
+ export const flatOriginalPlugin = ({ prefix }: { prefix?: string } = {}) => {
4
+ return Error0.plugin().adapt((error) => {
5
+ const cause = error.cause
6
+ if (cause instanceof Error && cause.constructor === Error) {
7
+ error.cause = cause.cause
8
+ error.message = `${prefix ?? ''}${cause.message}`
9
+ error.stack = cause.stack
10
+ }
11
+ })
12
+ }
@@ -66,8 +66,6 @@ describe('tagsPlugin', () => {
66
66
 
67
67
  // @ts-expect-error - unknown tag is not part of allow-list
68
68
  error.hasTag('custom')
69
- // @ts-expect-error - array checks require policy argument
70
- error.hasTag(['api', 'db'])
71
69
  // @ts-expect-error - unsupported policy
72
70
  error.hasTag(['api', 'db'], 'all')
73
71
  })
@@ -5,21 +5,21 @@ export const tagsPlugin = <TTag extends string>({
5
5
  tags,
6
6
  strict = true,
7
7
  }: { isPublic?: boolean; tags?: TTag[] | readonly TTag[]; strict?: boolean } = {}) => {
8
- function hasTag(error: Error0, tag: TTag): boolean
9
- function hasTag(error: Error0, tag: TTag[], policy: 'every' | 'some'): boolean
10
- function hasTag(error: Error0, tag: TTag | TTag[], policy?: 'every' | 'some'): boolean {
11
- const tags = (error as any).tags as string[] | undefined
12
- if (!tags) {
13
- return false
14
- }
15
- if (Array.isArray(tag)) {
16
- if (policy === 'every') {
17
- return tag.every((item) => tags.includes(item))
18
- }
19
- return tag.some((item) => tags.includes(item))
20
- }
21
- return tags.includes(tag)
22
- }
8
+ // function hasTag(error: Error0, tag: TTag): boolean
9
+ // function hasTag(error: Error0, tag: TTag[], policy: 'every' | 'some'): boolean
10
+ // function hasTag(error: Error0, tag: TTag | TTag[], policy?: 'every' | 'some'): boolean {
11
+ // const tags = (error as any).tags as string[] | undefined
12
+ // if (!tags) {
13
+ // return false
14
+ // }
15
+ // if (Array.isArray(tag)) {
16
+ // if (policy === 'every') {
17
+ // return tag.every((item) => tags.includes(item))
18
+ // }
19
+ // return tag.some((item) => tags.includes(item))
20
+ // }
21
+ // return tags.includes(tag)
22
+ // }
23
23
  const isTag = (value: unknown): value is TTag =>
24
24
  typeof value === 'string' && (!tags || !strict || tags.includes(value as TTag))
25
25
  return Error0.plugin()
@@ -47,5 +47,17 @@ export const tagsPlugin = <TTag extends string>({
47
47
  return value.filter((item) => isTag(item))
48
48
  },
49
49
  })
50
- .method('hasTag', hasTag)
50
+ .method('hasTag', (error, tag: TTag | TTag[], policy: 'every' | 'some' = 'every'): boolean => {
51
+ const tags = error.tags
52
+ if (!tags) {
53
+ return false
54
+ }
55
+ if (Array.isArray(tag)) {
56
+ if (policy === 'every') {
57
+ return tag.every((item) => tags.includes(item))
58
+ }
59
+ return tag.some((item) => tags.includes(item))
60
+ }
61
+ return tags.includes(tag)
62
+ })
51
63
  }