@bombillazo/error-x 0.2.1 → 0.3.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
@@ -596,41 +596,127 @@ declare class ErrorX extends Error {
596
596
  * ```
597
597
  */
598
598
  static fromJSON(serialized: SerializableError): ErrorX;
599
- }
600
-
601
- /**
602
- * Preset configurations for common errors organized by category.
603
- * Each preset includes httpStatus (for HTTP errors), code, name, message, and uiMessage.
604
- *
605
- * @example
606
- * ```typescript
607
- * import { ErrorX, ErrorPresets } from '@bombillazo/error-x'
608
- *
609
- * // Use preset directly
610
- * const error = new ErrorX(ErrorPresets.HTTP.NOT_FOUND)
611
- *
612
- * // Override preset values
613
- * const error = new ErrorX({
614
- * ...ErrorPresets.HTTP.NOT_FOUND,
615
- * message: 'User not found',
616
- * metadata: { userId: 123 }
617
- * })
618
- *
619
- * // Use with additional options
620
- * const error = new ErrorX({
621
- * ...ErrorPresets.HTTP.UNAUTHORIZED,
622
- * actions: [{ action: 'redirect', payload: { redirectURL: '/login' } }]
623
- * })
624
- * ```
625
- *
626
- * @public
627
- */
628
- declare const PRESETS: {
629
599
  /**
630
600
  * HTTP error presets for common HTTP status codes.
631
- * Includes both 4xx client errors and 5xx server errors.
601
+ *
602
+ * ## Features
603
+ * - **Pre-configured error templates** for common HTTP status codes (400-511)
604
+ * - **Type-safe** with TypeScript support
605
+ * - **Fully customizable** via destructuring and override pattern
606
+ * - **User-friendly messages** included for all presets
607
+ * - **Categorized by type** - all HTTP presets include `type: 'http'`
608
+ *
609
+ * ## Usage Patterns
610
+ *
611
+ * ### 1. Direct Usage
612
+ * Use a preset as-is without any modifications:
613
+ * ```typescript
614
+ * throw new ErrorX(ErrorX.HTTP.NOT_FOUND)
615
+ * // Result: 404 error with default message and UI message
616
+ * ```
617
+ *
618
+ * ### 2. Override Specific Fields
619
+ * Customize the error while keeping other preset values:
620
+ * ```typescript
621
+ * throw new ErrorX({
622
+ * ...ErrorX.HTTP.NOT_FOUND,
623
+ * message: 'User not found',
624
+ * metadata: { userId: 123 }
625
+ * })
626
+ * // Result: 404 error with custom message but keeps httpStatus, code, name, uiMessage, type
627
+ * ```
628
+ *
629
+ * ### 3. Add Metadata and Actions
630
+ * Enhance presets with additional context and behaviors:
631
+ * ```typescript
632
+ * throw new ErrorX({
633
+ * ...ErrorX.HTTP.UNAUTHORIZED,
634
+ * metadata: { attemptedAction: 'viewProfile', userId: 456 },
635
+ * actions: [
636
+ * { action: 'logout', payload: { clearStorage: true } },
637
+ * { action: 'redirect', payload: { redirectURL: '/login' } }
638
+ * ]
639
+ * })
640
+ * ```
641
+ *
642
+ * ### 4. Add Error Cause
643
+ * Chain errors by adding a cause:
644
+ * ```typescript
645
+ * try {
646
+ * // some operation
647
+ * } catch (originalError) {
648
+ * throw new ErrorX({
649
+ * ...ErrorX.HTTP.INTERNAL_SERVER_ERROR,
650
+ * cause: originalError,
651
+ * metadata: { operation: 'database-query' }
652
+ * })
653
+ * }
654
+ * ```
655
+ *
656
+ * ## Common HTTP Presets
657
+ *
658
+ * ### 4xx Client Errors
659
+ * - `BAD_REQUEST` (400) - Invalid request data
660
+ * - `UNAUTHORIZED` (401) - Authentication required
661
+ * - `FORBIDDEN` (403) - Insufficient permissions
662
+ * - `NOT_FOUND` (404) - Resource not found
663
+ * - `METHOD_NOT_ALLOWED` (405) - HTTP method not allowed
664
+ * - `CONFLICT` (409) - Resource conflict
665
+ * - `UNPROCESSABLE_ENTITY` (422) - Validation failed
666
+ * - `TOO_MANY_REQUESTS` (429) - Rate limit exceeded
667
+ *
668
+ * ### 5xx Server Errors
669
+ * - `INTERNAL_SERVER_ERROR` (500) - Unexpected server error
670
+ * - `NOT_IMPLEMENTED` (501) - Feature not implemented
671
+ * - `BAD_GATEWAY` (502) - Upstream server error
672
+ * - `SERVICE_UNAVAILABLE` (503) - Service temporarily down
673
+ * - `GATEWAY_TIMEOUT` (504) - Upstream timeout
674
+ *
675
+ * @example
676
+ * ```typescript
677
+ * // API endpoint example
678
+ * app.get('/users/:id', async (req, res) => {
679
+ * const user = await db.users.findById(req.params.id)
680
+ *
681
+ * if (!user) {
682
+ * throw new ErrorX({
683
+ * ...ErrorX.HTTP.NOT_FOUND,
684
+ * message: 'User not found',
685
+ * metadata: { userId: req.params.id }
686
+ * })
687
+ * }
688
+ *
689
+ * res.json(user)
690
+ * })
691
+ *
692
+ * // Authentication middleware example
693
+ * const requireAuth = (req, res, next) => {
694
+ * if (!req.user) {
695
+ * throw new ErrorX({
696
+ * ...ErrorX.HTTP.UNAUTHORIZED,
697
+ * actions: [
698
+ * { action: 'redirect', payload: { redirectURL: '/login' } }
699
+ * ]
700
+ * })
701
+ * }
702
+ * next()
703
+ * }
704
+ *
705
+ * // Rate limiting example
706
+ * if (isRateLimited(req.ip)) {
707
+ * throw new ErrorX({
708
+ * ...ErrorX.HTTP.TOO_MANY_REQUESTS,
709
+ * metadata: {
710
+ * ip: req.ip,
711
+ * retryAfter: 60
712
+ * }
713
+ * })
714
+ * }
715
+ * ```
716
+ *
717
+ * @public
632
718
  */
633
- readonly HTTP: {
719
+ static readonly HTTP: {
634
720
  readonly BAD_REQUEST: {
635
721
  httpStatus: number;
636
722
  code: string;
@@ -944,6 +1030,6 @@ declare const PRESETS: {
944
1030
  type: string;
945
1031
  };
946
1032
  };
947
- };
1033
+ }
948
1034
 
949
- export { type CustomAction, type ErrorAction, type ErrorMetadata, ErrorX, type ErrorXOptions, type HandlingTarget, HandlingTargets, type LogoutAction, type NotifyAction, PRESETS, type RedirectAction, type SerializableError };
1035
+ export { type CustomAction, type ErrorAction, type ErrorMetadata, ErrorX, type ErrorXOptions, type HandlingTarget, HandlingTargets, type LogoutAction, type NotifyAction, type RedirectAction, type SerializableError };