|
| 1 | +/** |
| 2 | + * A typed error surface: a stable code plus an optional human message and |
| 3 | + * structured details. Consumers parameterise `Code` with their own error-code |
| 4 | + * union (e.g. headless-buy codes) so the taxonomy stays with the consumer while |
| 5 | + * the pure extraction below is shared. |
| 6 | + */ |
| 7 | +export type TypedError<Code extends string> = { |
| 8 | + code: Code; |
| 9 | + message?: string; |
| 10 | + details?: Record<string, unknown>; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Type guard for a non-null object. |
| 15 | + * |
| 16 | + * @param value - The value to test. |
| 17 | + * @returns Whether the value is a record. |
| 18 | + */ |
| 19 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 20 | + return typeof value === 'object' && value !== null; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Best-effort human-readable message from an arbitrary thrown/native value. |
| 25 | + * |
| 26 | + * @param error - The caught value. |
| 27 | + * @returns The message, or `undefined` when none can be derived. |
| 28 | + */ |
| 29 | +export function getErrorMessage(error: unknown): string | undefined { |
| 30 | + if (error instanceof Error) { |
| 31 | + return error.message; |
| 32 | + } |
| 33 | + if (isRecord(error) && typeof error.message === 'string') { |
| 34 | + return error.message; |
| 35 | + } |
| 36 | + if (typeof error === 'string') { |
| 37 | + return error; |
| 38 | + } |
| 39 | + return undefined; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Extracts a caller-recognised typed error from an arbitrary thrown/native |
| 44 | + * value, when the value carries an explicit, valid code on one of the given |
| 45 | + * properties. Pure: performs no side effects and applies no fallback, so |
| 46 | + * callers keep full control over precedence (e.g. domain-specific special |
| 47 | + * cases) and the fallback code. |
| 48 | + * |
| 49 | + * @param error - The caught value. |
| 50 | + * @param options - The options. |
| 51 | + * @param options.isValidCode - Type guard identifying a recognised code. |
| 52 | + * @param options.codeProperties - Property names to read a code from, in |
| 53 | + * precedence order. Defaults to `['code']`. |
| 54 | + * @returns The typed error when an explicit valid code is present, else |
| 55 | + * `undefined`. |
| 56 | + */ |
| 57 | +export function extractExplicitTypedError<Code extends string>( |
| 58 | + error: unknown, |
| 59 | + { |
| 60 | + isValidCode, |
| 61 | + codeProperties = ['code'], |
| 62 | + }: { |
| 63 | + isValidCode: (value: unknown) => value is Code; |
| 64 | + codeProperties?: string[]; |
| 65 | + }, |
| 66 | +): TypedError<Code> | undefined { |
| 67 | + if (!isRecord(error)) { |
| 68 | + return undefined; |
| 69 | + } |
| 70 | + for (const property of codeProperties) { |
| 71 | + const candidate = error[property]; |
| 72 | + if (isValidCode(candidate)) { |
| 73 | + return { |
| 74 | + code: candidate, |
| 75 | + message: getErrorMessage(error), |
| 76 | + details: isRecord(error.details) ? error.details : undefined, |
| 77 | + }; |
| 78 | + } |
| 79 | + } |
| 80 | + return undefined; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Normalises an arbitrary thrown/native value into a {@link TypedError}, using |
| 85 | + * the caller's recognised codes and falling back to `fallbackCode` when no |
| 86 | + * explicit valid code is present. Pure. |
| 87 | + * |
| 88 | + * @param error - The caught value. |
| 89 | + * @param options - The options. |
| 90 | + * @param options.isValidCode - Type guard identifying a recognised code. |
| 91 | + * @param options.fallbackCode - Code used when no explicit valid code is found. |
| 92 | + * @param options.codeProperties - Property names to read a code from, in |
| 93 | + * precedence order. Defaults to `['code']`. |
| 94 | + * @returns The typed error. |
| 95 | + */ |
| 96 | +export function normalizeToTypedError<Code extends string>( |
| 97 | + error: unknown, |
| 98 | + { |
| 99 | + isValidCode, |
| 100 | + fallbackCode, |
| 101 | + codeProperties, |
| 102 | + }: { |
| 103 | + isValidCode: (value: unknown) => value is Code; |
| 104 | + fallbackCode: Code; |
| 105 | + codeProperties?: string[]; |
| 106 | + }, |
| 107 | +): TypedError<Code> { |
| 108 | + return ( |
| 109 | + extractExplicitTypedError(error, { isValidCode, codeProperties }) ?? { |
| 110 | + code: fallbackCode, |
| 111 | + message: getErrorMessage(error), |
| 112 | + } |
| 113 | + ); |
| 114 | +} |
0 commit comments