@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 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;
@@ -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
- export declare const ERROR_STATE_SCHEMA_V0: "carrel.runtime.error.v0";
2
- export interface ErrorStateV0<Tag extends string = string, Details = unknown> {
3
- readonly schema: typeof ERROR_STATE_SCHEMA_V0;
4
- readonly tag: Tag;
5
- readonly retryable: boolean;
6
- readonly reason?: string;
7
- readonly message?: string;
8
- readonly commandId?: string;
9
- readonly runId?: string;
10
- readonly stepId?: string;
11
- readonly correlationId?: string;
12
- readonly details?: Details;
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
- export interface SchemaPortErrorV0 {
15
- readonly message?: string;
16
- readonly issues?: readonly unknown[];
17
- readonly cause?: unknown;
15
+ interface SchemaPortErrorV0 {
16
+ readonly message?: string;
17
+ readonly issues?: readonly unknown[];
18
+ readonly cause?: unknown;
18
19
  }
19
- export type SchemaPortResultV0<T> = {
20
- readonly ok: true;
21
- readonly value: T;
20
+ type SchemaPortResultV0<T> = {
21
+ readonly ok: true;
22
+ readonly value: T;
22
23
  } | {
23
- readonly ok: false;
24
- readonly error: SchemaPortErrorV0;
24
+ readonly ok: false;
25
+ readonly error: SchemaPortErrorV0;
25
26
  };
26
- export interface SchemaPort<T> {
27
- validate(value: unknown): SchemaPortResultV0<T>;
27
+ interface SchemaPort<T> {
28
+ validate(value: unknown): SchemaPortResultV0<T>;
28
29
  }
29
- export interface ErrorKindSpecV0<Details = never> {
30
- readonly retryable: boolean;
31
- readonly reason?: string;
32
- readonly message?: string;
33
- readonly details?: SchemaPort<Details>;
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
- export interface ErrorKindDefinitionV0<Tag extends string = string, Details = never, Retryable extends boolean = boolean> {
36
- readonly tag: Tag;
37
- readonly retryable: Retryable;
38
- readonly reason?: string;
39
- readonly message?: string;
40
- readonly details?: SchemaPort<Details>;
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
- export type ErrorKindSpecMapV0 = Readonly<Record<string, ErrorKindSpecV0<unknown>>>;
43
- export type ErrorKindDefinitionsV0 = Readonly<Record<string, ErrorKindDefinitionV0<string, unknown, boolean>>>;
44
- export interface ErrorKindRegistryPortV0<Definitions extends ErrorKindDefinitionsV0 = ErrorKindDefinitionsV0> {
45
- register(namespace: string, spec: Definitions): void | Promise<void>;
46
- lookup(tag: string): ErrorKindDefinitionV0 | undefined | Promise<ErrorKindDefinitionV0 | undefined>;
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
- export type ErrorKindDetailsOf<Spec> = Spec extends {
49
- readonly details: SchemaPort<infer Details>;
49
+ type ErrorKindDetailsOf<Spec> = Spec extends {
50
+ readonly details: SchemaPort<infer Details>;
50
51
  } ? Details : never;
51
- export type ErrorKindRetryableOf<Spec> = Spec extends {
52
- readonly retryable: infer Retryable extends boolean;
52
+ type ErrorKindRetryableOf<Spec> = Spec extends {
53
+ readonly retryable: infer Retryable extends boolean;
53
54
  } ? Retryable : boolean;
54
- export type ErrorKindTag<Namespace extends string, Key extends string> = `${Namespace}.${Key}`;
55
- export type ErrorKindDefinitionsFor<Namespace extends string, Spec extends ErrorKindSpecMapV0> = Readonly<{
56
- [Key in keyof Spec & string]: ErrorKindDefinitionV0<ErrorKindTag<Namespace, Key>, ErrorKindDetailsOf<Spec[Key]>, ErrorKindRetryableOf<Spec[Key]>>;
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
- export type ErrorStateOf<Kinds extends ErrorKindDefinitionsV0> = {
62
- [Key in keyof Kinds]: Kinds[Key] extends ErrorKindDefinitionV0<infer Tag, infer Details, infer Retryable> ? ErrorStateV0<Tag, Details> & {
63
- readonly retryable: Retryable;
64
- } : never;
65
- }[keyof Kinds];
66
- export type ErrorStateFailInputV0<Details = never> = {
67
- readonly reason?: string;
68
- readonly message?: string;
69
- readonly commandId?: string;
70
- readonly runId?: string;
71
- readonly stepId?: string;
72
- readonly correlationId?: string;
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
- readonly details?: Details;
73
+ readonly details?: Details$1;
77
74
  });
78
- export type ErrorStateBuildResultV0<State extends ErrorStateV0> = {
79
- readonly ok: true;
80
- readonly value: State;
75
+ type ErrorStateBuildResultV0<State extends ErrorStateV0> = {
76
+ readonly ok: true;
77
+ readonly value: State;
81
78
  } | {
82
- readonly ok: false;
83
- readonly error: SchemaPortErrorV0;
84
- readonly state: State;
79
+ readonly ok: false;
80
+ readonly error: SchemaPortErrorV0;
81
+ readonly state: State;
85
82
  };
86
- export interface DefinedErrorKindsV0<Namespace extends string, Spec extends ErrorKindSpecMapV0> {
87
- readonly namespace: Namespace;
88
- readonly kinds: ErrorKindDefinitionsFor<Namespace, Spec>;
89
- fail<Key extends keyof Spec & string>(tag: Key, input?: ErrorStateFailInputV0<ErrorKindDetailsOf<Spec[Key]>>): ErrorStateForKind<Namespace, Key, Spec[Key]>;
90
- tryFail<Key extends keyof Spec & string>(tag: Key, input?: ErrorStateFailInputV0<ErrorKindDetailsOf<Spec[Key]>>): ErrorStateBuildResultV0<ErrorStateForKind<Namespace, Key, Spec[Key]>>;
91
- contribute(registry: ErrorKindRegistryPortV0<ErrorKindDefinitionsFor<Namespace, Spec>>): void | Promise<void>;
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
- export declare function defineErrorKinds<const Namespace extends string, const Spec extends ErrorKindSpecMapV0>(namespace: Namespace, constSpec: Spec): DefinedErrorKindsV0<Namespace, Spec>;
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
- export const ERROR_STATE_SCHEMA_V0 = "carrel.runtime.error.v0";
1
+ //#region src/index.ts
2
+ const ERROR_STATE_SCHEMA_V0 = "carrel.runtime.error.v0";
2
3
  function buildInvalidDetailsMessage(tag, error) {
3
- return error.message ?? `Invalid error-state details for ${tag}.`;
4
+ return error.message ?? `Invalid error-state details for ${tag}.`;
4
5
  }
5
6
  function hasOwn(value, key) {
6
- return Object.prototype.hasOwnProperty.call(value, key);
7
+ return Object.prototype.hasOwnProperty.call(value, key);
7
8
  }
8
- export function defineErrorKinds(namespace, constSpec) {
9
- const kinds = Object.freeze(Object.fromEntries(Object.entries(constSpec).map(([key, spec]) => [
10
- key,
11
- Object.freeze({
12
- tag: `${namespace}.${key}`,
13
- retryable: spec.retryable,
14
- ...(spec.reason !== undefined ? { reason: spec.reason } : {}),
15
- ...(spec.message !== undefined ? { message: spec.message } : {}),
16
- ...(spec.details !== undefined ? { details: spec.details } : {}),
17
- }),
18
- ])));
19
- function tryFail(key, input = {}) {
20
- const kind = kinds[key];
21
- const reason = input.reason ?? kind.reason;
22
- const message = input.message ?? kind.message;
23
- const stateBase = {
24
- schema: ERROR_STATE_SCHEMA_V0,
25
- tag: kind.tag,
26
- retryable: kind.retryable,
27
- ...(reason !== undefined ? { reason } : {}),
28
- ...(message !== undefined ? { message } : {}),
29
- ...(input.commandId !== undefined ? { commandId: input.commandId } : {}),
30
- ...(input.runId !== undefined ? { runId: input.runId } : {}),
31
- ...(input.stepId !== undefined ? { stepId: input.stepId } : {}),
32
- ...(input.correlationId !== undefined ? { correlationId: input.correlationId } : {}),
33
- };
34
- if (!hasOwn(input, "details"))
35
- return { ok: true, value: stateBase };
36
- const rawDetails = input.details;
37
- if (kind.details !== undefined) {
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 { ok: false, error: result.error, state };
46
- }
47
- return { ok: true, value: { ...stateBase, details: result.value } };
48
- }
49
- return { ok: true, value: { ...stateBase, details: rawDetails } };
50
- }
51
- return Object.freeze({
52
- namespace,
53
- kinds,
54
- fail(key, input) {
55
- const result = tryFail(key, input);
56
- return result.ok ? result.value : result.state;
57
- },
58
- tryFail,
59
- contribute(registry) {
60
- return registry.register(namespace, kinds);
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.0",
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.js",
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": "tsc",
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
  }