@nice-code/error 0.8.0 → 0.9.0
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/build/index.d.ts +913 -0
- package/build/index.js +1004 -933
- package/build/index.js.map +1 -0
- package/package.json +5 -4
- package/build/types/NiceError/NiceError.d.ts +0 -148
- package/build/types/NiceError/NiceError.enums.d.ts +0 -5
- package/build/types/NiceError/NiceError.types.d.ts +0 -204
- package/build/types/NiceError/NiceErrorHydrated.d.ts +0 -51
- package/build/types/NiceError/nice_error.static.d.ts +0 -2
- package/build/types/NiceErrorDefined/NiceErrorDefined.d.ts +0 -135
- package/build/types/NiceErrorDefined/defineNiceError.d.ts +0 -7
- package/build/types/NiceErrorDefined/err.d.ts +0 -59
- package/build/types/NiceErrorHandler/NiceErrorHandler.d.ts +0 -23
- package/build/types/NiceErrorHandler/NiceErrorHandler.types.d.ts +0 -82
- package/build/types/NiceErrorHandler/handleWith.d.ts +0 -8
- package/build/types/example/error_usage_example.d.ts +0 -41
- package/build/types/index.d.ts +0 -18
- package/build/types/internal/index.d.ts +0 -1
- package/build/types/internal/nice_core_errors.d.ts +0 -49
- package/build/types/test/helpers/nice_error_testing.static.d.ts +0 -3
- package/build/types/test/helpers/test_utils.d.ts +0 -1
- package/build/types/utils/castAndHydrate.d.ts +0 -35
- package/build/types/utils/castNiceError.d.ts +0 -15
- package/build/types/utils/inspectPotentialError/inspectPotentialError.d.ts +0 -2
- package/build/types/utils/inspectPotentialError/inspectPotentialError.enums.d.ts +0 -9
- package/build/types/utils/inspectPotentialError/inspectPotentialError.types.d.ts +0 -51
- package/build/types/utils/isNiceErrorObject.d.ts +0 -12
- package/build/types/utils/isRegularErrorObject.d.ts +0 -2
- package/build/types/utils/jsErrorOrCastJsError.d.ts +0 -1
- package/build/types/utils/logger.d.ts +0 -3
- package/build/types/utils/matchFirst.d.ts +0 -35
- package/build/types/utils/packError/causePack.d.ts +0 -2
- package/build/types/utils/packError/msgPack.d.ts +0 -2
- package/build/types/utils/packError/packError.d.ts +0 -3
- package/build/types/utils/packError/packError.enums.d.ts +0 -5
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { NiceErrorDomain } from "../NiceErrorDefined/NiceErrorDefined";
|
|
2
|
-
import { type INiceErrorOptions, NiceError } from "./NiceError";
|
|
3
|
-
import type { ExtractFromIdContextArg, INiceErrorDomainProps, TDomainNiceErrorId, TFromContextInput, TUnknownNiceErrorDef } from "./NiceError.types";
|
|
4
|
-
/**
|
|
5
|
-
* Resolves the args tuple for `addId` — mirrors `FromIdArgs` exactly so that
|
|
6
|
-
* optional vs required context is consistent across both `fromId` and `addId`.
|
|
7
|
-
*
|
|
8
|
-
* - No context on this id → `[id]`
|
|
9
|
-
* - Context defined, `required: true` → `[id, context]`
|
|
10
|
-
* - Context defined, `required` absent/false → `[id] | [id, context]`
|
|
11
|
-
*/
|
|
12
|
-
type AddIdArgs<ERR_DEF extends INiceErrorDomainProps, K extends TDomainNiceErrorId<ERR_DEF>> = [
|
|
13
|
-
ExtractFromIdContextArg<ERR_DEF["schema"][K]>
|
|
14
|
-
] extends [undefined] ? [id: K] : [undefined] extends [ExtractFromIdContextArg<ERR_DEF["schema"][K]>] ? [id: K] | [id: K, context: NonNullable<ExtractFromIdContextArg<ERR_DEF["schema"][K]>>] : [id: K, context: ExtractFromIdContextArg<ERR_DEF["schema"][K]>];
|
|
15
|
-
/** Full-featured construction from NiceErrorDefined.fromId / fromContext. */
|
|
16
|
-
export interface INiceErrorHydratedOptions<ERR_DEF extends INiceErrorDomainProps, ID extends TDomainNiceErrorId<ERR_DEF>> extends INiceErrorOptions<ERR_DEF, ID> {
|
|
17
|
-
def: ERR_DEF;
|
|
18
|
-
niceErrorDefined: NiceErrorDomain<ERR_DEF>;
|
|
19
|
-
}
|
|
20
|
-
export declare class NiceErrorHydrated<ERR_DEF extends INiceErrorDomainProps = TUnknownNiceErrorDef,
|
|
21
|
-
/**
|
|
22
|
-
* Union of active error-id keys.
|
|
23
|
-
* - After `fromId(id)`: exactly one key.
|
|
24
|
-
* - After `fromContext({...})`: a union of all supplied keys.
|
|
25
|
-
* - After `hasOneOfIds([a,b])`: narrows to that subset.
|
|
26
|
-
* - Default (bare construction / castNiceError): `TUnknownNiceErrorId`.
|
|
27
|
-
*/
|
|
28
|
-
ACTIVE_IDS extends TDomainNiceErrorId<ERR_DEF> = TDomainNiceErrorId<ERR_DEF>> extends NiceError<ERR_DEF, ACTIVE_IDS> {
|
|
29
|
-
readonly def: ERR_DEF;
|
|
30
|
-
private readonly niceErrorDefined;
|
|
31
|
-
constructor(options: INiceErrorHydratedOptions<ERR_DEF, ACTIVE_IDS>);
|
|
32
|
-
/**
|
|
33
|
-
* Returns a **new** `NiceErrorHydrated` with additional id+context entries merged in.
|
|
34
|
-
* The returned error's `ACTIVE_IDS` is the union of the original ids and the
|
|
35
|
-
* newly supplied keys.
|
|
36
|
-
*
|
|
37
|
-
* ```ts
|
|
38
|
-
* const err = errDef.fromId("id_a", { a: 1 })
|
|
39
|
-
* .addContext({ id_b: { b: "x" } });
|
|
40
|
-
* err.getIds(); // ["id_a", "id_b"]
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
addContext<INPUT extends TFromContextInput<ERR_DEF["schema"]>>(context: INPUT & Record<Exclude<keyof INPUT, keyof ERR_DEF["schema"] & string>, never>): NiceErrorHydrated<ERR_DEF, ACTIVE_IDS | (keyof INPUT & string)>;
|
|
44
|
-
/**
|
|
45
|
-
* Returns a **new** `NiceErrorHydrated` with an additional error id (and its context,
|
|
46
|
-
* if the schema requires one). Equivalent to `addContext({ [id]: context })`
|
|
47
|
-
* but mirrors the `fromId` ergonomics for single-id additions.
|
|
48
|
-
*/
|
|
49
|
-
addId<K extends TDomainNiceErrorId<ERR_DEF>>(...args: AddIdArgs<ERR_DEF, K>): NiceErrorHydrated<ERR_DEF, ACTIVE_IDS | K>;
|
|
50
|
-
}
|
|
51
|
-
export {};
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { NiceError } from "../NiceError/NiceError";
|
|
2
|
-
import { type FromIdArgs, type IDefineNewNiceErrorDomainOptions, type INiceErrorDomainProps, type INiceErrorJsonObject, type TDomainNiceErrorId, type TErrorReconciledData, type TFromContextInput } from "../NiceError/NiceError.types";
|
|
3
|
-
import { NiceErrorHydrated } from "../NiceError/NiceErrorHydrated";
|
|
4
|
-
import { type EErrorPackType } from "../utils/packError/packError.enums";
|
|
5
|
-
type ChildDef<PARENT_DEF extends INiceErrorDomainProps, SUB extends IDefineNewNiceErrorDomainOptions> = {
|
|
6
|
-
domain: SUB["domain"];
|
|
7
|
-
allDomains: [SUB["domain"], ...PARENT_DEF["allDomains"]];
|
|
8
|
-
schema: SUB["schema"];
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Extracts the union of keys present in a `TFromContextInput` object.
|
|
12
|
-
* e.g. `{ invalid_credentials: {...} }` → `"invalid_credentials"`
|
|
13
|
-
*/
|
|
14
|
-
type KeysOfContextInput<INPUT> = keyof INPUT & string;
|
|
15
|
-
/**
|
|
16
|
-
* Infers the strongly-typed `NiceError` class type from a `NiceErrorDefined` instance.
|
|
17
|
-
*
|
|
18
|
-
* `ACTIVE_IDS` is set to the full union of all schema keys. Use `hasId` /
|
|
19
|
-
* `hasOneOfIds` to narrow further at the call site.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* const err_user_auth = defineNiceError({ domain: "err_user_auth", schema: { ... } });
|
|
24
|
-
* type TUserAuthError = InferNiceError<typeof err_user_auth>;
|
|
25
|
-
* // → NiceError<{ domain: "err_user_auth"; ... }, keyof schema>
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
export type InferNiceError<T extends NiceErrorDomain<any>> = T extends NiceErrorDomain<infer ERR_DEF> ? NiceError<ERR_DEF, keyof ERR_DEF["schema"] & string> : never;
|
|
29
|
-
/**
|
|
30
|
-
* Infers the strongly-typed `NiceErrorHydrated` class type from a `NiceErrorDefined` instance.
|
|
31
|
-
*
|
|
32
|
-
* Use this when you need the builder methods (`addId`, `addContext`) as part of
|
|
33
|
-
* the inferred type — e.g. for function return types or variable annotations.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* const err_user_auth = defineNiceError({ domain: "err_user_auth", schema: { ... } });
|
|
38
|
-
* type TUserAuthErrorHydrated = InferNiceErrorHydrated<typeof err_user_auth>;
|
|
39
|
-
* // → NiceErrorHydrated<{ domain: "err_user_auth"; ... }, keyof schema>
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
export type InferNiceErrorHydrated<T extends NiceErrorDomain<any>> = T extends NiceErrorDomain<infer ERR_DEF> ? NiceErrorHydrated<ERR_DEF, keyof ERR_DEF["schema"] & string> : never;
|
|
43
|
-
export declare class NiceErrorDomain<ERR_DEF extends INiceErrorDomainProps = INiceErrorDomainProps> {
|
|
44
|
-
readonly domain: ERR_DEF["domain"];
|
|
45
|
-
readonly allDomains: ERR_DEF["allDomains"];
|
|
46
|
-
readonly defaultHttpStatusCode?: number;
|
|
47
|
-
readonly defaultMessage?: string;
|
|
48
|
-
/** Kept for runtime use (message resolution, httpStatusCode, context serialization, etc.). */
|
|
49
|
-
private readonly _schema;
|
|
50
|
-
private _definedChildNiceErrors;
|
|
51
|
-
private _definedParentNiceError?;
|
|
52
|
-
/** Set by `.packAs()` — explicit per-instance override, takes priority over `_packAsFn`. */
|
|
53
|
-
private _setPack?;
|
|
54
|
-
/** Set at definition time — called dynamically each time an error is created. */
|
|
55
|
-
private _packAsFn?;
|
|
56
|
-
constructor(definition: ERR_DEF);
|
|
57
|
-
/**
|
|
58
|
-
* Creates a child domain that inherits this domain in `allDomains`.
|
|
59
|
-
* The child has its own schema and its own domain string.
|
|
60
|
-
*/
|
|
61
|
-
createChildDomain<SUB extends IDefineNewNiceErrorDomainOptions>(subErrorDef: SUB & {
|
|
62
|
-
[K in Exclude<keyof SUB, keyof IDefineNewNiceErrorDomainOptions>]: never;
|
|
63
|
-
}): NiceErrorDomain<ChildDef<ERR_DEF, SUB>>;
|
|
64
|
-
protected addParentNiceErrorDefined<PARENT_DEF extends INiceErrorDomainProps>(parentError: NiceErrorDomain<PARENT_DEF>): void;
|
|
65
|
-
protected addChildNiceErrorDefined<CHILD_DEF extends INiceErrorDomainProps>(child: NiceErrorDomain<CHILD_DEF>): void;
|
|
66
|
-
packAs(pack: EErrorPackType): this;
|
|
67
|
-
private createError;
|
|
68
|
-
/**
|
|
69
|
-
* Promotes a plain `NiceError<ERR_DEF>` back into a `NiceErrorHydrated` so
|
|
70
|
-
* that builder methods (`addId`, `addContext`, etc.) are available again.
|
|
71
|
-
*
|
|
72
|
-
* For each active id, if the context is in the `"unhydrated"` state (i.e. the
|
|
73
|
-
* error was reconstructed from a JSON payload), `hydrate` calls
|
|
74
|
-
* `fromJsonSerializable` to reconstruct the typed value and advances the state
|
|
75
|
-
* to `"hydrated"`. Ids already in `"hydrated"` or `"raw_unset"` state
|
|
76
|
-
* are passed through unchanged.
|
|
77
|
-
*
|
|
78
|
-
* @throws If `error.def.domain` does not match this definition's domain. Use
|
|
79
|
-
* `niceErrorDefined.is(error)` before calling `hydrate` to ensure compatibility.
|
|
80
|
-
*
|
|
81
|
-
* ```ts
|
|
82
|
-
* const raw = castNiceError(apiResponseBody);
|
|
83
|
-
*
|
|
84
|
-
* if (err_user_auth.is(raw)) {
|
|
85
|
-
* const hydrated = err_user_auth.hydrate(raw);
|
|
86
|
-
* // hydrated.getContext("invalid_credentials") — fully typed, no throw
|
|
87
|
-
* // hydrated.addId / addContext — available again
|
|
88
|
-
* }
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
hydrate<ACTIVE_IDS extends TDomainNiceErrorId<ERR_DEF> = TDomainNiceErrorId<ERR_DEF>>(error: NiceError<ERR_DEF, ACTIVE_IDS> | INiceErrorJsonObject<ERR_DEF, ACTIVE_IDS>): NiceErrorHydrated<ERR_DEF, ACTIVE_IDS>;
|
|
92
|
-
/**
|
|
93
|
-
* Creates a `NiceErrorHydrated` for a single error id.
|
|
94
|
-
*
|
|
95
|
-
* - `id` autocompletes to the schema keys.
|
|
96
|
-
* - The second argument `context` is required / optional / absent based on
|
|
97
|
-
* whether the schema entry declares `context.required: true`.
|
|
98
|
-
* - The returned error has `ACTIVE_IDS` narrowed to exactly `K`, so
|
|
99
|
-
* `getContext(id)` is immediately strongly typed.
|
|
100
|
-
*/
|
|
101
|
-
fromId<K extends keyof ERR_DEF["schema"] & string>(...args: FromIdArgs<ERR_DEF, K>): NiceErrorHydrated<ERR_DEF, K>;
|
|
102
|
-
fromContext<INPUT extends TFromContextInput<ERR_DEF["schema"]>>(context: INPUT & Record<Exclude<keyof INPUT, keyof ERR_DEF["schema"]>, never>): NiceErrorHydrated<ERR_DEF, KeysOfContextInput<INPUT>>;
|
|
103
|
-
/**
|
|
104
|
-
* Returns `true` if `error` is a `NiceError` whose `def.domain` exactly matches
|
|
105
|
-
* this definition's domain.
|
|
106
|
-
*
|
|
107
|
-
* Use this after `castNiceError` to narrow an unknown error to this specific
|
|
108
|
-
* domain before accessing its typed ids/context:
|
|
109
|
-
*
|
|
110
|
-
* ```ts
|
|
111
|
-
* const caught = castNiceError(e);
|
|
112
|
-
*
|
|
113
|
-
* if (err_user_auth.is(caught)) {
|
|
114
|
-
* // caught is now NiceError<typeof err_user_auth's ERR_DEF>
|
|
115
|
-
* const hydrated = err_user_auth.hydrate(caught);
|
|
116
|
-
* const { username } = hydrated.getContext("invalid_credentials");
|
|
117
|
-
* }
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
isExact(error: unknown): error is NiceError<ERR_DEF, keyof ERR_DEF["schema"] & string>;
|
|
121
|
-
isThisOrChild(error: unknown): boolean;
|
|
122
|
-
/**
|
|
123
|
-
* Returns `true` if this domain appears anywhere in the target's ancestry
|
|
124
|
-
* chain (including an exact domain match).
|
|
125
|
-
*
|
|
126
|
-
* Accepts either a `NiceErrorDefined` (domain definition) or a `NiceError`
|
|
127
|
-
* instance (extracts the domain from its `def`).
|
|
128
|
-
*/
|
|
129
|
-
isParentOf(target: NiceErrorDomain<any> | NiceError<any, any>): boolean;
|
|
130
|
-
private _buildDef;
|
|
131
|
-
private _resolveMessage;
|
|
132
|
-
private _resolveHttpStatusCode;
|
|
133
|
-
reconcileErrorDataForId(id: keyof ERR_DEF["schema"] & string, context: TFromContextInput<ERR_DEF["schema"]>[typeof id]): TErrorReconciledData<ERR_DEF["schema"], typeof id>;
|
|
134
|
-
}
|
|
135
|
-
export {};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { IDefineNewNiceErrorDomainOptions, TNiceErrorSchema } from "../NiceError/NiceError.types";
|
|
2
|
-
import { NiceErrorDomain } from "./NiceErrorDefined";
|
|
3
|
-
export declare const defineNiceError: <ERR_DOMAIN extends string, SCHEMA extends TNiceErrorSchema>(definition: IDefineNewNiceErrorDomainOptions<ERR_DOMAIN, SCHEMA>) => NiceErrorDomain<{
|
|
4
|
-
domain: ERR_DOMAIN;
|
|
5
|
-
allDomains: [ERR_DOMAIN];
|
|
6
|
-
schema: SCHEMA;
|
|
7
|
-
}>;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { INiceErrorContextDefinition, INiceErrorIdMetadata, JSONSerializableValue } from "../NiceError/NiceError.types";
|
|
2
|
-
/**
|
|
3
|
-
* Schema entry factory — the idiomatic way to define a single error id in a
|
|
4
|
-
* `NiceErrorDefined` schema.
|
|
5
|
-
*
|
|
6
|
-
* Using `err()` instead of writing schema entries as plain object literals
|
|
7
|
-
* ensures TypeScript resolves the context argument as **required** vs **optional**
|
|
8
|
-
* correctly in `fromId` / `addId` call sites.
|
|
9
|
-
*
|
|
10
|
-
* How `required` affects the call site:
|
|
11
|
-
*
|
|
12
|
-
* | Schema entry | `fromId("id")` | `fromId("id", ctx)` |
|
|
13
|
-
* |------------------------------------|----------------|---------------------|
|
|
14
|
-
* | `err()` / `err({ ... })` | ✓ | ✗ (no context) |
|
|
15
|
-
* | `err<C>({ context: {} })` | ✓ | ✓ (optional ctx) |
|
|
16
|
-
* | `err<C>({ context: { required: true } })` | ✗ | ✓ (required ctx) |
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* // No context — `fromId("not_found")` takes no second argument.
|
|
21
|
-
* not_found: err({ message: "Resource not found", httpStatusCode: 404 }),
|
|
22
|
-
*
|
|
23
|
-
* // Optional context — second arg accepted but not required.
|
|
24
|
-
* rate_limited: err<{ retryAfter: number }>({
|
|
25
|
-
* message: (ctx) => ctx ? `Retry after ${ctx.retryAfter}s` : "Rate limited",
|
|
26
|
-
* httpStatusCode: 429,
|
|
27
|
-
* context: {},
|
|
28
|
-
* }),
|
|
29
|
-
*
|
|
30
|
-
* // Required context — `fromId("invalid_input", { field: "email" })` (second arg is required).
|
|
31
|
-
* invalid_input: err<{ field: string }>({
|
|
32
|
-
* message: ({ field }) => `Invalid value for field: ${field}`,
|
|
33
|
-
* httpStatusCode: 422,
|
|
34
|
-
* context: { required: true },
|
|
35
|
-
* }),
|
|
36
|
-
*
|
|
37
|
-
* // Required context with custom serialization (for non-JSON-safe context values).
|
|
38
|
-
* fs_error: err<{ cause: NodeJS.ErrnoException }>({
|
|
39
|
-
* message: ({ cause }) => `File system error: ${cause.message}`,
|
|
40
|
-
* context: {
|
|
41
|
-
* required: true,
|
|
42
|
-
* serialization: {
|
|
43
|
-
* toJsonSerializable: ({ cause }) => ({ code: cause.code, message: cause.message }),
|
|
44
|
-
* fromJsonSerializable: (obj) => ({ cause: Object.assign(new Error(obj.message), obj) }),
|
|
45
|
-
* },
|
|
46
|
-
* },
|
|
47
|
-
* }),
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export declare function err<C, D extends JSONSerializableValue = Record<string, any>>(meta: INiceErrorIdMetadata<C, D> & {
|
|
51
|
-
context: INiceErrorContextDefinition<C, D> & {
|
|
52
|
-
required: true;
|
|
53
|
-
};
|
|
54
|
-
}): INiceErrorIdMetadata<C> & {
|
|
55
|
-
context: {
|
|
56
|
-
required: true;
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
export declare function err<C = never>(meta?: INiceErrorIdMetadata<C>): INiceErrorIdMetadata<C>;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { NiceError } from "../NiceError/NiceError";
|
|
2
|
-
import type { INiceErrorDomainProps, TDomainNiceErrorId } from "../NiceError/NiceError.types";
|
|
3
|
-
import type { NiceErrorHydrated } from "../NiceError/NiceErrorHydrated";
|
|
4
|
-
import type { NiceErrorDomain } from "../NiceErrorDefined/NiceErrorDefined";
|
|
5
|
-
import { type IHandleErrorOptions, type MaybePromise, type THandleErrorWithPromiseInspection } from "./NiceErrorHandler.types";
|
|
6
|
-
export declare class NiceErrorHandler<RES_DEF = never, RES = never> {
|
|
7
|
-
private handlerConfigs;
|
|
8
|
-
private _defaultRequester?;
|
|
9
|
-
handleErrorWithPromiseInspection(error: NiceError<any, any>, options?: IHandleErrorOptions): THandleErrorWithPromiseInspection<RES | RES_DEF>;
|
|
10
|
-
/**
|
|
11
|
-
* Register a handler that fires for **any** error whose domain matches `domain`.
|
|
12
|
-
* The handler receives a fully hydrated error — `getContext`, `addId`, and `addContext`
|
|
13
|
-
* are all available. First matching case wins.
|
|
14
|
-
*/
|
|
15
|
-
forDomain<DEF extends INiceErrorDomainProps, H_RES = void>(domain: NiceErrorDomain<DEF>, handler: (error: NiceErrorHydrated<DEF, TDomainNiceErrorId<DEF>>) => MaybePromise<H_RES>): NiceErrorHandler<RES_DEF, RES | H_RES>;
|
|
16
|
-
forId<DEF extends INiceErrorDomainProps, ID extends TDomainNiceErrorId<DEF>, H_RES = void>(domain: NiceErrorDomain<DEF>, id: ID, handler: (error: NiceErrorHydrated<DEF, ID>) => MaybePromise<H_RES>): NiceErrorHandler<RES_DEF, RES | H_RES>;
|
|
17
|
-
forIds<DEF extends INiceErrorDomainProps, IDS extends TDomainNiceErrorId<DEF>[], H_RES = void>(domain: NiceErrorDomain<DEF>, ids: IDS, handler: (error: NiceErrorHydrated<DEF, IDS[number]>) => MaybePromise<H_RES>): NiceErrorHandler<RES_DEF, RES | H_RES>;
|
|
18
|
-
/**
|
|
19
|
-
* Register a fallback handler that fires when no other case matches.
|
|
20
|
-
* Only one default handler can be registered — calling this twice replaces the previous one.
|
|
21
|
-
*/
|
|
22
|
-
setDefaultHandler<H_RES>(handler: (error: NiceError<any, any>) => MaybePromise<H_RES>): NiceErrorHandler<H_RES, RES>;
|
|
23
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import type { NiceError } from "../NiceError/NiceError";
|
|
2
|
-
import type { INiceErrorDomainProps, TDomainNiceErrorId } from "../NiceError/NiceError.types";
|
|
3
|
-
import type { NiceErrorHydrated } from "../NiceError/NiceErrorHydrated";
|
|
4
|
-
export type MaybePromise<T> = T | Promise<T>;
|
|
5
|
-
/**
|
|
6
|
-
* Broad handler signature used internally for storage and dispatch.
|
|
7
|
-
*/
|
|
8
|
-
export type TBroadErrorHandler<E extends NiceError = NiceError, RES = unknown> = (action: E) => MaybePromise<RES>;
|
|
9
|
-
/**
|
|
10
|
-
* Handler registered via forDomain.
|
|
11
|
-
*
|
|
12
|
-
* `act.input` is typed as the union of input types for every action in `ACT_DOM`,
|
|
13
|
-
* and `act.coreAction` carries the matching schema — the same narrowing you get
|
|
14
|
-
* from `forActionIds` over all action IDs in the domain.
|
|
15
|
-
*/
|
|
16
|
-
export type TErrorHandlerForDomain<ERR_DOM extends INiceErrorDomainProps> = (error: NiceErrorHydrated<ERR_DOM>) => MaybePromise<unknown>;
|
|
17
|
-
/**
|
|
18
|
-
* Handler registered via forActionId — receives a specific action ID, with
|
|
19
|
-
* the primed action's input narrowed to that ID's schema.
|
|
20
|
-
*/
|
|
21
|
-
export type TErrorIdHandlerForDomain<ERR_DOM extends INiceErrorDomainProps, ID extends TDomainNiceErrorId<ERR_DOM>> = (error: NiceErrorHydrated<ERR_DOM, ID>) => MaybePromise<unknown>;
|
|
22
|
-
export declare enum EErrorHandlerTargetType {
|
|
23
|
-
ids = "ids",
|
|
24
|
-
domain = "domain",
|
|
25
|
-
default = "default"
|
|
26
|
-
}
|
|
27
|
-
export type TErrorHandlerTarget = ({
|
|
28
|
-
type: EErrorHandlerTargetType.ids;
|
|
29
|
-
domain: string;
|
|
30
|
-
ids: string[];
|
|
31
|
-
} | {
|
|
32
|
-
type: EErrorHandlerTargetType.domain;
|
|
33
|
-
domain: string;
|
|
34
|
-
} | {
|
|
35
|
-
type: EErrorHandlerTargetType.default;
|
|
36
|
-
}) & {
|
|
37
|
-
identifier: string;
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* A single case in a `handleWith` / `handleWithAsync` call.
|
|
41
|
-
*
|
|
42
|
-
* Construct via `forDomain` or `forIds` — do not build this object directly.
|
|
43
|
-
*/
|
|
44
|
-
export interface IErrorHandlerConfig<ERR extends NiceError<any, any> = NiceError<any, any>, RES = unknown> {
|
|
45
|
-
readonly _matcher: (action: ERR) => boolean;
|
|
46
|
-
readonly _requester: TBroadErrorHandler<ERR, RES>;
|
|
47
|
-
}
|
|
48
|
-
export interface IErrorHandleAttempt_Success<RES = void> {
|
|
49
|
-
handled: true;
|
|
50
|
-
result: RES;
|
|
51
|
-
target: TErrorHandlerTarget;
|
|
52
|
-
}
|
|
53
|
-
export interface IErrorHandleAttempt_Failure {
|
|
54
|
-
handled: false;
|
|
55
|
-
result?: undefined;
|
|
56
|
-
targets: TErrorHandlerTarget[];
|
|
57
|
-
}
|
|
58
|
-
export type TErrorHandleAttempt<RES = void> = IErrorHandleAttempt_Success<RES> | IErrorHandleAttempt_Failure;
|
|
59
|
-
export interface IHandleErrorOptions {
|
|
60
|
-
throwOnUnhandled?: boolean;
|
|
61
|
-
}
|
|
62
|
-
export type THandleErrorWithPromiseInspection<RES = void> = {
|
|
63
|
-
isPromise: false;
|
|
64
|
-
matched: true;
|
|
65
|
-
target: TErrorHandlerTarget;
|
|
66
|
-
handlerResponse: RES;
|
|
67
|
-
} | {
|
|
68
|
-
isPromise: true;
|
|
69
|
-
matched: true;
|
|
70
|
-
target: TErrorHandlerTarget;
|
|
71
|
-
handlerPromise: Promise<RES>;
|
|
72
|
-
} | {
|
|
73
|
-
matched: false;
|
|
74
|
-
attemptedTargets: TErrorHandlerTarget[];
|
|
75
|
-
};
|
|
76
|
-
export type THandleResponse<RES = unknown> = {
|
|
77
|
-
handled: false;
|
|
78
|
-
} | {
|
|
79
|
-
handled: true;
|
|
80
|
-
isPromise: boolean;
|
|
81
|
-
response: RES;
|
|
82
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { INiceErrorDomainProps, TDomainNiceErrorId } from "../NiceError/NiceError.types";
|
|
2
|
-
import type { NiceErrorHydrated } from "../NiceError/NiceErrorHydrated";
|
|
3
|
-
import type { NiceErrorDomain } from "../NiceErrorDefined/NiceErrorDefined";
|
|
4
|
-
import { NiceErrorHandler } from "./NiceErrorHandler";
|
|
5
|
-
import type { MaybePromise } from "./NiceErrorHandler.types";
|
|
6
|
-
export declare function forDomain<DEF extends INiceErrorDomainProps, H_RES = void>(domain: NiceErrorDomain<DEF>, handler: (error: NiceErrorHydrated<DEF, TDomainNiceErrorId<DEF>>) => MaybePromise<H_RES>): NiceErrorHandler<never, H_RES>;
|
|
7
|
-
export declare function forId<DEF extends INiceErrorDomainProps, ID extends TDomainNiceErrorId<DEF>, H_RES = void>(domain: NiceErrorDomain<DEF>, id: ID, handler: (error: NiceErrorHydrated<DEF, ID>) => MaybePromise<H_RES>): NiceErrorHandler<never, H_RES>;
|
|
8
|
-
export declare function forIds<DEF extends INiceErrorDomainProps, IDS extends TDomainNiceErrorId<DEF>[], H_RES = void>(domain: NiceErrorDomain<DEF>, ids: IDS, handler: (error: NiceErrorHydrated<DEF, IDS[number]>) => MaybePromise<H_RES>): NiceErrorHandler<never, H_RES>;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export declare const err_example_app: import("..").NiceErrorDomain<{
|
|
2
|
-
domain: "err_example_app";
|
|
3
|
-
allDomains: ["err_example_app"];
|
|
4
|
-
schema: {};
|
|
5
|
-
}>;
|
|
6
|
-
export declare enum EErrId_UserAuth {
|
|
7
|
-
invalid_credentials = "invalid_credentials",
|
|
8
|
-
account_locked = "account_locked"
|
|
9
|
-
}
|
|
10
|
-
export declare const err_user_auth: import("..").NiceErrorDomain<{
|
|
11
|
-
domain: string;
|
|
12
|
-
allDomains: [string, "err_example_app"];
|
|
13
|
-
schema: {
|
|
14
|
-
invalid_credentials: import("..").INiceErrorIdMetadata<{
|
|
15
|
-
username: string;
|
|
16
|
-
}, import("..").JSONSerializableValue> & {
|
|
17
|
-
context: {
|
|
18
|
-
required: true;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
account_locked: import("..").INiceErrorIdMetadata<any, import("..").JSONSerializableValue>;
|
|
22
|
-
};
|
|
23
|
-
}>;
|
|
24
|
-
export declare enum EErrId_UserAuth_Registration {
|
|
25
|
-
password_error = "password_error",
|
|
26
|
-
password_too_short = "password_too_short"
|
|
27
|
-
}
|
|
28
|
-
export declare const err_user_auth_registration: import("..").NiceErrorDomain<{
|
|
29
|
-
domain: string;
|
|
30
|
-
allDomains: [string, string, "err_example_app"];
|
|
31
|
-
schema: {
|
|
32
|
-
password_too_short: import("..").INiceErrorIdMetadata<{
|
|
33
|
-
minLength: number;
|
|
34
|
-
}, import("..").JSONSerializableValue> & {
|
|
35
|
-
context: {
|
|
36
|
-
required: true;
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
password_error: import("..").INiceErrorIdMetadata<any, import("..").JSONSerializableValue>;
|
|
40
|
-
};
|
|
41
|
-
}>;
|
package/build/types/index.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export * from "./internal";
|
|
2
|
-
export * from "./NiceError/NiceError";
|
|
3
|
-
export * from "./NiceError/NiceError.types";
|
|
4
|
-
export * from "./NiceError/NiceErrorHydrated";
|
|
5
|
-
export * from "./NiceErrorDefined/defineNiceError";
|
|
6
|
-
export * from "./NiceErrorDefined/err";
|
|
7
|
-
export * from "./NiceErrorDefined/NiceErrorDefined";
|
|
8
|
-
export * from "./NiceErrorHandler/handleWith";
|
|
9
|
-
export * from "./NiceErrorHandler/NiceErrorHandler";
|
|
10
|
-
export * from "./NiceErrorHandler/NiceErrorHandler.types";
|
|
11
|
-
export * from "./utils/castAndHydrate";
|
|
12
|
-
export * from "./utils/castNiceError";
|
|
13
|
-
export * from "./utils/isNiceErrorObject";
|
|
14
|
-
export * from "./utils/isRegularErrorObject";
|
|
15
|
-
export * from "./utils/matchFirst";
|
|
16
|
-
export * from "./utils/packError/causePack";
|
|
17
|
-
export * from "./utils/packError/msgPack";
|
|
18
|
-
export * from "./utils/packError/packError.enums";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./nice_core_errors";
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { IInspectErrorResult_JsError, IInspectErrorResult_JsErrorObject, IInspectErrorResult_JsOther, IInspectErrorResult_Nullish, TInspectErrorResult_JsDataType } from "../utils/inspectPotentialError/inspectPotentialError.types";
|
|
2
|
-
export declare const err_nice: import("..").NiceErrorDomain<{
|
|
3
|
-
domain: "err_nice";
|
|
4
|
-
allDomains: ["err_nice"];
|
|
5
|
-
schema: {};
|
|
6
|
-
}>;
|
|
7
|
-
export declare enum EErrId_CastNotNice {
|
|
8
|
-
js_error = "native_error",
|
|
9
|
-
js_error_like_object = "js_error_like_object",
|
|
10
|
-
nullish_value = "nullish_value",
|
|
11
|
-
js_data_type = "js_data_type",
|
|
12
|
-
js_other = "js_other"
|
|
13
|
-
}
|
|
14
|
-
export declare const err_cast_not_nice: import("..").NiceErrorDomain<{
|
|
15
|
-
domain: string;
|
|
16
|
-
allDomains: [string, "err_nice"];
|
|
17
|
-
schema: {
|
|
18
|
-
native_error: import("..").INiceErrorIdMetadata<IInspectErrorResult_JsError, import("..").JSONSerializableValue> & {
|
|
19
|
-
context: {
|
|
20
|
-
required: true;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
js_error_like_object: import("..").INiceErrorIdMetadata<IInspectErrorResult_JsErrorObject, import("..").JSONSerializableValue> & {
|
|
24
|
-
context: {
|
|
25
|
-
required: true;
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
nullish_value: import("..").INiceErrorIdMetadata<IInspectErrorResult_Nullish, import("..").JSONSerializableValue> & {
|
|
29
|
-
context: {
|
|
30
|
-
required: true;
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
js_data_type: import("..").INiceErrorIdMetadata<TInspectErrorResult_JsDataType, import("..").JSONSerializableValue> & {
|
|
34
|
-
context: {
|
|
35
|
-
required: true;
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
js_other: import("..").INiceErrorIdMetadata<IInspectErrorResult_JsOther, import("..").JSONSerializableValue> & {
|
|
39
|
-
context: {
|
|
40
|
-
required: true;
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
}>;
|
|
45
|
-
export declare const err_nice_handler: import("..").NiceErrorDomain<{
|
|
46
|
-
domain: string;
|
|
47
|
-
allDomains: [string, "err_nice"];
|
|
48
|
-
schema: {};
|
|
49
|
-
}>;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { INiceErrorOptions } from "../../NiceError/NiceError";
|
|
2
|
-
import type { INiceErrorDomainProps } from "../../NiceError/NiceError.types";
|
|
3
|
-
export declare const nice_error_test_options: INiceErrorOptions<INiceErrorDomainProps, keyof INiceErrorDomainProps["schema"]>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isVal: <T>(input: unknown, val: T) => input is T;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { NiceError } from "../NiceError/NiceError";
|
|
2
|
-
import type { INiceErrorDomainProps } from "../NiceError/NiceError.types";
|
|
3
|
-
import { NiceErrorHydrated } from "../NiceError/NiceErrorHydrated";
|
|
4
|
-
import type { NiceErrorDomain } from "../NiceErrorDefined/NiceErrorDefined";
|
|
5
|
-
/**
|
|
6
|
-
* Combines `castNiceError`, `is()`, and `hydrate()` in a single call — the
|
|
7
|
-
* idiomatic way to handle an unknown value arriving from a remote boundary
|
|
8
|
-
* (API response, message queue, IPC, etc.) when you have a specific domain in mind.
|
|
9
|
-
*
|
|
10
|
-
* - Casts `value` to a `NiceError` using `castNiceError`.
|
|
11
|
-
* - If the result belongs to `niceErrorDefined`'s domain (`is()` returns `true`),
|
|
12
|
-
* hydrates it and returns a fully-typed `NiceErrorHydrated`.
|
|
13
|
-
* - Otherwise returns the raw cast `NiceError` (which may be a `wasntNice` error
|
|
14
|
-
* if `value` was not a NiceError at all).
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* // In an Express error handler:
|
|
19
|
-
* app.use((err, req, res, next) => {
|
|
20
|
-
* const error = castAndHydrate(err, err_user_auth);
|
|
21
|
-
*
|
|
22
|
-
* if (err_user_auth.is(error)) {
|
|
23
|
-
* // error is NiceErrorHydrated — getContext / addId available
|
|
24
|
-
* const result = matchFirst(error, {
|
|
25
|
-
* invalid_credentials: ({ username }) => res.status(401).json({ username }),
|
|
26
|
-
* account_locked: () => res.status(403).json({ locked: true }),
|
|
27
|
-
* });
|
|
28
|
-
* if (result) return;
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* next(err);
|
|
32
|
-
* });
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export declare function castAndHydrate<ERR_DEF extends INiceErrorDomainProps>(value: unknown, niceErrorDefined: NiceErrorDomain<ERR_DEF>): NiceErrorHydrated<ERR_DEF, keyof ERR_DEF["schema"] & string> | NiceError;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { NiceError } from "../NiceError/NiceError";
|
|
2
|
-
/**
|
|
3
|
-
* Casts any unknown value into a `NiceError`.
|
|
4
|
-
*
|
|
5
|
-
* - If the value is already a `NiceError` instance, it is returned as-is.
|
|
6
|
-
* - If the value is a plain `Error`, it is wrapped with the original as `originError`.
|
|
7
|
-
* - If the value is a JSON-serialised `NiceError` object (e.g. from an API
|
|
8
|
-
* response), a best-effort `NiceError` is re-created from it.
|
|
9
|
-
* - For all other values, a generic `NiceError` is created with a descriptive
|
|
10
|
-
* message.
|
|
11
|
-
*
|
|
12
|
-
* After casting, use `NiceErrorDefined.is(error)` to narrow the error to a
|
|
13
|
-
* specific domain and access its strongly-typed ids and context.
|
|
14
|
-
*/
|
|
15
|
-
export declare const castNiceError: (error: unknown) => NiceError;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { NiceError } from "../../NiceError/NiceError";
|
|
2
|
-
import type { INiceErrorJsonObject, IRegularErrorJsonObject } from "../../NiceError/NiceError.types";
|
|
3
|
-
import type { EInspectErrorResultType } from "./inspectPotentialError.enums";
|
|
4
|
-
export interface IInspectErrorResult_Base<T extends EInspectErrorResultType> {
|
|
5
|
-
type: T;
|
|
6
|
-
}
|
|
7
|
-
export interface IInspectErrorResult_Nullish extends IInspectErrorResult_Base<EInspectErrorResultType.nullish> {
|
|
8
|
-
value: null | undefined;
|
|
9
|
-
}
|
|
10
|
-
export interface IInspectErrorResult_NiceErrorObject extends IInspectErrorResult_Base<EInspectErrorResultType.niceErrorObject> {
|
|
11
|
-
niceErrorObject: INiceErrorJsonObject;
|
|
12
|
-
}
|
|
13
|
-
export interface IInspectErrorResult_NiceError extends IInspectErrorResult_Base<EInspectErrorResultType.niceError> {
|
|
14
|
-
niceError: NiceError;
|
|
15
|
-
}
|
|
16
|
-
export interface IInspectErrorResult_JsError extends IInspectErrorResult_Base<EInspectErrorResultType.jsError> {
|
|
17
|
-
jsError: Error;
|
|
18
|
-
}
|
|
19
|
-
export interface IInspectErrorResult_JsErrorObject extends IInspectErrorResult_Base<EInspectErrorResultType.jsErrorObject> {
|
|
20
|
-
jsErrorObject: IRegularErrorJsonObject;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* JS DATA TYPES
|
|
24
|
-
*/
|
|
25
|
-
export interface IInspectErrorResult_JsDataType_String extends IInspectErrorResult_Base<EInspectErrorResultType.jsDataType> {
|
|
26
|
-
jsDataType: "string";
|
|
27
|
-
jsDataValue: string;
|
|
28
|
-
}
|
|
29
|
-
export interface IInspectErrorResult_JsDataType_Number extends IInspectErrorResult_Base<EInspectErrorResultType.jsDataType> {
|
|
30
|
-
jsDataType: "number";
|
|
31
|
-
jsDataValue: number;
|
|
32
|
-
}
|
|
33
|
-
export interface IInspectErrorResult_JsDataType_Boolean extends IInspectErrorResult_Base<EInspectErrorResultType.jsDataType> {
|
|
34
|
-
jsDataType: "boolean";
|
|
35
|
-
jsDataValue: boolean;
|
|
36
|
-
}
|
|
37
|
-
export interface IInspectErrorResult_JsDataType_Object extends IInspectErrorResult_Base<EInspectErrorResultType.jsDataType> {
|
|
38
|
-
jsDataType: "object";
|
|
39
|
-
jsDataValue: object;
|
|
40
|
-
}
|
|
41
|
-
export type TInspectErrorResult_JsDataType = IInspectErrorResult_JsDataType_String | IInspectErrorResult_JsDataType_Number | IInspectErrorResult_JsDataType_Boolean | IInspectErrorResult_JsDataType_Object;
|
|
42
|
-
/**
|
|
43
|
-
* Catch-all for any other JS data type that doesn't fit the above categories (e.g. symbol, bigint, function, etc.)
|
|
44
|
-
*/
|
|
45
|
-
export interface IInspectErrorResult_JsOther extends IInspectErrorResult_Base<EInspectErrorResultType.jsOther> {
|
|
46
|
-
jsDataValue: unknown;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Union of all possible results from inspectPotentialError
|
|
50
|
-
*/
|
|
51
|
-
export type TInspectErrorResult = IInspectErrorResult_Nullish | IInspectErrorResult_NiceErrorObject | IInspectErrorResult_NiceError | IInspectErrorResult_JsError | IInspectErrorResult_JsErrorObject | TInspectErrorResult_JsDataType | IInspectErrorResult_JsOther;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type INiceErrorJsonObject } from "../NiceError/NiceError.types";
|
|
2
|
-
/**
|
|
3
|
-
* Returns `true` if `obj` is a JSON-serialised `NiceError` object matching the
|
|
4
|
-
* current wire format (contextState-based errorData entries).
|
|
5
|
-
*
|
|
6
|
-
* Validates:
|
|
7
|
-
* - Top-level shape (`name`, `message`, `wasntNice`, `httpStatusCode`, `def`)
|
|
8
|
-
* - Each `errorData` entry has a `contextState` with a valid `kind` discriminant
|
|
9
|
-
* (`"no_serialization"` or `"unhydrated"`) — rejecting payloads in the old
|
|
10
|
-
* format (`context` / `serialized` fields) to prevent silent data corruption.
|
|
11
|
-
*/
|
|
12
|
-
export declare function isNiceErrorObject(obj: unknown): obj is INiceErrorJsonObject;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function jsErrorOrCastJsError(error: unknown, logMessage?: boolean): Error;
|