@daihum/error-state 0.1.0 → 0.1.1
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/index.cjs +84 -0
- package/dist/index.d.cts +92 -0
- package/dist/index.d.ts +76 -77
- package/dist/index.js +77 -58
- package/package.json +8 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
const ERROR_STATE_SCHEMA_V0 = "carrel.runtime.error.v0";
|
|
4
|
+
function buildInvalidDetailsMessage(tag, error) {
|
|
5
|
+
return error.message ?? `Invalid error-state details for ${tag}.`;
|
|
6
|
+
}
|
|
7
|
+
function hasOwn(value, key) {
|
|
8
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
9
|
+
}
|
|
10
|
+
function defineErrorKinds(namespace, constSpec) {
|
|
11
|
+
const kinds = Object.freeze(Object.fromEntries(Object.entries(constSpec).map(([key, spec]) => [key, Object.freeze({
|
|
12
|
+
tag: `${namespace}.${key}`,
|
|
13
|
+
retryable: spec.retryable,
|
|
14
|
+
...spec.reason !== void 0 ? { reason: spec.reason } : {},
|
|
15
|
+
...spec.message !== void 0 ? { message: spec.message } : {},
|
|
16
|
+
...spec.details !== void 0 ? { details: spec.details } : {}
|
|
17
|
+
})])));
|
|
18
|
+
function tryFail(key, input = {}) {
|
|
19
|
+
const kind = kinds[key];
|
|
20
|
+
const reason = input.reason ?? kind.reason;
|
|
21
|
+
const message = input.message ?? kind.message;
|
|
22
|
+
const stateBase = {
|
|
23
|
+
schema: ERROR_STATE_SCHEMA_V0,
|
|
24
|
+
tag: kind.tag,
|
|
25
|
+
retryable: kind.retryable,
|
|
26
|
+
...reason !== void 0 ? { reason } : {},
|
|
27
|
+
...message !== void 0 ? { message } : {},
|
|
28
|
+
...input.commandId !== void 0 ? { commandId: input.commandId } : {},
|
|
29
|
+
...input.runId !== void 0 ? { runId: input.runId } : {},
|
|
30
|
+
...input.stepId !== void 0 ? { stepId: input.stepId } : {},
|
|
31
|
+
...input.correlationId !== void 0 ? { correlationId: input.correlationId } : {}
|
|
32
|
+
};
|
|
33
|
+
if (!hasOwn(input, "details")) return {
|
|
34
|
+
ok: true,
|
|
35
|
+
value: stateBase
|
|
36
|
+
};
|
|
37
|
+
const rawDetails = input.details;
|
|
38
|
+
if (kind.details !== void 0) {
|
|
39
|
+
const result = kind.details.validate(rawDetails);
|
|
40
|
+
if (!result.ok) {
|
|
41
|
+
const state = {
|
|
42
|
+
...stateBase,
|
|
43
|
+
reason: "invalid-error-details",
|
|
44
|
+
message: buildInvalidDetailsMessage(kind.tag, result.error)
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
ok: false,
|
|
48
|
+
error: result.error,
|
|
49
|
+
state
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
ok: true,
|
|
54
|
+
value: {
|
|
55
|
+
...stateBase,
|
|
56
|
+
details: result.value
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
ok: true,
|
|
62
|
+
value: {
|
|
63
|
+
...stateBase,
|
|
64
|
+
details: rawDetails
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return Object.freeze({
|
|
69
|
+
namespace,
|
|
70
|
+
kinds,
|
|
71
|
+
fail(key, input) {
|
|
72
|
+
const result = tryFail(key, input);
|
|
73
|
+
return result.ok ? result.value : result.state;
|
|
74
|
+
},
|
|
75
|
+
tryFail,
|
|
76
|
+
contribute(registry) {
|
|
77
|
+
return registry.register(namespace, kinds);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
exports.ERROR_STATE_SCHEMA_V0 = ERROR_STATE_SCHEMA_V0;
|
|
84
|
+
exports.defineErrorKinds = defineErrorKinds;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
declare const ERROR_STATE_SCHEMA_V0: "carrel.runtime.error.v0";
|
|
3
|
+
interface ErrorStateV0<Tag$1 extends string = string, Details$1 = unknown> {
|
|
4
|
+
readonly schema: typeof ERROR_STATE_SCHEMA_V0;
|
|
5
|
+
readonly tag: Tag$1;
|
|
6
|
+
readonly retryable: boolean;
|
|
7
|
+
readonly reason?: string;
|
|
8
|
+
readonly message?: string;
|
|
9
|
+
readonly commandId?: string;
|
|
10
|
+
readonly runId?: string;
|
|
11
|
+
readonly stepId?: string;
|
|
12
|
+
readonly correlationId?: string;
|
|
13
|
+
readonly details?: Details$1;
|
|
14
|
+
}
|
|
15
|
+
interface SchemaPortErrorV0 {
|
|
16
|
+
readonly message?: string;
|
|
17
|
+
readonly issues?: readonly unknown[];
|
|
18
|
+
readonly cause?: unknown;
|
|
19
|
+
}
|
|
20
|
+
type SchemaPortResultV0<T> = {
|
|
21
|
+
readonly ok: true;
|
|
22
|
+
readonly value: T;
|
|
23
|
+
} | {
|
|
24
|
+
readonly ok: false;
|
|
25
|
+
readonly error: SchemaPortErrorV0;
|
|
26
|
+
};
|
|
27
|
+
interface SchemaPort<T> {
|
|
28
|
+
validate(value: unknown): SchemaPortResultV0<T>;
|
|
29
|
+
}
|
|
30
|
+
interface ErrorKindSpecV0<Details$1 = never> {
|
|
31
|
+
readonly retryable: boolean;
|
|
32
|
+
readonly reason?: string;
|
|
33
|
+
readonly message?: string;
|
|
34
|
+
readonly details?: SchemaPort<Details$1>;
|
|
35
|
+
}
|
|
36
|
+
interface ErrorKindDefinitionV0<Tag$1 extends string = string, Details$1 = never, Retryable$1 extends boolean = boolean> {
|
|
37
|
+
readonly tag: Tag$1;
|
|
38
|
+
readonly retryable: Retryable$1;
|
|
39
|
+
readonly reason?: string;
|
|
40
|
+
readonly message?: string;
|
|
41
|
+
readonly details?: SchemaPort<Details$1>;
|
|
42
|
+
}
|
|
43
|
+
type ErrorKindSpecMapV0 = Readonly<Record<string, ErrorKindSpecV0<unknown>>>;
|
|
44
|
+
type ErrorKindDefinitionsV0 = Readonly<Record<string, ErrorKindDefinitionV0<string, unknown, boolean>>>;
|
|
45
|
+
interface ErrorKindRegistryPortV0<Definitions extends ErrorKindDefinitionsV0 = ErrorKindDefinitionsV0> {
|
|
46
|
+
register(namespace: string, spec: Definitions): void | Promise<void>;
|
|
47
|
+
lookup(tag: string): ErrorKindDefinitionV0 | undefined | Promise<ErrorKindDefinitionV0 | undefined>;
|
|
48
|
+
}
|
|
49
|
+
type ErrorKindDetailsOf<Spec> = Spec extends {
|
|
50
|
+
readonly details: SchemaPort<infer Details>;
|
|
51
|
+
} ? Details : never;
|
|
52
|
+
type ErrorKindRetryableOf<Spec> = Spec extends {
|
|
53
|
+
readonly retryable: infer Retryable extends boolean;
|
|
54
|
+
} ? Retryable : boolean;
|
|
55
|
+
type ErrorKindTag<Namespace extends string, Key$1 extends string> = `${Namespace}.${Key$1}`;
|
|
56
|
+
type ErrorKindDefinitionsFor<Namespace extends string, Spec extends ErrorKindSpecMapV0> = Readonly<{ [Key in keyof Spec & string]: ErrorKindDefinitionV0<ErrorKindTag<Namespace, Key>, ErrorKindDetailsOf<Spec[Key]>, ErrorKindRetryableOf<Spec[Key]>> }>;
|
|
57
|
+
type ErrorStateForKind<Namespace extends string, Key$1 extends string, Spec> = ErrorStateV0<ErrorKindTag<Namespace, Key$1>, ErrorKindDetailsOf<Spec>> & {
|
|
58
|
+
readonly retryable: ErrorKindRetryableOf<Spec>;
|
|
59
|
+
};
|
|
60
|
+
type ErrorStateOf<Kinds extends ErrorKindDefinitionsV0> = { [Key in keyof Kinds]: Kinds[Key] extends ErrorKindDefinitionV0<infer Tag, infer Details, infer Retryable> ? ErrorStateV0<Tag, Details> & {
|
|
61
|
+
readonly retryable: Retryable;
|
|
62
|
+
} : never }[keyof Kinds];
|
|
63
|
+
type ErrorStateFailInputV0<Details$1 = never> = {
|
|
64
|
+
readonly reason?: string;
|
|
65
|
+
readonly message?: string;
|
|
66
|
+
readonly commandId?: string;
|
|
67
|
+
readonly runId?: string;
|
|
68
|
+
readonly stepId?: string;
|
|
69
|
+
readonly correlationId?: string;
|
|
70
|
+
} & ([Details$1] extends [never] ? {
|
|
71
|
+
readonly details?: never;
|
|
72
|
+
} : {
|
|
73
|
+
readonly details?: Details$1;
|
|
74
|
+
});
|
|
75
|
+
type ErrorStateBuildResultV0<State extends ErrorStateV0> = {
|
|
76
|
+
readonly ok: true;
|
|
77
|
+
readonly value: State;
|
|
78
|
+
} | {
|
|
79
|
+
readonly ok: false;
|
|
80
|
+
readonly error: SchemaPortErrorV0;
|
|
81
|
+
readonly state: State;
|
|
82
|
+
};
|
|
83
|
+
interface DefinedErrorKindsV0<Namespace extends string, Spec extends ErrorKindSpecMapV0> {
|
|
84
|
+
readonly namespace: Namespace;
|
|
85
|
+
readonly kinds: ErrorKindDefinitionsFor<Namespace, Spec>;
|
|
86
|
+
fail<Key$1 extends keyof Spec & string>(tag: Key$1, input?: ErrorStateFailInputV0<ErrorKindDetailsOf<Spec[Key$1]>>): ErrorStateForKind<Namespace, Key$1, Spec[Key$1]>;
|
|
87
|
+
tryFail<Key$1 extends keyof Spec & string>(tag: Key$1, input?: ErrorStateFailInputV0<ErrorKindDetailsOf<Spec[Key$1]>>): ErrorStateBuildResultV0<ErrorStateForKind<Namespace, Key$1, Spec[Key$1]>>;
|
|
88
|
+
contribute(registry: ErrorKindRegistryPortV0<ErrorKindDefinitionsFor<Namespace, Spec>>): void | Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
declare function defineErrorKinds<const Namespace extends string, const Spec extends ErrorKindSpecMapV0>(namespace: Namespace, constSpec: Spec): DefinedErrorKindsV0<Namespace, Spec>;
|
|
91
|
+
//#endregion
|
|
92
|
+
export { DefinedErrorKindsV0, ERROR_STATE_SCHEMA_V0, ErrorKindDefinitionV0, ErrorKindDefinitionsFor, ErrorKindDefinitionsV0, ErrorKindDetailsOf, ErrorKindRegistryPortV0, ErrorKindRetryableOf, ErrorKindSpecMapV0, ErrorKindSpecV0, ErrorKindTag, ErrorStateBuildResultV0, ErrorStateFailInputV0, ErrorStateForKind, ErrorStateOf, ErrorStateV0, SchemaPort, SchemaPortErrorV0, SchemaPortResultV0, defineErrorKinds };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,93 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
declare const ERROR_STATE_SCHEMA_V0: "carrel.runtime.error.v0";
|
|
3
|
+
interface ErrorStateV0<Tag$1 extends string = string, Details$1 = unknown> {
|
|
4
|
+
readonly schema: typeof ERROR_STATE_SCHEMA_V0;
|
|
5
|
+
readonly tag: Tag$1;
|
|
6
|
+
readonly retryable: boolean;
|
|
7
|
+
readonly reason?: string;
|
|
8
|
+
readonly message?: string;
|
|
9
|
+
readonly commandId?: string;
|
|
10
|
+
readonly runId?: string;
|
|
11
|
+
readonly stepId?: string;
|
|
12
|
+
readonly correlationId?: string;
|
|
13
|
+
readonly details?: Details$1;
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
interface SchemaPortErrorV0 {
|
|
16
|
+
readonly message?: string;
|
|
17
|
+
readonly issues?: readonly unknown[];
|
|
18
|
+
readonly cause?: unknown;
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
type SchemaPortResultV0<T> = {
|
|
21
|
+
readonly ok: true;
|
|
22
|
+
readonly value: T;
|
|
22
23
|
} | {
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
readonly ok: false;
|
|
25
|
+
readonly error: SchemaPortErrorV0;
|
|
25
26
|
};
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
interface SchemaPort<T> {
|
|
28
|
+
validate(value: unknown): SchemaPortResultV0<T>;
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
interface ErrorKindSpecV0<Details$1 = never> {
|
|
31
|
+
readonly retryable: boolean;
|
|
32
|
+
readonly reason?: string;
|
|
33
|
+
readonly message?: string;
|
|
34
|
+
readonly details?: SchemaPort<Details$1>;
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
interface ErrorKindDefinitionV0<Tag$1 extends string = string, Details$1 = never, Retryable$1 extends boolean = boolean> {
|
|
37
|
+
readonly tag: Tag$1;
|
|
38
|
+
readonly retryable: Retryable$1;
|
|
39
|
+
readonly reason?: string;
|
|
40
|
+
readonly message?: string;
|
|
41
|
+
readonly details?: SchemaPort<Details$1>;
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
type ErrorKindSpecMapV0 = Readonly<Record<string, ErrorKindSpecV0<unknown>>>;
|
|
44
|
+
type ErrorKindDefinitionsV0 = Readonly<Record<string, ErrorKindDefinitionV0<string, unknown, boolean>>>;
|
|
45
|
+
interface ErrorKindRegistryPortV0<Definitions extends ErrorKindDefinitionsV0 = ErrorKindDefinitionsV0> {
|
|
46
|
+
register(namespace: string, spec: Definitions): void | Promise<void>;
|
|
47
|
+
lookup(tag: string): ErrorKindDefinitionV0 | undefined | Promise<ErrorKindDefinitionV0 | undefined>;
|
|
47
48
|
}
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
type ErrorKindDetailsOf<Spec> = Spec extends {
|
|
50
|
+
readonly details: SchemaPort<infer Details>;
|
|
50
51
|
} ? Details : never;
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
type ErrorKindRetryableOf<Spec> = Spec extends {
|
|
53
|
+
readonly retryable: infer Retryable extends boolean;
|
|
53
54
|
} ? Retryable : boolean;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
export type ErrorStateForKind<Namespace extends string, Key extends string, Spec> = ErrorStateV0<ErrorKindTag<Namespace, Key>, ErrorKindDetailsOf<Spec>> & {
|
|
59
|
-
readonly retryable: ErrorKindRetryableOf<Spec>;
|
|
55
|
+
type ErrorKindTag<Namespace extends string, Key$1 extends string> = `${Namespace}.${Key$1}`;
|
|
56
|
+
type ErrorKindDefinitionsFor<Namespace extends string, Spec extends ErrorKindSpecMapV0> = Readonly<{ [Key in keyof Spec & string]: ErrorKindDefinitionV0<ErrorKindTag<Namespace, Key>, ErrorKindDetailsOf<Spec[Key]>, ErrorKindRetryableOf<Spec[Key]>> }>;
|
|
57
|
+
type ErrorStateForKind<Namespace extends string, Key$1 extends string, Spec> = ErrorStateV0<ErrorKindTag<Namespace, Key$1>, ErrorKindDetailsOf<Spec>> & {
|
|
58
|
+
readonly retryable: ErrorKindRetryableOf<Spec>;
|
|
60
59
|
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
} & ([Details] extends [never] ? {
|
|
74
|
-
readonly details?: never;
|
|
60
|
+
type ErrorStateOf<Kinds extends ErrorKindDefinitionsV0> = { [Key in keyof Kinds]: Kinds[Key] extends ErrorKindDefinitionV0<infer Tag, infer Details, infer Retryable> ? ErrorStateV0<Tag, Details> & {
|
|
61
|
+
readonly retryable: Retryable;
|
|
62
|
+
} : never }[keyof Kinds];
|
|
63
|
+
type ErrorStateFailInputV0<Details$1 = never> = {
|
|
64
|
+
readonly reason?: string;
|
|
65
|
+
readonly message?: string;
|
|
66
|
+
readonly commandId?: string;
|
|
67
|
+
readonly runId?: string;
|
|
68
|
+
readonly stepId?: string;
|
|
69
|
+
readonly correlationId?: string;
|
|
70
|
+
} & ([Details$1] extends [never] ? {
|
|
71
|
+
readonly details?: never;
|
|
75
72
|
} : {
|
|
76
|
-
|
|
73
|
+
readonly details?: Details$1;
|
|
77
74
|
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
75
|
+
type ErrorStateBuildResultV0<State extends ErrorStateV0> = {
|
|
76
|
+
readonly ok: true;
|
|
77
|
+
readonly value: State;
|
|
81
78
|
} | {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
79
|
+
readonly ok: false;
|
|
80
|
+
readonly error: SchemaPortErrorV0;
|
|
81
|
+
readonly state: State;
|
|
85
82
|
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
interface DefinedErrorKindsV0<Namespace extends string, Spec extends ErrorKindSpecMapV0> {
|
|
84
|
+
readonly namespace: Namespace;
|
|
85
|
+
readonly kinds: ErrorKindDefinitionsFor<Namespace, Spec>;
|
|
86
|
+
fail<Key$1 extends keyof Spec & string>(tag: Key$1, input?: ErrorStateFailInputV0<ErrorKindDetailsOf<Spec[Key$1]>>): ErrorStateForKind<Namespace, Key$1, Spec[Key$1]>;
|
|
87
|
+
tryFail<Key$1 extends keyof Spec & string>(tag: Key$1, input?: ErrorStateFailInputV0<ErrorKindDetailsOf<Spec[Key$1]>>): ErrorStateBuildResultV0<ErrorStateForKind<Namespace, Key$1, Spec[Key$1]>>;
|
|
88
|
+
contribute(registry: ErrorKindRegistryPortV0<ErrorKindDefinitionsFor<Namespace, Spec>>): void | Promise<void>;
|
|
92
89
|
}
|
|
93
|
-
|
|
90
|
+
declare function defineErrorKinds<const Namespace extends string, const Spec extends ErrorKindSpecMapV0>(namespace: Namespace, constSpec: Spec): DefinedErrorKindsV0<Namespace, Spec>;
|
|
91
|
+
//#endregion
|
|
92
|
+
export { DefinedErrorKindsV0, ERROR_STATE_SCHEMA_V0, ErrorKindDefinitionV0, ErrorKindDefinitionsFor, ErrorKindDefinitionsV0, ErrorKindDetailsOf, ErrorKindRegistryPortV0, ErrorKindRetryableOf, ErrorKindSpecMapV0, ErrorKindSpecV0, ErrorKindTag, ErrorStateBuildResultV0, ErrorStateFailInputV0, ErrorStateForKind, ErrorStateOf, ErrorStateV0, SchemaPort, SchemaPortErrorV0, SchemaPortResultV0, defineErrorKinds };
|
package/dist/index.js
CHANGED
|
@@ -1,63 +1,82 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
const ERROR_STATE_SCHEMA_V0 = "carrel.runtime.error.v0";
|
|
2
3
|
function buildInvalidDetailsMessage(tag, error) {
|
|
3
|
-
|
|
4
|
+
return error.message ?? `Invalid error-state details for ${tag}.`;
|
|
4
5
|
}
|
|
5
6
|
function hasOwn(value, key) {
|
|
6
|
-
|
|
7
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
9
|
+
function defineErrorKinds(namespace, constSpec) {
|
|
10
|
+
const kinds = Object.freeze(Object.fromEntries(Object.entries(constSpec).map(([key, spec]) => [key, Object.freeze({
|
|
11
|
+
tag: `${namespace}.${key}`,
|
|
12
|
+
retryable: spec.retryable,
|
|
13
|
+
...spec.reason !== void 0 ? { reason: spec.reason } : {},
|
|
14
|
+
...spec.message !== void 0 ? { message: spec.message } : {},
|
|
15
|
+
...spec.details !== void 0 ? { details: spec.details } : {}
|
|
16
|
+
})])));
|
|
17
|
+
function tryFail(key, input = {}) {
|
|
18
|
+
const kind = kinds[key];
|
|
19
|
+
const reason = input.reason ?? kind.reason;
|
|
20
|
+
const message = input.message ?? kind.message;
|
|
21
|
+
const stateBase = {
|
|
22
|
+
schema: ERROR_STATE_SCHEMA_V0,
|
|
23
|
+
tag: kind.tag,
|
|
24
|
+
retryable: kind.retryable,
|
|
25
|
+
...reason !== void 0 ? { reason } : {},
|
|
26
|
+
...message !== void 0 ? { message } : {},
|
|
27
|
+
...input.commandId !== void 0 ? { commandId: input.commandId } : {},
|
|
28
|
+
...input.runId !== void 0 ? { runId: input.runId } : {},
|
|
29
|
+
...input.stepId !== void 0 ? { stepId: input.stepId } : {},
|
|
30
|
+
...input.correlationId !== void 0 ? { correlationId: input.correlationId } : {}
|
|
31
|
+
};
|
|
32
|
+
if (!hasOwn(input, "details")) return {
|
|
33
|
+
ok: true,
|
|
34
|
+
value: stateBase
|
|
35
|
+
};
|
|
36
|
+
const rawDetails = input.details;
|
|
37
|
+
if (kind.details !== void 0) {
|
|
38
|
+
const result = kind.details.validate(rawDetails);
|
|
39
|
+
if (!result.ok) {
|
|
40
|
+
const state = {
|
|
41
|
+
...stateBase,
|
|
42
|
+
reason: "invalid-error-details",
|
|
43
|
+
message: buildInvalidDetailsMessage(kind.tag, result.error)
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
error: result.error,
|
|
48
|
+
state
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
ok: true,
|
|
53
|
+
value: {
|
|
54
|
+
...stateBase,
|
|
55
|
+
details: result.value
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
ok: true,
|
|
61
|
+
value: {
|
|
62
|
+
...stateBase,
|
|
63
|
+
details: rawDetails
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return Object.freeze({
|
|
68
|
+
namespace,
|
|
69
|
+
kinds,
|
|
70
|
+
fail(key, input) {
|
|
71
|
+
const result = tryFail(key, input);
|
|
72
|
+
return result.ok ? result.value : result.state;
|
|
73
|
+
},
|
|
74
|
+
tryFail,
|
|
75
|
+
contribute(registry) {
|
|
76
|
+
return registry.register(namespace, kinds);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
63
79
|
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
export { ERROR_STATE_SCHEMA_V0, defineErrorKinds };
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daihum/error-state",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Neutral typed error-state mechanism: frozen carrel.runtime.error.v0 envelope, const-spec kind definitions, registry contribution port, and injected details validation. Runtime-free and zero dependency.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"main": "./dist/index.
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
8
9
|
"types": "./dist/index.d.ts",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
12
|
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.js"
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
13
15
|
},
|
|
14
16
|
"./package.json": "./package.json"
|
|
15
17
|
},
|
|
@@ -26,6 +28,7 @@
|
|
|
26
28
|
"author": "DAIHUM",
|
|
27
29
|
"dependencies": {},
|
|
28
30
|
"devDependencies": {
|
|
31
|
+
"tsdown": "^0.16.6",
|
|
29
32
|
"typescript": "~5.6.2",
|
|
30
33
|
"vitest": "^2.1.8"
|
|
31
34
|
},
|
|
@@ -34,8 +37,9 @@
|
|
|
34
37
|
"access": "restricted"
|
|
35
38
|
},
|
|
36
39
|
"scripts": {
|
|
37
|
-
"build": "
|
|
40
|
+
"build": "tsdown",
|
|
38
41
|
"clean": "rm -rf dist",
|
|
42
|
+
"test:artifact": "node scripts/artifact-conformance.mjs",
|
|
39
43
|
"test": "vitest run",
|
|
40
44
|
"typecheck": "tsc --noEmit"
|
|
41
45
|
}
|