@devp0nt/error0 1.0.0-next.50 → 1.0.0-next.51
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 +15 -15
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +11 -2
- package/dist/cjs/plugins/{cause-serialize.cjs → cause.cjs} +34 -12
- package/dist/cjs/plugins/cause.cjs.map +1 -0
- package/dist/cjs/plugins/cause.d.cts +15 -0
- package/dist/cjs/plugins/code.cjs +10 -2
- package/dist/cjs/plugins/code.cjs.map +1 -1
- package/dist/cjs/plugins/code.d.cts +2 -1
- package/dist/cjs/plugins/expected.cjs +19 -6
- package/dist/cjs/plugins/expected.cjs.map +1 -1
- package/dist/cjs/plugins/expected.d.cts +4 -3
- package/dist/cjs/plugins/meta.cjs +3 -3
- package/dist/cjs/plugins/meta.cjs.map +1 -1
- package/dist/cjs/plugins/meta.d.cts +2 -2
- package/dist/cjs/plugins/stack-merge.cjs +3 -3
- package/dist/cjs/plugins/stack-merge.cjs.map +1 -1
- package/dist/cjs/plugins/stack-merge.d.cts +2 -2
- package/dist/cjs/plugins/status.cjs +7 -1
- package/dist/cjs/plugins/status.cjs.map +1 -1
- package/dist/cjs/plugins/status.d.cts +2 -1
- package/dist/cjs/plugins/tags.cjs +3 -3
- package/dist/cjs/plugins/tags.cjs.map +1 -1
- package/dist/cjs/plugins/tags.d.cts +2 -2
- package/dist/esm/index.d.ts +11 -2
- package/dist/esm/index.js +15 -15
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugins/cause.d.ts +15 -0
- package/dist/esm/plugins/cause.js +39 -0
- package/dist/esm/plugins/cause.js.map +1 -0
- package/dist/esm/plugins/code.d.ts +2 -1
- package/dist/esm/plugins/code.js +10 -2
- package/dist/esm/plugins/code.js.map +1 -1
- package/dist/esm/plugins/expected.d.ts +4 -3
- package/dist/esm/plugins/expected.js +19 -6
- package/dist/esm/plugins/expected.js.map +1 -1
- package/dist/esm/plugins/meta.d.ts +2 -2
- package/dist/esm/plugins/meta.js +3 -3
- package/dist/esm/plugins/meta.js.map +1 -1
- package/dist/esm/plugins/stack-merge.d.ts +2 -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 +2 -1
- package/dist/esm/plugins/status.js +7 -1
- package/dist/esm/plugins/status.js.map +1 -1
- package/dist/esm/plugins/tags.d.ts +2 -2
- package/dist/esm/plugins/tags.js +3 -3
- package/dist/esm/plugins/tags.js.map +1 -1
- package/package.json +1 -1
- package/src/index.test.ts +9 -2
- package/src/index.ts +31 -16
- package/src/plugins/{cause-serialize.test.ts → cause.test.ts} +56 -3
- package/src/plugins/cause.ts +45 -0
- package/src/plugins/code.ts +10 -2
- package/src/plugins/expected.test.ts +22 -3
- package/src/plugins/expected.ts +23 -6
- package/src/plugins/meta.ts +3 -3
- package/src/plugins/stack-merge.test.ts +0 -7
- package/src/plugins/stack-merge.ts +4 -4
- package/src/plugins/status.ts +8 -2
- package/src/plugins/tags.ts +4 -4
- package/dist/cjs/plugins/cause-serialize.cjs.map +0 -1
- package/dist/cjs/plugins/cause-serialize.d.cts +0 -7
- package/dist/esm/plugins/cause-serialize.d.ts +0 -7
- package/dist/esm/plugins/cause-serialize.js +0 -17
- package/dist/esm/plugins/cause-serialize.js.map +0 -1
- package/src/plugins/cause-serialize.ts +0 -15
|
@@ -11,7 +11,7 @@ describe('expectedPlugin', () => {
|
|
|
11
11
|
})
|
|
12
12
|
|
|
13
13
|
it('can be used to control error tracker behavior', () => {
|
|
14
|
-
const AppError = Error0.use(statusPlugin).use(expectedPlugin({
|
|
14
|
+
const AppError = Error0.use(statusPlugin).use(expectedPlugin({ isPublic: true }))
|
|
15
15
|
const errorExpected = new AppError('test', { status: 400, expected: true })
|
|
16
16
|
const errorUnexpected = new AppError('test', { status: 400, expected: false })
|
|
17
17
|
const usualError = new Error('test')
|
|
@@ -30,7 +30,7 @@ describe('expectedPlugin', () => {
|
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
it('resolves to false when any cause has false', () => {
|
|
33
|
-
const AppError = Error0.use(expectedPlugin({
|
|
33
|
+
const AppError = Error0.use(expectedPlugin({ isPublic: true }))
|
|
34
34
|
const root = new AppError('root', { expected: true })
|
|
35
35
|
const middle = new AppError('middle', { expected: false, cause: root })
|
|
36
36
|
const leaf = new AppError('leaf', { expected: false, cause: middle })
|
|
@@ -39,9 +39,28 @@ describe('expectedPlugin', () => {
|
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
it('treats undefined expected as unexpected', () => {
|
|
42
|
-
const AppError = Error0.use(expectedPlugin({
|
|
42
|
+
const AppError = Error0.use(expectedPlugin({ isPublic: true }))
|
|
43
43
|
const error = new AppError('without expected')
|
|
44
44
|
expect(error.expected).toBe(false)
|
|
45
45
|
expect(error.isExpected()).toBe(false)
|
|
46
46
|
})
|
|
47
|
+
|
|
48
|
+
it('supports override', () => {
|
|
49
|
+
const AppError = Error0.use(statusPlugin).use(
|
|
50
|
+
expectedPlugin({
|
|
51
|
+
isPublic: true,
|
|
52
|
+
override: (error) => {
|
|
53
|
+
return error.message.includes('CRITICAL') ? false : undefined
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
)
|
|
57
|
+
const errorNonCritical = new AppError('USUAL: test', { status: 400 })
|
|
58
|
+
const errorNonCriticalExpected = new AppError('USUAL: test', { status: 400, expected: true })
|
|
59
|
+
const errorCritical = new AppError('CRITICAL: test', { status: 400 })
|
|
60
|
+
const errorCriticalExpected = new AppError('CRITICAL: test', { status: 400, expected: true })
|
|
61
|
+
expect(errorNonCritical.expected).toBe(false) // becouse expected was not provided at all
|
|
62
|
+
expect(errorNonCriticalExpected.expected).toBe(true)
|
|
63
|
+
expect(errorCritical.expected).toBe(false)
|
|
64
|
+
expect(errorCriticalExpected.expected).toBe(false)
|
|
65
|
+
})
|
|
47
66
|
})
|
package/src/plugins/expected.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { Error0 } from '../index.js'
|
|
2
2
|
|
|
3
|
-
const isExpected = (
|
|
3
|
+
const isExpected = ({
|
|
4
|
+
flow,
|
|
5
|
+
error,
|
|
6
|
+
override,
|
|
7
|
+
}: {
|
|
8
|
+
flow: unknown[]
|
|
9
|
+
error: Error0
|
|
10
|
+
override?: (error: any) => boolean | undefined
|
|
11
|
+
}) => {
|
|
12
|
+
if (override) {
|
|
13
|
+
const overridden = override(error)
|
|
14
|
+
if (overridden !== undefined) {
|
|
15
|
+
return overridden
|
|
16
|
+
}
|
|
17
|
+
}
|
|
4
18
|
let expected = false
|
|
5
19
|
for (const value of flow) {
|
|
6
20
|
if (value === false) {
|
|
@@ -13,13 +27,16 @@ const isExpected = (flow: unknown[]) => {
|
|
|
13
27
|
return expected
|
|
14
28
|
}
|
|
15
29
|
|
|
16
|
-
export const expectedPlugin =
|
|
30
|
+
export const expectedPlugin = <TError extends Error0>({
|
|
31
|
+
isPublic = false,
|
|
32
|
+
override,
|
|
33
|
+
}: { isPublic?: boolean; override?: (error: TError) => boolean | undefined } = {}) =>
|
|
17
34
|
Error0.plugin()
|
|
18
35
|
.prop('expected', {
|
|
19
36
|
init: (input: boolean) => input,
|
|
20
|
-
resolve: ({ flow }) => isExpected(flow),
|
|
21
|
-
serialize: ({ resolved, isPublic }) => {
|
|
22
|
-
if (
|
|
37
|
+
resolve: ({ flow, error }) => isExpected({ flow, error, override }),
|
|
38
|
+
serialize: ({ resolved, isPublic: _isPublic }) => {
|
|
39
|
+
if (isPublic && _isPublic) {
|
|
23
40
|
return undefined
|
|
24
41
|
}
|
|
25
42
|
return resolved
|
|
@@ -27,5 +44,5 @@ export const expectedPlugin = ({ hideWhenPublic = true }: { hideWhenPublic?: boo
|
|
|
27
44
|
deserialize: ({ value }) => (typeof value === 'boolean' ? value : undefined),
|
|
28
45
|
})
|
|
29
46
|
.method('isExpected', (error) => {
|
|
30
|
-
return isExpected(error.flow('expected'))
|
|
47
|
+
return isExpected({ flow: error.flow('expected'), error, override })
|
|
31
48
|
})
|
package/src/plugins/meta.ts
CHANGED
|
@@ -28,7 +28,7 @@ const toJsonSafe = (input: unknown): Json | undefined => {
|
|
|
28
28
|
const isMetaRecord = (value: unknown): value is Record<string, unknown> =>
|
|
29
29
|
typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
30
30
|
|
|
31
|
-
export const metaPlugin = ({
|
|
31
|
+
export const metaPlugin = ({ isPublic = false }: { isPublic?: boolean } = {}) =>
|
|
32
32
|
Error0.plugin().prop('meta', {
|
|
33
33
|
init: (input: Record<string, unknown>) => input,
|
|
34
34
|
resolve: ({ flow }) => {
|
|
@@ -44,8 +44,8 @@ export const metaPlugin = ({ hideWhenPublic = true }: { hideWhenPublic?: boolean
|
|
|
44
44
|
}
|
|
45
45
|
return merged
|
|
46
46
|
},
|
|
47
|
-
serialize: ({ resolved, isPublic }) => {
|
|
48
|
-
if (
|
|
47
|
+
serialize: ({ resolved, isPublic: _isPublic }) => {
|
|
48
|
+
if (!isPublic && _isPublic) {
|
|
49
49
|
return undefined
|
|
50
50
|
}
|
|
51
51
|
return toJsonSafe(resolved)
|
|
@@ -54,11 +54,4 @@ describe('stackMergePlugin', () => {
|
|
|
54
54
|
`)
|
|
55
55
|
expect(mergedStack2Public).toBeUndefined()
|
|
56
56
|
})
|
|
57
|
-
|
|
58
|
-
it('by default serializes stack of this error only', () => {
|
|
59
|
-
const AppError = Error0.use(statusPlugin).use(codePlugin).use(stackMergePlugin())
|
|
60
|
-
const error = new AppError('test', { status: 400, code: 'NOT_FOUND' })
|
|
61
|
-
const json = AppError.serialize(error, false)
|
|
62
|
-
expect(json.stack).toBe(error.stack)
|
|
63
|
-
})
|
|
64
57
|
})
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Error0 } from '../index.js'
|
|
2
2
|
|
|
3
3
|
export const stackMergePlugin = ({
|
|
4
|
-
|
|
4
|
+
isPublic = false,
|
|
5
5
|
delimiter = '\n',
|
|
6
|
-
}: {
|
|
6
|
+
}: { isPublic?: boolean; delimiter?: string } = {}) =>
|
|
7
7
|
Error0.plugin().stack({
|
|
8
|
-
serialize: ({ error, isPublic }) => {
|
|
9
|
-
if (
|
|
8
|
+
serialize: ({ error, isPublic: _isPublic }) => {
|
|
9
|
+
if (!isPublic && _isPublic) {
|
|
10
10
|
return undefined
|
|
11
11
|
}
|
|
12
12
|
return error
|
package/src/plugins/status.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Error0 } from '../index.js'
|
|
2
2
|
|
|
3
3
|
export const statusPlugin = <TStatuses extends Record<string, number> = Record<never, number>>({
|
|
4
|
+
isPublic = false,
|
|
4
5
|
statuses,
|
|
5
6
|
strict = false,
|
|
6
|
-
}: { statuses?: TStatuses; strict?: boolean } = {}) => {
|
|
7
|
+
}: { isPublic?: boolean; statuses?: TStatuses; strict?: boolean } = {}) => {
|
|
7
8
|
const statusValues = statuses ? Object.values(statuses) : undefined
|
|
8
9
|
const isStatusValue = (value: unknown): value is number =>
|
|
9
10
|
typeof value === 'number' && (!statusValues || !strict || statusValues.includes(value))
|
|
@@ -23,7 +24,12 @@ export const statusPlugin = <TStatuses extends Record<string, number> = Record<n
|
|
|
23
24
|
return Error0.plugin().prop('status', {
|
|
24
25
|
init: (status: number | Extract<keyof TStatuses, string>) => convertStatusValue(status),
|
|
25
26
|
resolve: ({ flow }) => flow.find(Boolean),
|
|
26
|
-
serialize: ({ resolved }) =>
|
|
27
|
+
serialize: ({ resolved, isPublic: _isPublic }) => {
|
|
28
|
+
if (!isPublic && _isPublic) {
|
|
29
|
+
return undefined
|
|
30
|
+
}
|
|
31
|
+
return resolved
|
|
32
|
+
},
|
|
27
33
|
deserialize: ({ value }) => (typeof value === 'number' ? value : undefined),
|
|
28
34
|
})
|
|
29
35
|
}
|
package/src/plugins/tags.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Error0 } from '../index.js'
|
|
2
2
|
|
|
3
3
|
export const tagsPlugin = <TTag extends string>({
|
|
4
|
-
|
|
4
|
+
isPublic = false,
|
|
5
5
|
tags,
|
|
6
6
|
strict = true,
|
|
7
|
-
}: {
|
|
7
|
+
}: { isPublic?: boolean; tags?: TTag[] | readonly TTag[]; strict?: boolean } = {}) => {
|
|
8
8
|
function hasTag(error: Error0, tag: TTag): boolean
|
|
9
9
|
function hasTag(error: Error0, tag: TTag[], policy: 'every' | 'some'): boolean
|
|
10
10
|
function hasTag(error: Error0, tag: TTag | TTag[], policy?: 'every' | 'some'): boolean {
|
|
@@ -34,8 +34,8 @@ export const tagsPlugin = <TTag extends string>({
|
|
|
34
34
|
}
|
|
35
35
|
return merged.length > 0 ? Array.from(new Set(merged)) : undefined
|
|
36
36
|
},
|
|
37
|
-
serialize: ({ resolved, isPublic }) => {
|
|
38
|
-
if (
|
|
37
|
+
serialize: ({ resolved, isPublic: _isPublic }) => {
|
|
38
|
+
if (!isPublic && _isPublic) {
|
|
39
39
|
return undefined
|
|
40
40
|
}
|
|
41
41
|
return resolved
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/plugins/cause-serialize.ts"],"sourcesContent":["import { Error0 } from '../index.js'\n\nexport const causeSerializePlugin = ({ hideWhenPublic = true }: { hideWhenPublic?: boolean } = {}) =>\n Error0.plugin().cause({\n serialize: ({ value, error, isPublic }) => {\n if (hideWhenPublic && isPublic) {\n return undefined\n }\n const ctor = error.constructor as typeof Error0\n if (ctor.is(value)) {\n return ctor.serialize(value, isPublic)\n }\n return undefined\n },\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAuB;AAEhB,MAAM,uBAAuB,CAAC,EAAE,iBAAiB,KAAK,IAAkC,CAAC,MAC9F,gBAAO,OAAO,EAAE,MAAM;AAAA,EACpB,WAAW,CAAC,EAAE,OAAO,OAAO,SAAS,MAAM;AACzC,QAAI,kBAAkB,UAAU;AAC9B,aAAO;AAAA,IACT;AACA,UAAM,OAAO,MAAM;AACnB,QAAI,KAAK,GAAG,KAAK,GAAG;AAClB,aAAO,KAAK,UAAU,OAAO,QAAQ;AAAA,IACvC;AACA,WAAO;AAAA,EACT;AACF,CAAC;","names":[]}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Error0 } from "../index.js";
|
|
2
|
-
const causeSerializePlugin = ({ hideWhenPublic = true } = {}) => Error0.plugin().cause({
|
|
3
|
-
serialize: ({ value, error, isPublic }) => {
|
|
4
|
-
if (hideWhenPublic && isPublic) {
|
|
5
|
-
return void 0;
|
|
6
|
-
}
|
|
7
|
-
const ctor = error.constructor;
|
|
8
|
-
if (ctor.is(value)) {
|
|
9
|
-
return ctor.serialize(value, isPublic);
|
|
10
|
-
}
|
|
11
|
-
return void 0;
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
export {
|
|
15
|
-
causeSerializePlugin
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=cause-serialize.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/plugins/cause-serialize.ts"],"sourcesContent":["import { Error0 } from '../index.js'\n\nexport const causeSerializePlugin = ({ hideWhenPublic = true }: { hideWhenPublic?: boolean } = {}) =>\n Error0.plugin().cause({\n serialize: ({ value, error, isPublic }) => {\n if (hideWhenPublic && isPublic) {\n return undefined\n }\n const ctor = error.constructor as typeof Error0\n if (ctor.is(value)) {\n return ctor.serialize(value, isPublic)\n }\n return undefined\n },\n })\n"],"mappings":"AAAA,SAAS,cAAc;AAEhB,MAAM,uBAAuB,CAAC,EAAE,iBAAiB,KAAK,IAAkC,CAAC,MAC9F,OAAO,OAAO,EAAE,MAAM;AAAA,EACpB,WAAW,CAAC,EAAE,OAAO,OAAO,SAAS,MAAM;AACzC,QAAI,kBAAkB,UAAU;AAC9B,aAAO;AAAA,IACT;AACA,UAAM,OAAO,MAAM;AACnB,QAAI,KAAK,GAAG,KAAK,GAAG;AAClB,aAAO,KAAK,UAAU,OAAO,QAAQ;AAAA,IACvC;AACA,WAAO;AAAA,EACT;AACF,CAAC;","names":[]}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Error0 } from '../index.js'
|
|
2
|
-
|
|
3
|
-
export const causeSerializePlugin = ({ hideWhenPublic = true }: { hideWhenPublic?: boolean } = {}) =>
|
|
4
|
-
Error0.plugin().cause({
|
|
5
|
-
serialize: ({ value, error, isPublic }) => {
|
|
6
|
-
if (hideWhenPublic && isPublic) {
|
|
7
|
-
return undefined
|
|
8
|
-
}
|
|
9
|
-
const ctor = error.constructor as typeof Error0
|
|
10
|
-
if (ctor.is(value)) {
|
|
11
|
-
return ctor.serialize(value, isPublic)
|
|
12
|
-
}
|
|
13
|
-
return undefined
|
|
14
|
-
},
|
|
15
|
-
})
|