@codenhub/error 0.0.1 → 0.1.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/LICENSE +201 -201
- package/README.md +145 -52
- package/dist/bucket.js +167 -0
- package/dist/create-app-error.d.ts +33 -0
- package/dist/create-app-error.js +104 -0
- package/dist/index.d.ts +5 -53
- package/dist/index.js +4 -2
- package/dist/normalize.js +125 -0
- package/dist/registries/browser.d.ts +21 -5
- package/dist/registries/browser.js +70 -16
- package/dist/registries/index.d.ts +3 -3
- package/dist/registries/index.js +3 -3
- package/dist/registries/supabase.d.ts +22 -5
- package/dist/registries/supabase.js +118 -19
- package/dist/registry.d.ts +43 -0
- package/dist/registry.js +116 -0
- package/dist/result.d.ts +100 -0
- package/dist/result.js +90 -0
- package/dist/types.d.ts +286 -0
- package/package.json +8 -5
- package/dist/src-DrlQ39IZ.js +0 -322
- package/dist/types-D6n8W1xw.d.ts +0 -87
package/dist/registry.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { createFeedbackMapBucket, createPatternBucket, createPrefixBucket } from "./bucket.js";
|
|
2
|
+
//#region src/registry.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates an empty, isolated error registry.
|
|
5
|
+
*
|
|
6
|
+
* Optionally merges a list of preset registries into the newly created registry.
|
|
7
|
+
*
|
|
8
|
+
* @param presets - Optional list of existing registries to merge during creation.
|
|
9
|
+
* @returns A new, mutable ErrorRegistry instance.
|
|
10
|
+
*/
|
|
11
|
+
const createErrorRegistry = (presets) => {
|
|
12
|
+
const codes = createFeedbackMapBucket();
|
|
13
|
+
const names = createFeedbackMapBucket();
|
|
14
|
+
const messages = createFeedbackMapBucket();
|
|
15
|
+
const prefixes = createPrefixBucket();
|
|
16
|
+
const patterns = createPatternBucket();
|
|
17
|
+
const registry = {
|
|
18
|
+
codes,
|
|
19
|
+
names,
|
|
20
|
+
messages,
|
|
21
|
+
prefixes,
|
|
22
|
+
patterns,
|
|
23
|
+
clear() {
|
|
24
|
+
codes.clear();
|
|
25
|
+
names.clear();
|
|
26
|
+
messages.clear();
|
|
27
|
+
prefixes.clear();
|
|
28
|
+
patterns.clear();
|
|
29
|
+
},
|
|
30
|
+
merge(sourceRegistry) {
|
|
31
|
+
for (const [identifier, feedback] of sourceRegistry.codes.values()) codes.add(identifier, feedback);
|
|
32
|
+
for (const [identifier, feedback] of sourceRegistry.names.values()) names.add(identifier, feedback);
|
|
33
|
+
for (const [identifier, feedback] of sourceRegistry.messages.values()) messages.add(identifier, feedback);
|
|
34
|
+
for (const { prefix, ...feedback } of sourceRegistry.prefixes.values()) prefixes.add(prefix, feedback);
|
|
35
|
+
for (const { pattern, ...feedback } of sourceRegistry.patterns.values()) patterns.add(pattern, feedback);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
if (presets) for (const preset of presets) registry.merge(preset);
|
|
39
|
+
return registry;
|
|
40
|
+
};
|
|
41
|
+
let activeRegistry = createErrorRegistry();
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves the active global error registry.
|
|
44
|
+
*
|
|
45
|
+
* This registry is used as the default classification source for `createAppError`.
|
|
46
|
+
*
|
|
47
|
+
* @returns The current active ErrorRegistry.
|
|
48
|
+
*/
|
|
49
|
+
const getErrorRegistry = () => {
|
|
50
|
+
return activeRegistry;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Sets the active global error registry.
|
|
54
|
+
*
|
|
55
|
+
* Allows consumers to replace the default registry at application initialization.
|
|
56
|
+
* Throws a TypeError if the provided value is null or not an object.
|
|
57
|
+
*
|
|
58
|
+
* @param registry - The ErrorRegistry instance to set as active.
|
|
59
|
+
* @throws TypeError - If the parameter is not a valid ErrorRegistry.
|
|
60
|
+
*/
|
|
61
|
+
const setErrorRegistry = (registry) => {
|
|
62
|
+
if (typeof registry !== "object" || registry === null) throw new TypeError("Error registry must be an object.");
|
|
63
|
+
activeRegistry = registry;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Wraps an ErrorRegistry in a read-only Proxy to prevent any future mutations.
|
|
67
|
+
*
|
|
68
|
+
* All mutating methods (such as `add`, `addList`, `clear`, `delete`) will throw a TypeError
|
|
69
|
+
* if called on a frozen registry bucket. The returned type is `ReadonlyErrorRegistry`,
|
|
70
|
+
* which only exposes the read-facing bucket surface at the type level.
|
|
71
|
+
*
|
|
72
|
+
* @param registry - The ErrorRegistry instance to freeze.
|
|
73
|
+
* @returns A read-only `ReadonlyErrorRegistry` instance.
|
|
74
|
+
*/
|
|
75
|
+
const freezeRegistry = (registry) => {
|
|
76
|
+
const throwReadOnly = () => {
|
|
77
|
+
throw new TypeError("Cannot modify a read-only error registry.");
|
|
78
|
+
};
|
|
79
|
+
const freezeBucket = (bucket) => {
|
|
80
|
+
return new Proxy(bucket, {
|
|
81
|
+
get(target, prop, receiver) {
|
|
82
|
+
if (prop === "add" || prop === "addList" || prop === "clear" || prop === "delete") return throwReadOnly;
|
|
83
|
+
return Reflect.get(target, prop, receiver);
|
|
84
|
+
},
|
|
85
|
+
set() {
|
|
86
|
+
throwReadOnly();
|
|
87
|
+
return false;
|
|
88
|
+
},
|
|
89
|
+
defineProperty() {
|
|
90
|
+
throwReadOnly();
|
|
91
|
+
return false;
|
|
92
|
+
},
|
|
93
|
+
deleteProperty() {
|
|
94
|
+
throwReadOnly();
|
|
95
|
+
return false;
|
|
96
|
+
},
|
|
97
|
+
preventExtensions() {
|
|
98
|
+
throwReadOnly();
|
|
99
|
+
return false;
|
|
100
|
+
},
|
|
101
|
+
setPrototypeOf() {
|
|
102
|
+
throwReadOnly();
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
return Object.freeze({
|
|
108
|
+
codes: freezeBucket(registry.codes),
|
|
109
|
+
names: freezeBucket(registry.names),
|
|
110
|
+
messages: freezeBucket(registry.messages),
|
|
111
|
+
prefixes: freezeBucket(registry.prefixes),
|
|
112
|
+
patterns: freezeBucket(registry.patterns)
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
//#endregion
|
|
116
|
+
export { createErrorRegistry, freezeRegistry, getErrorRegistry, setErrorRegistry };
|
package/dist/result.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { AppError, AppErrorOptions } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/result.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Represents a successful result value holding the resolved data.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type of the value wrapped in the success result.
|
|
8
|
+
*/
|
|
9
|
+
interface Ok<T> {
|
|
10
|
+
readonly ok: true;
|
|
11
|
+
readonly value: T;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents a failed result value wrapping a normalized AppError.
|
|
15
|
+
*/
|
|
16
|
+
interface Err {
|
|
17
|
+
readonly ok: false;
|
|
18
|
+
readonly error: AppError;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A Result type representing either a successful outcome (`Ok<T>`) or a failure outcome (`Err`).
|
|
22
|
+
* Useful for handling asynchronous or fallible operations without throwing exceptions.
|
|
23
|
+
*
|
|
24
|
+
* @typeParam T - The type of the value returned on success.
|
|
25
|
+
*/
|
|
26
|
+
type Result<T> = Ok<T> | Err;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a successful Result instance wrapping the provided value.
|
|
29
|
+
* If no value is provided, returns an Ok<void> result.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam T - The type of the value.
|
|
32
|
+
* @param value - The optional success value to wrap.
|
|
33
|
+
* @returns An Ok result object.
|
|
34
|
+
*/
|
|
35
|
+
declare function ok(): Ok<void>;
|
|
36
|
+
declare function ok<T>(value: T): Ok<T>;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a failed Result instance wrapping a normalized AppError.
|
|
39
|
+
*
|
|
40
|
+
* If the input error is a string, it is automatically treated as the fallback message.
|
|
41
|
+
*
|
|
42
|
+
* @param error - The raw error value to normalize.
|
|
43
|
+
* @param options - Configuration options for AppError normalization.
|
|
44
|
+
* @returns An Err result object.
|
|
45
|
+
*/
|
|
46
|
+
declare const err: (error: unknown, options?: AppErrorOptions) => Err;
|
|
47
|
+
/**
|
|
48
|
+
* Unwraps a Result, returning the value if successful, or throwing the normalized AppError if failed.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam T - The type of the value.
|
|
51
|
+
* @param result - The Result instance to unwrap.
|
|
52
|
+
* @returns The unwrapped success value.
|
|
53
|
+
* @throws AppError - If the result is an Err.
|
|
54
|
+
*/
|
|
55
|
+
declare const unwrap: <T>(result: Result<T>) => T;
|
|
56
|
+
/**
|
|
57
|
+
* Maps the success value of a Result using the provided mapper function.
|
|
58
|
+
*
|
|
59
|
+
* @typeParam T - The type of the original value.
|
|
60
|
+
* @typeParam U - The type of the mapped value.
|
|
61
|
+
* @param result - The Result instance to map.
|
|
62
|
+
* @param mapper - The function to map the success value.
|
|
63
|
+
* @returns A new Result instance with the mapped value or the original Err.
|
|
64
|
+
*/
|
|
65
|
+
declare const map: <T, U>(result: Result<T>, mapper: (value: T) => U) => Result<U>;
|
|
66
|
+
/**
|
|
67
|
+
* Pattern matches on a Result, executing the corresponding callback based on the outcome.
|
|
68
|
+
*
|
|
69
|
+
* @typeParam T - The type of the success value.
|
|
70
|
+
* @typeParam U - The type of the return value from the callbacks.
|
|
71
|
+
* @param result - The Result instance to match.
|
|
72
|
+
* @param callbacks - An object containing onOk and onErr callback functions.
|
|
73
|
+
* @returns The value returned by the executed callback.
|
|
74
|
+
*/
|
|
75
|
+
declare const match: <T, U>(result: Result<T>, callbacks: {
|
|
76
|
+
readonly onOk: (value: T) => U;
|
|
77
|
+
readonly onErr: (error: AppError) => U;
|
|
78
|
+
}) => U;
|
|
79
|
+
/**
|
|
80
|
+
* Maps the success value of a Result using the provided mapper function that returns another Result.
|
|
81
|
+
* Prevents nested Result structures like `Result<Result<U>>`.
|
|
82
|
+
*
|
|
83
|
+
* @typeParam T - The type of the original success value.
|
|
84
|
+
* @typeParam U - The type of the mapped success value.
|
|
85
|
+
* @param result - The Result instance to process.
|
|
86
|
+
* @param mapper - The function to map the success value to a new Result.
|
|
87
|
+
* @returns A new Result instance from the mapper or the original Err.
|
|
88
|
+
*/
|
|
89
|
+
declare const andThen: <T, U>(result: Result<T>, mapper: (value: T) => Result<U>) => Result<U>;
|
|
90
|
+
/**
|
|
91
|
+
* Unwraps a Result, returning the value if successful, or the provided fallback value if failed.
|
|
92
|
+
*
|
|
93
|
+
* @typeParam T - The type of the value.
|
|
94
|
+
* @param result - The Result instance to unwrap.
|
|
95
|
+
* @param fallback - The value to return if the result is an Err.
|
|
96
|
+
* @returns The success value or the fallback value.
|
|
97
|
+
*/
|
|
98
|
+
declare const unwrapOr: <T>(result: Result<T>, fallback: T) => T;
|
|
99
|
+
//#endregion
|
|
100
|
+
export { Err, Ok, Result, andThen, err, map, match, ok, unwrap, unwrapOr };
|
package/dist/result.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createAppError } from "./create-app-error.js";
|
|
2
|
+
//#region src/result.ts
|
|
3
|
+
function ok(value) {
|
|
4
|
+
return {
|
|
5
|
+
ok: true,
|
|
6
|
+
value
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates a failed Result instance wrapping a normalized AppError.
|
|
11
|
+
*
|
|
12
|
+
* If the input error is a string, it is automatically treated as the fallback message.
|
|
13
|
+
*
|
|
14
|
+
* @param error - The raw error value to normalize.
|
|
15
|
+
* @param options - Configuration options for AppError normalization.
|
|
16
|
+
* @returns An Err result object.
|
|
17
|
+
*/
|
|
18
|
+
const err = (error, options = {}) => ({
|
|
19
|
+
ok: false,
|
|
20
|
+
error: createAppError(error, typeof error === "string" ? {
|
|
21
|
+
fallbackMessage: error,
|
|
22
|
+
...options
|
|
23
|
+
} : options)
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Unwraps a Result, returning the value if successful, or throwing the normalized AppError if failed.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam T - The type of the value.
|
|
29
|
+
* @param result - The Result instance to unwrap.
|
|
30
|
+
* @returns The unwrapped success value.
|
|
31
|
+
* @throws AppError - If the result is an Err.
|
|
32
|
+
*/
|
|
33
|
+
const unwrap = (result) => {
|
|
34
|
+
if (!result.ok) throw result.error;
|
|
35
|
+
return result.value;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Maps the success value of a Result using the provided mapper function.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam T - The type of the original value.
|
|
41
|
+
* @typeParam U - The type of the mapped value.
|
|
42
|
+
* @param result - The Result instance to map.
|
|
43
|
+
* @param mapper - The function to map the success value.
|
|
44
|
+
* @returns A new Result instance with the mapped value or the original Err.
|
|
45
|
+
*/
|
|
46
|
+
const map = (result, mapper) => {
|
|
47
|
+
if (!result.ok) return result;
|
|
48
|
+
return ok(mapper(result.value));
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Pattern matches on a Result, executing the corresponding callback based on the outcome.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam T - The type of the success value.
|
|
54
|
+
* @typeParam U - The type of the return value from the callbacks.
|
|
55
|
+
* @param result - The Result instance to match.
|
|
56
|
+
* @param callbacks - An object containing onOk and onErr callback functions.
|
|
57
|
+
* @returns The value returned by the executed callback.
|
|
58
|
+
*/
|
|
59
|
+
const match = (result, callbacks) => {
|
|
60
|
+
if (result.ok) return callbacks.onOk(result.value);
|
|
61
|
+
return callbacks.onErr(result.error);
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Maps the success value of a Result using the provided mapper function that returns another Result.
|
|
65
|
+
* Prevents nested Result structures like `Result<Result<U>>`.
|
|
66
|
+
*
|
|
67
|
+
* @typeParam T - The type of the original success value.
|
|
68
|
+
* @typeParam U - The type of the mapped success value.
|
|
69
|
+
* @param result - The Result instance to process.
|
|
70
|
+
* @param mapper - The function to map the success value to a new Result.
|
|
71
|
+
* @returns A new Result instance from the mapper or the original Err.
|
|
72
|
+
*/
|
|
73
|
+
const andThen = (result, mapper) => {
|
|
74
|
+
if (!result.ok) return result;
|
|
75
|
+
return mapper(result.value);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Unwraps a Result, returning the value if successful, or the provided fallback value if failed.
|
|
79
|
+
*
|
|
80
|
+
* @typeParam T - The type of the value.
|
|
81
|
+
* @param result - The Result instance to unwrap.
|
|
82
|
+
* @param fallback - The value to return if the result is an Err.
|
|
83
|
+
* @returns The success value or the fallback value.
|
|
84
|
+
*/
|
|
85
|
+
const unwrapOr = (result, fallback) => {
|
|
86
|
+
if (!result.ok) return fallback;
|
|
87
|
+
return result.value;
|
|
88
|
+
};
|
|
89
|
+
//#endregion
|
|
90
|
+
export { andThen, err, map, match, ok, unwrap, unwrapOr };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A predictable, structured error shape representing a normalized application error.
|
|
4
|
+
*
|
|
5
|
+
* Implements the standard JavaScript `Error` interface and adds classification,
|
|
6
|
+
* localization support, and original error wrapping.
|
|
7
|
+
*/
|
|
8
|
+
interface AppError extends Error {
|
|
9
|
+
/**
|
|
10
|
+
* The classification of this error, denoting whether it was explicitly mapped
|
|
11
|
+
* as a known error, matched heuristically as unexpected, or completely unknown.
|
|
12
|
+
*/
|
|
13
|
+
readonly type: AppErrorType;
|
|
14
|
+
/**
|
|
15
|
+
* An optional localization key from matched registry feedback,
|
|
16
|
+
* suitable for displaying translated messages to consumers.
|
|
17
|
+
*/
|
|
18
|
+
readonly messageKey: string | null;
|
|
19
|
+
/**
|
|
20
|
+
* The optional source namespace, module, or package (e.g., `auth`, `supabase.database`)
|
|
21
|
+
* that originally produced the error.
|
|
22
|
+
*/
|
|
23
|
+
readonly source: AppErrorSource;
|
|
24
|
+
/**
|
|
25
|
+
* The original error value passed to the factory, or the original error retrieved
|
|
26
|
+
* from nested wrapper structures (such as `cause`, `originalError`, or `error` properties).
|
|
27
|
+
*/
|
|
28
|
+
readonly originalError: unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Indicates whether retrying the operation that failed with this error is likely to succeed.
|
|
31
|
+
*/
|
|
32
|
+
readonly isRetryable: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Options configuration for controlling how raw error values are normalized into `AppError` instances.
|
|
36
|
+
*/
|
|
37
|
+
interface AppErrorOptions {
|
|
38
|
+
/**
|
|
39
|
+
* The fallback error message to use when the error cannot be matched in the registry.
|
|
40
|
+
* Defaults to `DEFAULT_APP_ERROR_MESSAGE`.
|
|
41
|
+
*/
|
|
42
|
+
fallbackMessage?: string;
|
|
43
|
+
/**
|
|
44
|
+
* The specific error registry to query for classifications.
|
|
45
|
+
* Defaults to the global registry retrieved by `getErrorRegistry()`.
|
|
46
|
+
*/
|
|
47
|
+
registry?: ErrorRegistry;
|
|
48
|
+
/**
|
|
49
|
+
* The maximum depth to unwrap nested error wrappers (e.g. cause, originalError).
|
|
50
|
+
* Defaults to 3.
|
|
51
|
+
*/
|
|
52
|
+
maxDepth?: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The classification level of a normalized application error.
|
|
56
|
+
*
|
|
57
|
+
* - `"known"`: Matched explicitly in the registry (by code, name, exact message, or prefix).
|
|
58
|
+
* - `"unexpected"`: Matched heuristically via regex patterns in the registry.
|
|
59
|
+
* - `"unknown"`: Could not be resolved/classified by the active registry.
|
|
60
|
+
*/
|
|
61
|
+
type AppErrorType = "known" | "unexpected" | "unknown";
|
|
62
|
+
/**
|
|
63
|
+
* The namespace or subsystem label (e.g., `browser`, `supabase.auth`) representing
|
|
64
|
+
* where the error originated.
|
|
65
|
+
*/
|
|
66
|
+
type AppErrorSource = string | null;
|
|
67
|
+
/**
|
|
68
|
+
* The structured feedback returned when a registry matches an error.
|
|
69
|
+
* Defines the safe user-facing message and metadata to attach to the `AppError`.
|
|
70
|
+
*/
|
|
71
|
+
interface ErrorFeedback {
|
|
72
|
+
/**
|
|
73
|
+
* A safe, user-facing error message description.
|
|
74
|
+
*/
|
|
75
|
+
message: string;
|
|
76
|
+
/**
|
|
77
|
+
* An optional localization/translation key corresponding to the message.
|
|
78
|
+
*/
|
|
79
|
+
messageKey?: string;
|
|
80
|
+
/**
|
|
81
|
+
* An optional namespace or source label (e.g. `supabase.auth`).
|
|
82
|
+
*/
|
|
83
|
+
source?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Indicates if the operation can be safely retried.
|
|
86
|
+
*/
|
|
87
|
+
isRetryable?: boolean;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A registry bucket for storing and retrieving deterministic string-based error mappings
|
|
91
|
+
* (such as error codes, error names, or exact messages).
|
|
92
|
+
*/
|
|
93
|
+
interface ErrorRegistryBucket {
|
|
94
|
+
/**
|
|
95
|
+
* Adds or replaces error feedback for a given identifier.
|
|
96
|
+
* Trims whitespace and strips trailing punctuation from the identifier before storing.
|
|
97
|
+
*
|
|
98
|
+
* @param identifier - The exact identifier to match (e.g., `"23505"`, `"AbortError"`).
|
|
99
|
+
* @param feedback - The feedback metadata to attach to this identifier.
|
|
100
|
+
* @throws TypeError - If the identifier is empty or feedback is invalid.
|
|
101
|
+
*/
|
|
102
|
+
add(identifier: string, feedback: ErrorFeedback): void;
|
|
103
|
+
/**
|
|
104
|
+
* Adds or replaces multiple feedback entries from a list of tuple definitions.
|
|
105
|
+
*
|
|
106
|
+
* @param entries - List of tuples containing [identifier, feedback].
|
|
107
|
+
* @throws TypeError - If any identifier or feedback in the list is invalid.
|
|
108
|
+
*/
|
|
109
|
+
addList(entries: readonly (readonly [identifier: string, feedback: ErrorFeedback])[]): void;
|
|
110
|
+
/**
|
|
111
|
+
* Clears all mapped entries from this registry bucket.
|
|
112
|
+
*/
|
|
113
|
+
clear(): void;
|
|
114
|
+
/**
|
|
115
|
+
* Retrieves a defensive copy of the feedback mapping for the specified identifier.
|
|
116
|
+
*
|
|
117
|
+
* @param identifier - The exact error identifier to lookup.
|
|
118
|
+
* @returns The matched feedback mapping, or `undefined` if not found.
|
|
119
|
+
*/
|
|
120
|
+
get(identifier: string): ErrorFeedback | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* Removes error feedback mapped to the specified identifier.
|
|
123
|
+
* Trims whitespace and strips trailing punctuation from the identifier before deleting.
|
|
124
|
+
*
|
|
125
|
+
* @param identifier - The exact identifier to delete.
|
|
126
|
+
* @returns True if an element in the bucket existed and has been removed, or false if the element does not exist.
|
|
127
|
+
*/
|
|
128
|
+
delete(identifier: string): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Returns an iterator yielding defensive copies of all mappings stored in the bucket.
|
|
131
|
+
*
|
|
132
|
+
* @returns An iterator of [identifier, feedback] entries.
|
|
133
|
+
*/
|
|
134
|
+
values(): IterableIterator<[string, ErrorFeedback]>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Represents a registered message prefix definition and its feedback mapping.
|
|
138
|
+
*/
|
|
139
|
+
interface ErrorPrefixDefinition extends ErrorFeedback {
|
|
140
|
+
/**
|
|
141
|
+
* The message prefix matched after trimming trailing sentence punctuation.
|
|
142
|
+
*/
|
|
143
|
+
prefix: string;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Represents a registered regex pattern definition and its feedback mapping.
|
|
147
|
+
*/
|
|
148
|
+
interface ErrorPatternDefinition extends ErrorFeedback {
|
|
149
|
+
/**
|
|
150
|
+
* The RegExp instance used to evaluate heuristic error matches.
|
|
151
|
+
*/
|
|
152
|
+
pattern: RegExp;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A registry bucket for storing and matching error classifications based on message prefixes.
|
|
156
|
+
*/
|
|
157
|
+
interface ErrorPrefixRegistryBucket {
|
|
158
|
+
/**
|
|
159
|
+
* Adds or replaces feedback for a given message prefix.
|
|
160
|
+
* Strips trailing punctuation from the prefix before registration.
|
|
161
|
+
*
|
|
162
|
+
* @param prefix - The message prefix to register (e.g., `"Upload failed:"`).
|
|
163
|
+
* @param feedback - The feedback metadata to assign when matching this prefix.
|
|
164
|
+
* @throws TypeError - If the prefix is empty or feedback is invalid.
|
|
165
|
+
*/
|
|
166
|
+
add(prefix: string, feedback: ErrorFeedback): void;
|
|
167
|
+
/**
|
|
168
|
+
* Adds or replaces multiple prefix feedback definitions from a list of tuples.
|
|
169
|
+
*
|
|
170
|
+
* @param entries - List of tuples containing [prefix, feedback].
|
|
171
|
+
* @throws TypeError - If any entry is invalid.
|
|
172
|
+
*/
|
|
173
|
+
addList(entries: readonly (readonly [prefix: string, feedback: ErrorFeedback])[]): void;
|
|
174
|
+
/**
|
|
175
|
+
* Clears all registered prefix definitions from this bucket.
|
|
176
|
+
*/
|
|
177
|
+
clear(): void;
|
|
178
|
+
/**
|
|
179
|
+
* Removes prefix-based feedback definition for the specified prefix.
|
|
180
|
+
* Strips trailing punctuation from the prefix before deletion.
|
|
181
|
+
*
|
|
182
|
+
* @param prefix - The message prefix to delete.
|
|
183
|
+
* @returns True if the prefix definition existed and has been removed; otherwise, false.
|
|
184
|
+
*/
|
|
185
|
+
delete(prefix: string): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Returns defensive copies of all prefix definitions registered in this bucket.
|
|
188
|
+
*
|
|
189
|
+
* @returns Readonly list of prefix definitions.
|
|
190
|
+
*/
|
|
191
|
+
values(): readonly ErrorPrefixDefinition[];
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* A registry bucket for storing and matching error classifications heuristically using RegExp patterns.
|
|
195
|
+
*/
|
|
196
|
+
interface ErrorPatternRegistryBucket {
|
|
197
|
+
/**
|
|
198
|
+
* Adds or replaces a RegExp pattern and its feedback mapping.
|
|
199
|
+
*
|
|
200
|
+
* @param pattern - The regular expression to evaluate against error messages.
|
|
201
|
+
* @param feedback - The feedback metadata to attach on pattern match.
|
|
202
|
+
* @throws TypeError - If the pattern is not a RegExp or feedback is invalid.
|
|
203
|
+
*/
|
|
204
|
+
add(pattern: RegExp, feedback: ErrorFeedback): void;
|
|
205
|
+
/**
|
|
206
|
+
* Adds or replaces multiple RegExp pattern definitions from a list of tuples.
|
|
207
|
+
*
|
|
208
|
+
* @param entries - List of tuples containing [pattern, feedback].
|
|
209
|
+
* @throws TypeError - If any entry is invalid.
|
|
210
|
+
*/
|
|
211
|
+
addList(entries: readonly (readonly [pattern: RegExp, feedback: ErrorFeedback])[]): void;
|
|
212
|
+
/**
|
|
213
|
+
* Clears all registered patterns from this bucket.
|
|
214
|
+
*/
|
|
215
|
+
clear(): void;
|
|
216
|
+
/**
|
|
217
|
+
* Removes a RegExp pattern and its feedback mapping.
|
|
218
|
+
*
|
|
219
|
+
* @param pattern - The regular expression to remove.
|
|
220
|
+
* @returns True if the pattern existed and has been removed; otherwise, false.
|
|
221
|
+
*/
|
|
222
|
+
delete(pattern: RegExp): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Returns defensive copies of all pattern definitions registered in this bucket.
|
|
225
|
+
* RegExp instances are stateless since global and sticky flags are stripped on registration.
|
|
226
|
+
*
|
|
227
|
+
* @returns Readonly list of pattern definitions.
|
|
228
|
+
*/
|
|
229
|
+
values(): readonly ErrorPatternDefinition[];
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* A mutable collection of exact and heuristic error buckets used to classify unknown errors.
|
|
233
|
+
*/
|
|
234
|
+
interface ErrorRegistry {
|
|
235
|
+
/**
|
|
236
|
+
* Mappings for exact error codes, typically returned by database systems or APIs.
|
|
237
|
+
*/
|
|
238
|
+
codes: ErrorRegistryBucket;
|
|
239
|
+
/**
|
|
240
|
+
* Mappings for exact error names, typically found on native `Error.name` or `DOMException.name` properties.
|
|
241
|
+
*/
|
|
242
|
+
names: ErrorRegistryBucket;
|
|
243
|
+
/**
|
|
244
|
+
* Mappings for exact error messages (matched after trimming whitespace and trailing punctuation).
|
|
245
|
+
*/
|
|
246
|
+
messages: ErrorRegistryBucket;
|
|
247
|
+
/**
|
|
248
|
+
* Mappings for matching errors by message prefixes (e.g., longest prefix match wins).
|
|
249
|
+
*/
|
|
250
|
+
prefixes: ErrorPrefixRegistryBucket;
|
|
251
|
+
/**
|
|
252
|
+
* Heuristic mappings for matching errors using RegExp patterns. Matches are classified as `"unexpected"`.
|
|
253
|
+
*/
|
|
254
|
+
patterns: ErrorPatternRegistryBucket;
|
|
255
|
+
/**
|
|
256
|
+
* Clears all mappings from every bucket in this registry.
|
|
257
|
+
*/
|
|
258
|
+
clear(): void;
|
|
259
|
+
/**
|
|
260
|
+
* Merges all mappings from the source registry into this registry, overwriting matching identifiers.
|
|
261
|
+
*
|
|
262
|
+
* @param registry - The source registry to merge. Accepts both mutable and read-only registries.
|
|
263
|
+
*/
|
|
264
|
+
merge(registry: ErrorRegistry | ReadonlyErrorRegistry): void;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* A read-only view of an error registry returned by `freezeRegistry`.
|
|
268
|
+
*
|
|
269
|
+
* Exposes only the read-facing surface of each bucket. Mutation methods (`add`, `addList`,
|
|
270
|
+
* `clear`, `delete`) are not part of this type. Suitable as a preset source passed to
|
|
271
|
+
* `createErrorRegistry` or `merge`.
|
|
272
|
+
*/
|
|
273
|
+
interface ReadonlyErrorRegistry {
|
|
274
|
+
/** Read-only view of code mappings. */
|
|
275
|
+
readonly codes: Pick<ErrorRegistryBucket, "get" | "values">;
|
|
276
|
+
/** Read-only view of name mappings. */
|
|
277
|
+
readonly names: Pick<ErrorRegistryBucket, "get" | "values">;
|
|
278
|
+
/** Read-only view of message mappings. */
|
|
279
|
+
readonly messages: Pick<ErrorRegistryBucket, "get" | "values">;
|
|
280
|
+
/** Read-only view of prefix definitions. */
|
|
281
|
+
readonly prefixes: Pick<ErrorPrefixRegistryBucket, "values">;
|
|
282
|
+
/** Read-only view of pattern definitions. */
|
|
283
|
+
readonly patterns: Pick<ErrorPatternRegistryBucket, "values">;
|
|
284
|
+
}
|
|
285
|
+
//#endregion
|
|
286
|
+
export { AppError, AppErrorOptions, AppErrorSource, AppErrorType, ErrorFeedback, ErrorPatternDefinition, ErrorPatternRegistryBucket, ErrorPrefixDefinition, ErrorPrefixRegistryBucket, ErrorRegistry, ErrorRegistryBucket, ReadonlyErrorRegistry };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenhub/error",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Typed error normalization, result helpers, and opt-in registry presets for TypeScript apps.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
15
|
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
16
17
|
"main": "./dist/index.js",
|
|
17
18
|
"module": "./dist/index.js",
|
|
18
19
|
"types": "./dist/index.d.ts",
|
|
@@ -38,13 +39,15 @@
|
|
|
38
39
|
"access": "public"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@vitest/coverage-v8": "4.1.
|
|
42
|
-
"tsdown": "^0.22.
|
|
42
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
43
|
+
"tsdown": "^0.22.3",
|
|
43
44
|
"typescript": "^6.0.3",
|
|
44
|
-
"vitest": "^4.
|
|
45
|
+
"vitest": "^4.1.10"
|
|
45
46
|
},
|
|
46
47
|
"scripts": {
|
|
47
|
-
"build": "tsdown src/index.ts src/registries/index.ts src/registries/browser.ts src/registries/supabase.ts --format esm --dts --clean --no-fixed-extension",
|
|
48
|
+
"build": "tsdown src/index.ts src/registries/index.ts src/registries/browser.ts src/registries/supabase.ts --format esm --dts --clean --no-fixed-extension --unbundle",
|
|
49
|
+
"status:npm": "npm view @codenhub/error version dist-tags time --json && npm dist-tag ls @codenhub/error && npm access get status @codenhub/error",
|
|
50
|
+
"status:pack": "npm pack --dry-run",
|
|
48
51
|
"test": "vitest run",
|
|
49
52
|
"test:coverage": "vitest run --coverage",
|
|
50
53
|
"test:watch": "vitest",
|