@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.
- package/dist/cjs/index.cjs +2 -8
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -50
- package/dist/cjs/plugins/flat-original.cjs +39 -0
- package/dist/cjs/plugins/flat-original.cjs.map +1 -0
- package/dist/cjs/plugins/flat-original.d.cts +7 -0
- package/dist/cjs/plugins/tags.cjs +13 -14
- package/dist/cjs/plugins/tags.cjs.map +1 -1
- package/dist/cjs/plugins/tags.d.cts +30 -4
- package/dist/esm/index.d.ts +2 -50
- package/dist/esm/index.js +2 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugins/flat-original.d.ts +7 -0
- package/dist/esm/plugins/flat-original.js +15 -0
- package/dist/esm/plugins/flat-original.js.map +1 -0
- package/dist/esm/plugins/tags.d.ts +30 -4
- package/dist/esm/plugins/tags.js +13 -14
- package/dist/esm/plugins/tags.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +74 -75
- package/src/plugins/flat-original.test.ts +45 -0
- package/src/plugins/flat-original.ts +12 -0
- package/src/plugins/tags.test.ts +0 -2
- package/src/plugins/tags.ts +28 -16
|
@@ -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
|
+
}
|
package/src/plugins/tags.test.ts
CHANGED
|
@@ -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
|
})
|
package/src/plugins/tags.ts
CHANGED
|
@@ -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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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',
|
|
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
|
}
|