|
1 | 1 | /** |
2 | | - * Error handling mode for the library. |
3 | | - * - 'throw': Throw errors (default, fail-fast) |
4 | | - * - 'warn': console.warn and continue |
5 | | - * - 'error': console.error and continue |
6 | | - * - 'silent': Suppress all errors |
7 | | - */ |
8 | | -export type ErrorHandlingMode = 'throw' | 'warn' | 'error' | 'silent'; |
9 | | - |
10 | | -let _errorHandlingMode: ErrorHandlingMode = 'throw'; |
11 | | - |
12 | | -/** |
13 | | - * Set the global error handling mode. |
14 | | - * @param mode - The error handling mode to use. |
15 | | - */ |
16 | | -export function setErrorHandling(mode: ErrorHandlingMode): void { |
17 | | - _errorHandlingMode = mode; |
18 | | -} |
19 | | - |
20 | | -/** |
21 | | - * Get the current error handling mode. |
22 | | - */ |
23 | | -export function getErrorHandling(): ErrorHandlingMode { |
24 | | - return _errorHandlingMode; |
25 | | -} |
26 | | - |
27 | | -/** |
28 | | - * Raise an error through the configured error handling mode. |
29 | | - * In 'throw' mode, throws the error. In other modes, logs and continues. |
| 2 | + * Centralized error dispatch. |
| 3 | + * All library errors go through this function for consistent messaging and easy grep. |
| 4 | + * @remarks Always throws — data structure errors are never recoverable. |
30 | 5 | * @param ErrorClass - The error constructor (Error, TypeError, RangeError, etc.) |
31 | 6 | * @param message - The error message. |
32 | 7 | */ |
33 | 8 | export function raise( |
34 | 9 | ErrorClass: new (msg: string) => Error, |
35 | 10 | message: string |
36 | | -): void { |
37 | | - switch (_errorHandlingMode) { |
38 | | - case 'throw': |
39 | | - throw new ErrorClass(message); |
40 | | - case 'warn': |
41 | | - console.warn(`[data-structure-typed] ${message}`); |
42 | | - break; |
43 | | - case 'error': |
44 | | - console.error(`[data-structure-typed] ${message}`); |
45 | | - break; |
46 | | - case 'silent': |
47 | | - break; |
48 | | - } |
| 11 | +): never { |
| 12 | + throw new ErrorClass(message); |
49 | 13 | } |
50 | 14 |
|
51 | 15 | /** |
|
0 commit comments