@bombillazo/error-x 0.2.2 → 0.4.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/dist/index.d.ts CHANGED
@@ -2,167 +2,22 @@
2
2
  * Metadata object containing additional context information for an error.
3
3
  * Can store any key-value pairs to provide extra debugging or business context.
4
4
  *
5
- * @example
5
+ * Users can use metadata to store application-specific behavior instructions if needed:
6
6
  * ```typescript
7
- * const metadata: ErrorMetadata = {
7
+ * const metadata = {
8
8
  * userId: 123,
9
9
  * operation: 'fetchUser',
10
- * retryCount: 3
11
- * }
12
- * ```
13
- *
14
- * @public
15
- */
16
- type ErrorMetadata = Record<string, any>;
17
- /**
18
- * Predefined display targets for error notifications and UI feedback.
19
- * These enum values provide consistent, type-safe options for where errors should be displayed.
20
- *
21
- * @public
22
- */
23
- declare enum HandlingTargets {
24
- MODAL = "modal",
25
- TOAST = "toast",
26
- INLINE = "inline",
27
- BANNER = "banner",
28
- CONSOLE = "console",
29
- LOGGER = "logger",
30
- NOTIFICATION = "notification"
31
- }
32
- /**
33
- * Display target type that allows both predefined enum values and custom strings.
34
- * This enables flexibility for custom UI components while providing standard options.
35
- *
36
- * @example
37
- * ```typescript
38
- * // Using predefined enum values
39
- * targets: [HandlingTargets.MODAL, HandlingTargets.TOAST]
40
- *
41
- * // Using custom strings
42
- * targets: ['custom-sidebar', 'my-notification-center']
43
- *
44
- * // Mixing both
45
- * targets: [HandlingTargets.MODAL, 'custom-popup', HandlingTargets.CONSOLE]
46
- * ```
47
- *
48
- * @public
49
- */
50
- type HandlingTarget = HandlingTargets | string;
51
- /**
52
- * Action to display notifications in specified UI targets.
53
- * Used to notify applications to handle error messages through the indicated display mechanisms.
54
- *
55
- * @example
56
- * ```typescript
57
- * {
58
- * action: 'notify',
59
- * payload: {
60
- * targets: [HandlingTargets.TOAST, 'custom-sidebar'],
61
- * title: 'Error occurred',
62
- * duration: 5000
63
- * }
64
- * }
65
- * ```
66
- *
67
- * @public
68
- */
69
- type NotifyAction = {
70
- action: 'notify';
71
- payload: {
72
- targets: HandlingTarget[];
73
- [key: string]: any;
74
- };
75
- };
76
- /**
77
- * Action to log out the current user when an error occurs.
78
- * Useful for authentication errors or session expiration.
79
- *
80
- * @example
81
- * ```typescript
82
- * {
83
- * action: 'logout',
84
- * payload: {
85
- * clearStorage: true,
86
- * redirectURL: '/login'
87
- * }
88
- * }
89
- * ```
90
- *
91
- * @public
92
- */
93
- type LogoutAction = {
94
- action: 'logout';
95
- payload?: {
96
- [key: string]: any;
97
- };
98
- };
99
- /**
100
- * Action to redirect the user to a different URL when an error occurs.
101
- * Commonly used for navigation after authentication errors or access denied scenarios.
102
- *
103
- * @example
104
- * ```typescript
105
- * {
106
- * action: 'redirect',
107
- * payload: {
108
- * redirectURL: '/login',
109
- * delay: 2000,
110
- * replace: true,
111
- * }
10
+ * retryCount: 3,
11
+ * // Application-specific behavior can be stored here:
12
+ * shouldNotify: true,
13
+ * notifyTargets: ['toast', 'banner'],
14
+ * redirectTo: '/login'
112
15
  * }
113
16
  * ```
114
17
  *
115
18
  * @public
116
19
  */
117
- type RedirectAction = {
118
- action: 'redirect';
119
- payload: {
120
- redirectURL: string;
121
- [key: string]: any;
122
- };
123
- };
124
- /**
125
- * Custom action type for application-specific actions.
126
- * This type is essential for proper TypeScript discrimination in the ErrorAction union.
127
- * Without this, TypeScript cannot properly distinguish between predefined and custom actions.
128
- *
129
- * @example
130
- * ```typescript
131
- * {
132
- * action: 'custom',
133
- * payload: {
134
- * type: 'analytics',
135
- * event: 'error_occurred',
136
- * category: 'authentication',
137
- * severity: 'high'
138
- * }
139
- * }
140
- *
141
- * {
142
- * action: 'custom',
143
- * payload: {
144
- * type: 'show-modal',
145
- * modalId: 'error-modal',
146
- * title: 'Error',
147
- * message: 'Something went wrong'
148
- * }
149
- * }
150
- * ```
151
- *
152
- * @public
153
- */
154
- type CustomAction = {
155
- action: 'custom';
156
- payload?: Record<string, any>;
157
- };
158
- /**
159
- * Union type of all possible error actions.
160
- * Includes predefined actions (NotifyAction, LogoutAction, RedirectAction)
161
- * and CustomAction for application-specific actions.
162
- *
163
- * @public
164
- */
165
- type ErrorAction = NotifyAction | LogoutAction | RedirectAction | CustomAction;
20
+ type ErrorXMetadata = Record<string, unknown>;
166
21
  /**
167
22
  * Configuration options for creating an ErrorX instance.
168
23
  * All properties are optional with sensible defaults.
@@ -178,6 +33,10 @@ type ErrorAction = NotifyAction | LogoutAction | RedirectAction | CustomAction;
178
33
  * // ✅ Works - object literal
179
34
  * const opts = { message: 'Error' }
180
35
  * new ErrorX(opts)
36
+ *
37
+ * // ✅ Works - with type-safe metadata
38
+ * type MyMeta = { userId: number; action: string };
39
+ * new ErrorX<MyMeta>({ metadata: { userId: 123, action: 'login' } })
181
40
  * ```
182
41
  *
183
42
  * If ErrorXOptions were a class, you would need to instantiate it:
@@ -192,25 +51,43 @@ type ErrorAction = NotifyAction | LogoutAction | RedirectAction | CustomAction;
192
51
  *
193
52
  * @public
194
53
  */
195
- type ErrorXOptions = {
54
+ type ErrorXOptions<TMetadata extends ErrorXMetadata = ErrorXMetadata> = {
196
55
  /** Technical error message (default: 'An error occurred') */
197
56
  message?: string;
198
57
  /** Error type/name (default: 'Error') */
199
58
  name?: string;
200
59
  /** Error identifier code (auto-generated from name if not provided) */
201
60
  code?: string | number;
202
- /** User-friendly message for UI display (default: undefined) */
61
+ /** User-friendly message for UI display */
203
62
  uiMessage?: string | undefined;
204
63
  /** Original error that caused this error (preserves error chain) */
205
64
  cause?: Error | unknown;
206
- /** Additional context and debugging information (default: undefined) */
207
- metadata?: ErrorMetadata;
208
- /** Actions to perform when this error occurs (default: undefined) */
209
- actions?: ErrorAction[];
210
- /** HTTP status code (100-599) for HTTP-related errors (default: undefined) */
65
+ /** Additional context and debugging information */
66
+ metadata?: TMetadata | undefined;
67
+ /** HTTP status code (100-599) for HTTP-related errors */
211
68
  httpStatus?: number | undefined;
212
69
  /** Error type for categorization */
213
70
  type?: string | undefined;
71
+ /** Source URL related to the error (API endpoint, page URL, resource URL) */
72
+ sourceUrl?: string | undefined;
73
+ /** Documentation URL for this specific error */
74
+ docsUrl?: string | undefined;
75
+ /** Where the error originated (service name, module, component) */
76
+ source?: string | undefined;
77
+ };
78
+ /**
79
+ * Simplified representation of an error cause for serialization.
80
+ * Used to store error chain information without circular references.
81
+ *
82
+ * @public
83
+ */
84
+ type ErrorXCause = {
85
+ /** Error message */
86
+ message: string;
87
+ /** Error name (optional) */
88
+ name?: string;
89
+ /** Stack trace (optional) */
90
+ stack?: string;
214
91
  };
215
92
  /**
216
93
  * JSON-serializable representation of an ErrorX instance.
@@ -226,21 +103,20 @@ type ErrorXOptions = {
226
103
  * stack: 'Error: Authentication failed.\n at login (auth.ts:42:15)',
227
104
  * metadata: { userId: 123, loginAttempt: 3 },
228
105
  * timestamp: '2024-01-15T10:30:45.123Z',
229
- * actions: [
230
- * { action: 'logout', payload: { clearStorage: true } }
231
- * ],
232
106
  * cause: {
233
107
  * name: 'NetworkError',
234
108
  * message: 'Request timeout.',
235
- * code: 'NETWORK_TIMEOUT',
236
- * // ... other error properties
237
- * }
109
+ * stack: '...'
110
+ * },
111
+ * sourceUrl: 'https://api.example.com/auth',
112
+ * docsUrl: 'https://docs.example.com/errors#auth-failed',
113
+ * source: 'auth-service'
238
114
  * }
239
115
  * ```
240
116
  *
241
117
  * @public
242
118
  */
243
- type SerializableError = {
119
+ type ErrorXSerialized = {
244
120
  /** Error type/name */
245
121
  name: string;
246
122
  /** Technical error message */
@@ -252,24 +128,58 @@ type SerializableError = {
252
128
  /** Stack trace (optional) */
253
129
  stack?: string;
254
130
  /** Additional context and debugging information */
255
- metadata: ErrorMetadata | undefined;
131
+ metadata: ErrorXMetadata | undefined;
256
132
  /** ISO timestamp when error was created */
257
133
  timestamp: string;
258
- /** Actions to perform when this error occurs */
259
- actions?: ErrorAction[];
260
- /** Serialized cause error (for error chaining) */
261
- cause?: SerializableError;
134
+ /** Simplified cause error (for error chaining) */
135
+ cause?: ErrorXCause;
262
136
  /** HTTP status code for HTTP-related errors */
263
137
  httpStatus?: number;
264
138
  /** Error type for categorization */
265
139
  type?: string;
140
+ /** Source URL related to the error */
141
+ sourceUrl?: string;
142
+ /** Documentation URL for this error */
143
+ docsUrl?: string;
144
+ /** Where the error originated */
145
+ source?: string;
266
146
  };
267
147
 
148
+ /**
149
+ * Configuration interface for ErrorX global settings
150
+ *
151
+ * @public
152
+ */
153
+ interface ErrorXConfig {
154
+ /** Default source identifier for all errors (e.g., service name, module name) */
155
+ source?: string;
156
+ /** Base URL for error documentation */
157
+ docsBaseURL?: string;
158
+ /** Mapping of error codes to documentation paths */
159
+ docsMap?: Record<string, string>;
160
+ /**
161
+ * Control stack trace cleaning behavior
162
+ * - true: Enable automatic stack trace cleaning (default)
163
+ * - false: Disable stack trace cleaning entirely
164
+ * - string[]: Custom patterns to match and remove from stack traces
165
+ */
166
+ cleanStack?: boolean | string[];
167
+ }
268
168
  /**
269
169
  * Enhanced Error class with rich metadata, type-safe error handling, and intelligent error conversion.
270
170
  *
271
171
  * @example
272
172
  * ```typescript
173
+ * // Configure globally (optional)
174
+ * ErrorX.configure({
175
+ * source: 'my-service',
176
+ * docsBaseURL: 'https://docs.example.com',
177
+ * docsMap: {
178
+ * 'AUTH_FAILED': 'errors/authentication',
179
+ * 'DB_ERROR': 'errors/database'
180
+ * }
181
+ * })
182
+ *
273
183
  * // Basic usage
274
184
  * const error = new ErrorX({ message: 'Database connection failed' })
275
185
  *
@@ -281,68 +191,103 @@ type SerializableError = {
281
191
  * uiMessage: 'Please check your credentials',
282
192
  * metadata: { userId: 123, loginAttempt: 3 }
283
193
  * })
194
+ *
195
+ * // With type-safe metadata
196
+ * type MyMetadata = { userId: number; action: string };
197
+ * const error = new ErrorX<MyMetadata>({
198
+ * message: 'Action failed',
199
+ * metadata: { userId: 123, action: 'delete' }
200
+ * })
201
+ * // error.metadata?.userId is typed as number
284
202
  * ```
285
203
  *
286
204
  * @public
287
205
  */
288
- declare class ErrorX extends Error {
206
+ declare class ErrorX<TMetadata extends ErrorXMetadata = ErrorXMetadata> extends Error {
207
+ /** Global configuration for all ErrorX instances */
208
+ private static _config;
289
209
  /** Error identifier code, auto-generated from name if not provided */
290
- readonly code: string;
210
+ code: string;
291
211
  /** User-friendly message suitable for display in UI */
292
- readonly uiMessage: string | undefined;
212
+ uiMessage: string | undefined;
293
213
  /** Additional context and metadata associated with the error */
294
- readonly metadata: ErrorMetadata | undefined;
214
+ metadata: TMetadata | undefined;
295
215
  /** Timestamp when the error was created */
296
- readonly timestamp: Date;
297
- /** Error actions for UI behavior and handling */
298
- readonly actions: ErrorAction[] | undefined;
216
+ timestamp: Date;
299
217
  /** HTTP status code (100-599) for HTTP-related errors */
300
- readonly httpStatus: number | undefined;
218
+ httpStatus: number | undefined;
301
219
  /** Error type for categorization */
302
- readonly type: string | undefined;
220
+ type: string | undefined;
221
+ /** Source URL related to the error (API endpoint, page URL, resource URL) */
222
+ sourceUrl: string | undefined;
223
+ /** Documentation URL for this specific error */
224
+ docsUrl: string | undefined;
225
+ /** Where the error originated (service name, module, component) */
226
+ source: string | undefined;
303
227
  /**
304
228
  * Creates a new ErrorX instance with enhanced error handling capabilities.
305
229
  *
306
- * @param messageOrOptions - Error message string, ErrorXOptions object, or any value to convert to ErrorX
307
- * @param additionalOptions - Additional options when first parameter is a string (optional)
230
+ * @param messageOrOptions - Error message string or ErrorXOptions object (optional)
308
231
  *
309
232
  * @example
310
233
  * ```typescript
234
+ * // Create with default message
235
+ * const error1 = new ErrorX()
236
+ *
311
237
  * // Create with string message only
312
- * const error1 = new ErrorX('Database query failed')
238
+ * const error2 = new ErrorX('Database query failed')
313
239
  *
314
- * // Create with string message and additional options
315
- * const error2 = new ErrorX('Database query failed', {
240
+ * // Create with options object
241
+ * const error3 = new ErrorX({
242
+ * message: 'Database query failed',
316
243
  * name: 'DatabaseError',
317
244
  * code: 'DB_QUERY_FAILED',
318
245
  * uiMessage: 'Unable to load data. Please try again.',
319
246
  * metadata: { query: 'SELECT * FROM users', timeout: 5000 }
320
247
  * })
321
248
  *
322
- * // Create with options object (backward compatible)
323
- * const error3 = new ErrorX({
324
- * message: 'Database query failed',
325
- * name: 'DatabaseError',
326
- * code: 'DB_QUERY_FAILED',
327
- * actions: [
328
- * { action: 'notify', payload: { targets: [HandlingTargets.TOAST] } }
329
- * ]
249
+ * // With type-safe metadata
250
+ * type MyMeta = { userId: number };
251
+ * const error4 = new ErrorX<MyMeta>({
252
+ * message: 'User action failed',
253
+ * metadata: { userId: 123 }
330
254
  * })
331
255
  *
332
- * // Create with unknown input (smart conversion)
256
+ * // For converting unknown errors, use ErrorX.from()
333
257
  * const apiError = { message: 'User not found', code: 404 }
334
- * const error4 = new ErrorX(apiError)
335
- *
336
- * // Create with no options (uses defaults)
337
- * const error5 = new ErrorX()
258
+ * const error5 = ErrorX.from(apiError)
338
259
  * ```
339
260
  */
340
- constructor(messageOrOptions?: string | ErrorXOptions | unknown, additionalOptions?: Partial<ErrorXOptions>);
261
+ constructor(messageOrOptions?: string | ErrorXOptions<TMetadata>);
341
262
  /**
342
263
  * Returns the default error name.
343
264
  * @returns Default error name 'Error'
344
265
  */
345
266
  private static getDefaultName;
267
+ /**
268
+ * Configure global ErrorX settings.
269
+ * This method allows you to set defaults for all ErrorX instances.
270
+ *
271
+ * @param config - Configuration object
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * ErrorX.configure({
276
+ * source: 'my-api-service',
277
+ * docsBaseURL: 'https://docs.example.com/errors',
278
+ * docsMap: {
279
+ * 'AUTH_FAILED': 'authentication-errors',
280
+ * 'DB_ERROR': 'database-errors'
281
+ * }
282
+ * })
283
+ * ```
284
+ */
285
+ static configure(config: ErrorXConfig): void;
286
+ /**
287
+ * Get the current global configuration.
288
+ * Returns null if no configuration has been set.
289
+ */
290
+ static getConfig(): ErrorXConfig | null;
346
291
  /**
347
292
  * Validates HTTP status code to ensure it's within valid range (100-599)
348
293
  *
@@ -412,21 +357,6 @@ declare class ErrorX extends Error {
412
357
  * ```
413
358
  */
414
359
  private static processErrorStack;
415
- /**
416
- * Formats error messages with proper capitalization and punctuation.
417
- * Ensures consistent message formatting across all ErrorX instances.
418
- *
419
- * @param message - Raw error message to format (optional)
420
- * @returns Formatted message with proper capitalization and punctuation
421
- *
422
- * @example
423
- * ```typescript
424
- * formatMessage('database connection failed') // 'Database connection failed.'
425
- * formatMessage('user not found. please check credentials') // 'User not found. Please check credentials.'
426
- * formatMessage() // 'An error occurred'
427
- * ```
428
- */
429
- private static formatMessage;
430
360
  /**
431
361
  * Creates a new ErrorX instance with additional metadata merged with existing metadata.
432
362
  * The original error properties are preserved while extending the metadata.
@@ -448,7 +378,7 @@ declare class ErrorX extends Error {
448
378
  * // Result: metadata = { endpoint: '/users', retryCount: 3, userId: 123 }
449
379
  * ```
450
380
  */
451
- withMetadata(additionalMetadata: ErrorMetadata): ErrorX;
381
+ withMetadata(additionalMetadata: Partial<TMetadata>): ErrorX<TMetadata>;
452
382
  /**
453
383
  * Type guard that checks if a value is an ErrorX instance.
454
384
  *
@@ -467,7 +397,7 @@ declare class ErrorX extends Error {
467
397
  * }
468
398
  * ```
469
399
  */
470
- static isErrorX(value: unknown): value is ErrorX;
400
+ static isErrorX<TMetadata extends ErrorXMetadata = ErrorXMetadata>(value: unknown): value is ErrorX<TMetadata>;
471
401
  /**
472
402
  * Converts unknown input into ErrorXOptions with intelligent property extraction.
473
403
  * Handles strings, regular Error objects, API response objects, and unknown values.
@@ -488,10 +418,10 @@ declare class ErrorX extends Error {
488
418
  * @example
489
419
  * ```typescript
490
420
  * // Convert string error
491
- * const error1 = ErrorX.toErrorX('Something went wrong')
421
+ * const error1 = ErrorX.from('Something went wrong')
492
422
  *
493
423
  * // Convert regular Error
494
- * const error2 = ErrorX.toErrorX(new Error('Database failed'))
424
+ * const error2 = ErrorX.from(new Error('Database failed'))
495
425
  *
496
426
  * // Convert API response object
497
427
  * const apiError = {
@@ -499,26 +429,13 @@ declare class ErrorX extends Error {
499
429
  * code: 'USER_404',
500
430
  * statusText: 'Not Found'
501
431
  * }
502
- * const error3 = ErrorX.toErrorX(apiError)
503
- * ```
504
- */
505
- static toErrorX(error: unknown): ErrorX;
506
- /**
507
- * Public wrapper for processing error stack traces with delimiter.
508
- * Delegates to the private processErrorStack method for implementation.
509
- *
510
- * @param error - Error whose stack to process
511
- * @param delimiter - String to search for in stack lines
512
- * @returns Processed stack trace starting after the delimiter
513
- *
514
- * @example
515
- * ```typescript
516
- * const error = new Error('Something failed')
517
- * const cleanStack = ErrorX.processStack(error, 'my-app-entry')
518
- * // Returns stack trace starting after the line containing 'my-app-entry'
432
+ * const error3 = ErrorX.from(apiError)
519
433
  * ```
520
434
  */
521
- static processStack(error: Error, delimiter: string): string;
435
+ static from<TMetadata extends ErrorXMetadata = ErrorXMetadata>(error: ErrorX<TMetadata>): ErrorX<TMetadata>;
436
+ static from(error: Error): ErrorX;
437
+ static from(error: string): ErrorX;
438
+ static from(error: unknown): ErrorX;
522
439
  /**
523
440
  * Creates a new ErrorX instance with cleaned stack trace using the specified delimiter.
524
441
  * Returns the same instance if no delimiter is provided or no stack is available.
@@ -533,7 +450,7 @@ declare class ErrorX extends Error {
533
450
  * // Returns new ErrorX with stack trace starting after 'database-layer'
534
451
  * ```
535
452
  */
536
- cleanStackTrace(delimiter?: string): ErrorX;
453
+ cleanStackTrace(delimiter?: string): ErrorX<TMetadata>;
537
454
  /**
538
455
  * Converts the ErrorX instance to a detailed string representation.
539
456
  * Includes error name, message, code, timestamp, metadata, and stack trace.
@@ -572,7 +489,7 @@ declare class ErrorX extends Error {
572
489
  * // Can be safely passed to JSON.stringify() or sent over network
573
490
  * ```
574
491
  */
575
- toJSON(): SerializableError;
492
+ toJSON(): ErrorXSerialized;
576
493
  /**
577
494
  * Deserializes a JSON object back into an ErrorX instance.
578
495
  * Recursively reconstructs the error chain and restores all properties.
@@ -595,54 +512,41 @@ declare class ErrorX extends Error {
595
512
  * // Fully restored ErrorX instance with all properties
596
513
  * ```
597
514
  */
598
- static fromJSON(serialized: SerializableError): ErrorX;
515
+ static fromJSON<TMetadata extends ErrorXMetadata = ErrorXMetadata>(serialized: ErrorXSerialized): ErrorX<TMetadata>;
599
516
  }
600
517
 
601
518
  /**
602
- * Preset configurations for common errors organized by category.
519
+ * HTTP error presets for common HTTP status codes.
603
520
  *
604
- * ## Features
605
- * - **Pre-configured error templates** for common HTTP status codes (400-511)
606
- * - **Type-safe** with TypeScript support
607
- * - **Fully customizable** via destructuring and override pattern
608
- * - **User-friendly messages** included for all presets
609
- * - **Categorized by type** - all HTTP presets include `type: 'http'`
610
- *
611
- * ## Available Categories
612
- * - **HTTP**: Common HTTP status codes (4xx client errors, 5xx server errors)
521
+ * These presets provide pre-configured error options for standard HTTP error responses,
522
+ * including appropriate status codes, error codes, names, messages (sentence case), and user-friendly UI messages.
613
523
  *
614
524
  * ## Usage Patterns
615
525
  *
616
- * ### 1. Direct Usage
617
- * Use a preset as-is without any modifications:
526
+ * ### 1. Use Preset Directly
527
+ * Create an error with all preset values:
618
528
  * ```typescript
619
- * import { ErrorX, PRESETS } from '@bombillazo/error-x'
620
- *
621
- * throw new ErrorX(PRESETS.HTTP.NOT_FOUND)
622
- * // Result: 404 error with default message and UI message
529
+ * throw new ErrorX(http.notFound)
530
+ * // Result: 404 error with message "Not found.", code, name, uiMessage, and type
623
531
  * ```
624
532
  *
625
533
  * ### 2. Override Specific Fields
626
534
  * Customize the error while keeping other preset values:
627
535
  * ```typescript
628
536
  * throw new ErrorX({
629
- * ...PRESETS.HTTP.NOT_FOUND,
537
+ * ...http.notFound,
630
538
  * message: 'User not found',
631
539
  * metadata: { userId: 123 }
632
540
  * })
633
541
  * // Result: 404 error with custom message but keeps httpStatus, code, name, uiMessage, type
634
542
  * ```
635
543
  *
636
- * ### 3. Add Metadata and Actions
637
- * Enhance presets with additional context and behaviors:
544
+ * ### 3. Add Metadata
545
+ * Enhance presets with additional context:
638
546
  * ```typescript
639
547
  * throw new ErrorX({
640
- * ...PRESETS.HTTP.UNAUTHORIZED,
641
- * metadata: { attemptedAction: 'viewProfile', userId: 456 },
642
- * actions: [
643
- * { action: 'logout', payload: { clearStorage: true } },
644
- * { action: 'redirect', payload: { redirectURL: '/login' } }
645
- * ]
548
+ * ...http.unauthorized,
549
+ * metadata: { attemptedAction: 'viewProfile', userId: 456 }
646
550
  * })
647
551
  * ```
648
552
  *
@@ -653,7 +557,7 @@ declare class ErrorX extends Error {
653
557
  * // some operation
654
558
  * } catch (originalError) {
655
559
  * throw new ErrorX({
656
- * ...PRESETS.HTTP.INTERNAL_SERVER_ERROR,
560
+ * ...http.internalServerError,
657
561
  * cause: originalError,
658
562
  * metadata: { operation: 'database-query' }
659
563
  * })
@@ -663,33 +567,31 @@ declare class ErrorX extends Error {
663
567
  * ## Common HTTP Presets
664
568
  *
665
569
  * ### 4xx Client Errors
666
- * - `BAD_REQUEST` (400) - Invalid request data
667
- * - `UNAUTHORIZED` (401) - Authentication required
668
- * - `FORBIDDEN` (403) - Insufficient permissions
669
- * - `NOT_FOUND` (404) - Resource not found
670
- * - `METHOD_NOT_ALLOWED` (405) - HTTP method not allowed
671
- * - `CONFLICT` (409) - Resource conflict
672
- * - `UNPROCESSABLE_ENTITY` (422) - Validation failed
673
- * - `TOO_MANY_REQUESTS` (429) - Rate limit exceeded
570
+ * - `badRequest` (400) - Invalid request data
571
+ * - `unauthorized` (401) - Authentication required
572
+ * - `forbidden` (403) - Insufficient permissions
573
+ * - `notFound` (404) - Resource not found
574
+ * - `methodNotAllowed` (405) - HTTP method not allowed
575
+ * - `conflict` (409) - Resource conflict
576
+ * - `unprocessableEntity` (422) - Validation failed
577
+ * - `tooManyRequests` (429) - Rate limit exceeded
674
578
  *
675
579
  * ### 5xx Server Errors
676
- * - `INTERNAL_SERVER_ERROR` (500) - Unexpected server error
677
- * - `NOT_IMPLEMENTED` (501) - Feature not implemented
678
- * - `BAD_GATEWAY` (502) - Upstream server error
679
- * - `SERVICE_UNAVAILABLE` (503) - Service temporarily down
680
- * - `GATEWAY_TIMEOUT` (504) - Upstream timeout
580
+ * - `internalServerError` (500) - Unexpected server error
581
+ * - `notImplemented` (501) - Feature not implemented
582
+ * - `badGateway` (502) - Upstream server error
583
+ * - `serviceUnavailable` (503) - Service temporarily down
584
+ * - `gatewayTimeout` (504) - Upstream timeout
681
585
  *
682
586
  * @example
683
587
  * ```typescript
684
- * import { ErrorX, PRESETS } from '@bombillazo/error-x'
685
- *
686
588
  * // API endpoint example
687
589
  * app.get('/users/:id', async (req, res) => {
688
590
  * const user = await db.users.findById(req.params.id)
689
591
  *
690
592
  * if (!user) {
691
593
  * throw new ErrorX({
692
- * ...PRESETS.HTTP.NOT_FOUND,
594
+ * ...http.notFound,
693
595
  * message: 'User not found',
694
596
  * metadata: { userId: req.params.id }
695
597
  * })
@@ -701,12 +603,7 @@ declare class ErrorX extends Error {
701
603
  * // Authentication middleware example
702
604
  * const requireAuth = (req, res, next) => {
703
605
  * if (!req.user) {
704
- * throw new ErrorX({
705
- * ...PRESETS.HTTP.UNAUTHORIZED,
706
- * actions: [
707
- * { action: 'redirect', payload: { redirectURL: '/login' } }
708
- * ]
709
- * })
606
+ * throw new ErrorX(http.unauthorized)
710
607
  * }
711
608
  * next()
712
609
  * }
@@ -714,7 +611,7 @@ declare class ErrorX extends Error {
714
611
  * // Rate limiting example
715
612
  * if (isRateLimited(req.ip)) {
716
613
  * throw new ErrorX({
717
- * ...PRESETS.HTTP.TOO_MANY_REQUESTS,
614
+ * ...http.tooManyRequests,
718
615
  * metadata: {
719
616
  * ip: req.ip,
720
617
  * retryAfter: 60
@@ -725,325 +622,319 @@ declare class ErrorX extends Error {
725
622
  *
726
623
  * @public
727
624
  */
728
- declare const PRESETS: {
729
- /**
730
- * HTTP error presets for common HTTP status codes.
731
- * Includes both 4xx client errors and 5xx server errors.
732
- */
733
- readonly HTTP: {
734
- readonly BAD_REQUEST: {
735
- httpStatus: number;
736
- code: string;
737
- name: string;
738
- message: string;
739
- uiMessage: string;
740
- type: string;
741
- };
742
- readonly UNAUTHORIZED: {
743
- httpStatus: number;
744
- code: string;
745
- name: string;
746
- message: string;
747
- uiMessage: string;
748
- type: string;
749
- };
750
- readonly PAYMENT_REQUIRED: {
751
- httpStatus: number;
752
- code: string;
753
- name: string;
754
- message: string;
755
- uiMessage: string;
756
- type: string;
757
- };
758
- readonly FORBIDDEN: {
759
- httpStatus: number;
760
- code: string;
761
- name: string;
762
- message: string;
763
- uiMessage: string;
764
- type: string;
765
- };
766
- readonly NOT_FOUND: {
767
- httpStatus: number;
768
- code: string;
769
- name: string;
770
- message: string;
771
- uiMessage: string;
772
- type: string;
773
- };
774
- readonly METHOD_NOT_ALLOWED: {
775
- httpStatus: number;
776
- code: string;
777
- name: string;
778
- message: string;
779
- uiMessage: string;
780
- type: string;
781
- };
782
- readonly NOT_ACCEPTABLE: {
783
- httpStatus: number;
784
- code: string;
785
- name: string;
786
- message: string;
787
- uiMessage: string;
788
- type: string;
789
- };
790
- readonly PROXY_AUTHENTICATION_REQUIRED: {
791
- httpStatus: number;
792
- code: string;
793
- name: string;
794
- message: string;
795
- uiMessage: string;
796
- type: string;
797
- };
798
- readonly REQUEST_TIMEOUT: {
799
- httpStatus: number;
800
- code: string;
801
- name: string;
802
- message: string;
803
- uiMessage: string;
804
- type: string;
805
- };
806
- readonly CONFLICT: {
807
- httpStatus: number;
808
- code: string;
809
- name: string;
810
- message: string;
811
- uiMessage: string;
812
- type: string;
813
- };
814
- readonly GONE: {
815
- httpStatus: number;
816
- code: string;
817
- name: string;
818
- message: string;
819
- uiMessage: string;
820
- type: string;
821
- };
822
- readonly LENGTH_REQUIRED: {
823
- httpStatus: number;
824
- code: string;
825
- name: string;
826
- message: string;
827
- uiMessage: string;
828
- type: string;
829
- };
830
- readonly PRECONDITION_FAILED: {
831
- httpStatus: number;
832
- code: string;
833
- name: string;
834
- message: string;
835
- uiMessage: string;
836
- type: string;
837
- };
838
- readonly PAYLOAD_TOO_LARGE: {
839
- httpStatus: number;
840
- code: string;
841
- name: string;
842
- message: string;
843
- uiMessage: string;
844
- type: string;
845
- };
846
- readonly URI_TOO_LONG: {
847
- httpStatus: number;
848
- code: string;
849
- name: string;
850
- message: string;
851
- uiMessage: string;
852
- type: string;
853
- };
854
- readonly UNSUPPORTED_MEDIA_TYPE: {
855
- httpStatus: number;
856
- code: string;
857
- name: string;
858
- message: string;
859
- uiMessage: string;
860
- type: string;
861
- };
862
- readonly RANGE_NOT_SATISFIABLE: {
863
- httpStatus: number;
864
- code: string;
865
- name: string;
866
- message: string;
867
- uiMessage: string;
868
- type: string;
869
- };
870
- readonly EXPECTATION_FAILED: {
871
- httpStatus: number;
872
- code: string;
873
- name: string;
874
- message: string;
875
- uiMessage: string;
876
- type: string;
877
- };
878
- readonly IM_A_TEAPOT: {
879
- httpStatus: number;
880
- code: string;
881
- name: string;
882
- message: string;
883
- uiMessage: string;
884
- type: string;
885
- };
886
- readonly UNPROCESSABLE_ENTITY: {
887
- httpStatus: number;
888
- code: string;
889
- name: string;
890
- message: string;
891
- uiMessage: string;
892
- type: string;
893
- };
894
- readonly LOCKED: {
895
- httpStatus: number;
896
- code: string;
897
- name: string;
898
- message: string;
899
- uiMessage: string;
900
- type: string;
901
- };
902
- readonly FAILED_DEPENDENCY: {
903
- httpStatus: number;
904
- code: string;
905
- name: string;
906
- message: string;
907
- uiMessage: string;
908
- type: string;
909
- };
910
- readonly TOO_EARLY: {
911
- httpStatus: number;
912
- code: string;
913
- name: string;
914
- message: string;
915
- uiMessage: string;
916
- type: string;
917
- };
918
- readonly UPGRADE_REQUIRED: {
919
- httpStatus: number;
920
- code: string;
921
- name: string;
922
- message: string;
923
- uiMessage: string;
924
- type: string;
925
- };
926
- readonly PRECONDITION_REQUIRED: {
927
- httpStatus: number;
928
- code: string;
929
- name: string;
930
- message: string;
931
- uiMessage: string;
932
- type: string;
933
- };
934
- readonly TOO_MANY_REQUESTS: {
935
- httpStatus: number;
936
- code: string;
937
- name: string;
938
- message: string;
939
- uiMessage: string;
940
- type: string;
941
- };
942
- readonly REQUEST_HEADER_FIELDS_TOO_LARGE: {
943
- httpStatus: number;
944
- code: string;
945
- name: string;
946
- message: string;
947
- uiMessage: string;
948
- type: string;
949
- };
950
- readonly UNAVAILABLE_FOR_LEGAL_REASONS: {
951
- httpStatus: number;
952
- code: string;
953
- name: string;
954
- message: string;
955
- uiMessage: string;
956
- type: string;
957
- };
958
- readonly INTERNAL_SERVER_ERROR: {
959
- httpStatus: number;
960
- code: string;
961
- name: string;
962
- message: string;
963
- uiMessage: string;
964
- type: string;
965
- };
966
- readonly NOT_IMPLEMENTED: {
967
- httpStatus: number;
968
- code: string;
969
- name: string;
970
- message: string;
971
- uiMessage: string;
972
- type: string;
973
- };
974
- readonly BAD_GATEWAY: {
975
- httpStatus: number;
976
- code: string;
977
- name: string;
978
- message: string;
979
- uiMessage: string;
980
- type: string;
981
- };
982
- readonly SERVICE_UNAVAILABLE: {
983
- httpStatus: number;
984
- code: string;
985
- name: string;
986
- message: string;
987
- uiMessage: string;
988
- type: string;
989
- };
990
- readonly GATEWAY_TIMEOUT: {
991
- httpStatus: number;
992
- code: string;
993
- name: string;
994
- message: string;
995
- uiMessage: string;
996
- type: string;
997
- };
998
- readonly HTTP_VERSION_NOT_SUPPORTED: {
999
- httpStatus: number;
1000
- code: string;
1001
- name: string;
1002
- message: string;
1003
- uiMessage: string;
1004
- type: string;
1005
- };
1006
- readonly VARIANT_ALSO_NEGOTIATES: {
1007
- httpStatus: number;
1008
- code: string;
1009
- name: string;
1010
- message: string;
1011
- uiMessage: string;
1012
- type: string;
1013
- };
1014
- readonly INSUFFICIENT_STORAGE: {
1015
- httpStatus: number;
1016
- code: string;
1017
- name: string;
1018
- message: string;
1019
- uiMessage: string;
1020
- type: string;
1021
- };
1022
- readonly LOOP_DETECTED: {
1023
- httpStatus: number;
1024
- code: string;
1025
- name: string;
1026
- message: string;
1027
- uiMessage: string;
1028
- type: string;
1029
- };
1030
- readonly NOT_EXTENDED: {
1031
- httpStatus: number;
1032
- code: string;
1033
- name: string;
1034
- message: string;
1035
- uiMessage: string;
1036
- type: string;
1037
- };
1038
- readonly NETWORK_AUTHENTICATION_REQUIRED: {
1039
- httpStatus: number;
1040
- code: string;
1041
- name: string;
1042
- message: string;
1043
- uiMessage: string;
1044
- type: string;
1045
- };
625
+ declare const http: {
626
+ readonly badRequest: {
627
+ httpStatus: number;
628
+ code: string;
629
+ name: string;
630
+ message: string;
631
+ uiMessage: string;
632
+ type: string;
633
+ };
634
+ readonly unauthorized: {
635
+ httpStatus: number;
636
+ code: string;
637
+ name: string;
638
+ message: string;
639
+ uiMessage: string;
640
+ type: string;
641
+ };
642
+ readonly paymentRequired: {
643
+ httpStatus: number;
644
+ code: string;
645
+ name: string;
646
+ message: string;
647
+ uiMessage: string;
648
+ type: string;
649
+ };
650
+ readonly forbidden: {
651
+ httpStatus: number;
652
+ code: string;
653
+ name: string;
654
+ message: string;
655
+ uiMessage: string;
656
+ type: string;
657
+ };
658
+ readonly notFound: {
659
+ httpStatus: number;
660
+ code: string;
661
+ name: string;
662
+ message: string;
663
+ uiMessage: string;
664
+ type: string;
665
+ };
666
+ readonly methodNotAllowed: {
667
+ httpStatus: number;
668
+ code: string;
669
+ name: string;
670
+ message: string;
671
+ uiMessage: string;
672
+ type: string;
673
+ };
674
+ readonly notAcceptable: {
675
+ httpStatus: number;
676
+ code: string;
677
+ name: string;
678
+ message: string;
679
+ uiMessage: string;
680
+ type: string;
681
+ };
682
+ readonly proxyAuthenticationRequired: {
683
+ httpStatus: number;
684
+ code: string;
685
+ name: string;
686
+ message: string;
687
+ uiMessage: string;
688
+ type: string;
689
+ };
690
+ readonly requestTimeout: {
691
+ httpStatus: number;
692
+ code: string;
693
+ name: string;
694
+ message: string;
695
+ uiMessage: string;
696
+ type: string;
697
+ };
698
+ readonly conflict: {
699
+ httpStatus: number;
700
+ code: string;
701
+ name: string;
702
+ message: string;
703
+ uiMessage: string;
704
+ type: string;
705
+ };
706
+ readonly gone: {
707
+ httpStatus: number;
708
+ code: string;
709
+ name: string;
710
+ message: string;
711
+ uiMessage: string;
712
+ type: string;
713
+ };
714
+ readonly lengthRequired: {
715
+ httpStatus: number;
716
+ code: string;
717
+ name: string;
718
+ message: string;
719
+ uiMessage: string;
720
+ type: string;
721
+ };
722
+ readonly preconditionFailed: {
723
+ httpStatus: number;
724
+ code: string;
725
+ name: string;
726
+ message: string;
727
+ uiMessage: string;
728
+ type: string;
729
+ };
730
+ readonly payloadTooLarge: {
731
+ httpStatus: number;
732
+ code: string;
733
+ name: string;
734
+ message: string;
735
+ uiMessage: string;
736
+ type: string;
737
+ };
738
+ readonly uriTooLong: {
739
+ httpStatus: number;
740
+ code: string;
741
+ name: string;
742
+ message: string;
743
+ uiMessage: string;
744
+ type: string;
745
+ };
746
+ readonly unsupportedMediaType: {
747
+ httpStatus: number;
748
+ code: string;
749
+ name: string;
750
+ message: string;
751
+ uiMessage: string;
752
+ type: string;
753
+ };
754
+ readonly rangeNotSatisfiable: {
755
+ httpStatus: number;
756
+ code: string;
757
+ name: string;
758
+ message: string;
759
+ uiMessage: string;
760
+ type: string;
761
+ };
762
+ readonly expectationFailed: {
763
+ httpStatus: number;
764
+ code: string;
765
+ name: string;
766
+ message: string;
767
+ uiMessage: string;
768
+ type: string;
769
+ };
770
+ readonly imATeapot: {
771
+ httpStatus: number;
772
+ code: string;
773
+ name: string;
774
+ message: string;
775
+ uiMessage: string;
776
+ type: string;
777
+ };
778
+ readonly unprocessableEntity: {
779
+ httpStatus: number;
780
+ code: string;
781
+ name: string;
782
+ message: string;
783
+ uiMessage: string;
784
+ type: string;
785
+ };
786
+ readonly locked: {
787
+ httpStatus: number;
788
+ code: string;
789
+ name: string;
790
+ message: string;
791
+ uiMessage: string;
792
+ type: string;
793
+ };
794
+ readonly failedDependency: {
795
+ httpStatus: number;
796
+ code: string;
797
+ name: string;
798
+ message: string;
799
+ uiMessage: string;
800
+ type: string;
801
+ };
802
+ readonly tooEarly: {
803
+ httpStatus: number;
804
+ code: string;
805
+ name: string;
806
+ message: string;
807
+ uiMessage: string;
808
+ type: string;
809
+ };
810
+ readonly upgradeRequired: {
811
+ httpStatus: number;
812
+ code: string;
813
+ name: string;
814
+ message: string;
815
+ uiMessage: string;
816
+ type: string;
817
+ };
818
+ readonly preconditionRequired: {
819
+ httpStatus: number;
820
+ code: string;
821
+ name: string;
822
+ message: string;
823
+ uiMessage: string;
824
+ type: string;
825
+ };
826
+ readonly tooManyRequests: {
827
+ httpStatus: number;
828
+ code: string;
829
+ name: string;
830
+ message: string;
831
+ uiMessage: string;
832
+ type: string;
833
+ };
834
+ readonly requestHeaderFieldsTooLarge: {
835
+ httpStatus: number;
836
+ code: string;
837
+ name: string;
838
+ message: string;
839
+ uiMessage: string;
840
+ type: string;
841
+ };
842
+ readonly unavailableForLegalReasons: {
843
+ httpStatus: number;
844
+ code: string;
845
+ name: string;
846
+ message: string;
847
+ uiMessage: string;
848
+ type: string;
849
+ };
850
+ readonly internalServerError: {
851
+ httpStatus: number;
852
+ code: string;
853
+ name: string;
854
+ message: string;
855
+ uiMessage: string;
856
+ type: string;
857
+ };
858
+ readonly notImplemented: {
859
+ httpStatus: number;
860
+ code: string;
861
+ name: string;
862
+ message: string;
863
+ uiMessage: string;
864
+ type: string;
865
+ };
866
+ readonly badGateway: {
867
+ httpStatus: number;
868
+ code: string;
869
+ name: string;
870
+ message: string;
871
+ uiMessage: string;
872
+ type: string;
873
+ };
874
+ readonly serviceUnavailable: {
875
+ httpStatus: number;
876
+ code: string;
877
+ name: string;
878
+ message: string;
879
+ uiMessage: string;
880
+ type: string;
881
+ };
882
+ readonly gatewayTimeout: {
883
+ httpStatus: number;
884
+ code: string;
885
+ name: string;
886
+ message: string;
887
+ uiMessage: string;
888
+ type: string;
889
+ };
890
+ readonly httpVersionNotSupported: {
891
+ httpStatus: number;
892
+ code: string;
893
+ name: string;
894
+ message: string;
895
+ uiMessage: string;
896
+ type: string;
897
+ };
898
+ readonly variantAlsoNegotiates: {
899
+ httpStatus: number;
900
+ code: string;
901
+ name: string;
902
+ message: string;
903
+ uiMessage: string;
904
+ type: string;
905
+ };
906
+ readonly insufficientStorage: {
907
+ httpStatus: number;
908
+ code: string;
909
+ name: string;
910
+ message: string;
911
+ uiMessage: string;
912
+ type: string;
913
+ };
914
+ readonly loopDetected: {
915
+ httpStatus: number;
916
+ code: string;
917
+ name: string;
918
+ message: string;
919
+ uiMessage: string;
920
+ type: string;
921
+ };
922
+ readonly notExtended: {
923
+ httpStatus: number;
924
+ code: string;
925
+ name: string;
926
+ message: string;
927
+ uiMessage: string;
928
+ type: string;
929
+ };
930
+ readonly networkAuthenticationRequired: {
931
+ httpStatus: number;
932
+ code: string;
933
+ name: string;
934
+ message: string;
935
+ uiMessage: string;
936
+ type: string;
1046
937
  };
1047
938
  };
1048
939
 
1049
- export { type CustomAction, type ErrorAction, type ErrorMetadata, ErrorX, type ErrorXOptions, type HandlingTarget, HandlingTargets, type LogoutAction, type NotifyAction, PRESETS, type RedirectAction, type SerializableError };
940
+ export { ErrorX, type ErrorXCause, type ErrorXConfig, type ErrorXMetadata, type ErrorXOptions, type ErrorXSerialized, http };