@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.cjs CHANGED
@@ -652,132 +652,244 @@ ${this.stack}`;
652
652
  }
653
653
  return error;
654
654
  }
655
- };
656
-
657
- // src/presets.ts
658
- var PRESETS = {
659
655
  /**
660
656
  * HTTP error presets for common HTTP status codes.
661
- * Includes both 4xx client errors and 5xx server errors.
657
+ *
658
+ * ## Features
659
+ * - **Pre-configured error templates** for common HTTP status codes (400-511)
660
+ * - **Type-safe** with TypeScript support
661
+ * - **Fully customizable** via destructuring and override pattern
662
+ * - **User-friendly messages** included for all presets
663
+ * - **Categorized by type** - all HTTP presets include `type: 'http'`
664
+ *
665
+ * ## Usage Patterns
666
+ *
667
+ * ### 1. Direct Usage
668
+ * Use a preset as-is without any modifications:
669
+ * ```typescript
670
+ * throw new ErrorX(ErrorX.HTTP.NOT_FOUND)
671
+ * // Result: 404 error with default message and UI message
672
+ * ```
673
+ *
674
+ * ### 2. Override Specific Fields
675
+ * Customize the error while keeping other preset values:
676
+ * ```typescript
677
+ * throw new ErrorX({
678
+ * ...ErrorX.HTTP.NOT_FOUND,
679
+ * message: 'User not found',
680
+ * metadata: { userId: 123 }
681
+ * })
682
+ * // Result: 404 error with custom message but keeps httpStatus, code, name, uiMessage, type
683
+ * ```
684
+ *
685
+ * ### 3. Add Metadata and Actions
686
+ * Enhance presets with additional context and behaviors:
687
+ * ```typescript
688
+ * throw new ErrorX({
689
+ * ...ErrorX.HTTP.UNAUTHORIZED,
690
+ * metadata: { attemptedAction: 'viewProfile', userId: 456 },
691
+ * actions: [
692
+ * { action: 'logout', payload: { clearStorage: true } },
693
+ * { action: 'redirect', payload: { redirectURL: '/login' } }
694
+ * ]
695
+ * })
696
+ * ```
697
+ *
698
+ * ### 4. Add Error Cause
699
+ * Chain errors by adding a cause:
700
+ * ```typescript
701
+ * try {
702
+ * // some operation
703
+ * } catch (originalError) {
704
+ * throw new ErrorX({
705
+ * ...ErrorX.HTTP.INTERNAL_SERVER_ERROR,
706
+ * cause: originalError,
707
+ * metadata: { operation: 'database-query' }
708
+ * })
709
+ * }
710
+ * ```
711
+ *
712
+ * ## Common HTTP Presets
713
+ *
714
+ * ### 4xx Client Errors
715
+ * - `BAD_REQUEST` (400) - Invalid request data
716
+ * - `UNAUTHORIZED` (401) - Authentication required
717
+ * - `FORBIDDEN` (403) - Insufficient permissions
718
+ * - `NOT_FOUND` (404) - Resource not found
719
+ * - `METHOD_NOT_ALLOWED` (405) - HTTP method not allowed
720
+ * - `CONFLICT` (409) - Resource conflict
721
+ * - `UNPROCESSABLE_ENTITY` (422) - Validation failed
722
+ * - `TOO_MANY_REQUESTS` (429) - Rate limit exceeded
723
+ *
724
+ * ### 5xx Server Errors
725
+ * - `INTERNAL_SERVER_ERROR` (500) - Unexpected server error
726
+ * - `NOT_IMPLEMENTED` (501) - Feature not implemented
727
+ * - `BAD_GATEWAY` (502) - Upstream server error
728
+ * - `SERVICE_UNAVAILABLE` (503) - Service temporarily down
729
+ * - `GATEWAY_TIMEOUT` (504) - Upstream timeout
730
+ *
731
+ * @example
732
+ * ```typescript
733
+ * // API endpoint example
734
+ * app.get('/users/:id', async (req, res) => {
735
+ * const user = await db.users.findById(req.params.id)
736
+ *
737
+ * if (!user) {
738
+ * throw new ErrorX({
739
+ * ...ErrorX.HTTP.NOT_FOUND,
740
+ * message: 'User not found',
741
+ * metadata: { userId: req.params.id }
742
+ * })
743
+ * }
744
+ *
745
+ * res.json(user)
746
+ * })
747
+ *
748
+ * // Authentication middleware example
749
+ * const requireAuth = (req, res, next) => {
750
+ * if (!req.user) {
751
+ * throw new ErrorX({
752
+ * ...ErrorX.HTTP.UNAUTHORIZED,
753
+ * actions: [
754
+ * { action: 'redirect', payload: { redirectURL: '/login' } }
755
+ * ]
756
+ * })
757
+ * }
758
+ * next()
759
+ * }
760
+ *
761
+ * // Rate limiting example
762
+ * if (isRateLimited(req.ip)) {
763
+ * throw new ErrorX({
764
+ * ...ErrorX.HTTP.TOO_MANY_REQUESTS,
765
+ * metadata: {
766
+ * ip: req.ip,
767
+ * retryAfter: 60
768
+ * }
769
+ * })
770
+ * }
771
+ * ```
772
+ *
773
+ * @public
662
774
  */
663
- HTTP: {
775
+ static HTTP = {
664
776
  // 4xx Client Errors
665
777
  BAD_REQUEST: {
666
778
  httpStatus: 400,
667
779
  code: "BAD_REQUEST",
668
- name: "BadRequestError",
669
- message: "Bad request",
780
+ name: "Bad Request Error",
781
+ message: "bad request",
670
782
  uiMessage: "The request could not be processed. Please check your input and try again.",
671
783
  type: "http"
672
784
  },
673
785
  UNAUTHORIZED: {
674
786
  httpStatus: 401,
675
787
  code: "UNAUTHORIZED",
676
- name: "UnauthorizedError",
677
- message: "Unauthorized",
788
+ name: "Unauthorized Error",
789
+ message: "unauthorized",
678
790
  uiMessage: "Authentication required. Please log in to continue.",
679
791
  type: "http"
680
792
  },
681
793
  PAYMENT_REQUIRED: {
682
794
  httpStatus: 402,
683
795
  code: "PAYMENT_REQUIRED",
684
- name: "PaymentRequiredError",
685
- message: "Payment required",
796
+ name: "Payment Required Error",
797
+ message: "payment required",
686
798
  uiMessage: "Payment is required to access this resource.",
687
799
  type: "http"
688
800
  },
689
801
  FORBIDDEN: {
690
802
  httpStatus: 403,
691
803
  code: "FORBIDDEN",
692
- name: "ForbiddenError",
693
- message: "Forbidden",
804
+ name: "Forbidden Error",
805
+ message: "forbidden",
694
806
  uiMessage: "You do not have permission to access this resource.",
695
807
  type: "http"
696
808
  },
697
809
  NOT_FOUND: {
698
810
  httpStatus: 404,
699
811
  code: "NOT_FOUND",
700
- name: "NotFoundError",
701
- message: "Not found",
812
+ name: "Not Found Error",
813
+ message: "not found",
702
814
  uiMessage: "The requested resource could not be found.",
703
815
  type: "http"
704
816
  },
705
817
  METHOD_NOT_ALLOWED: {
706
818
  httpStatus: 405,
707
819
  code: "METHOD_NOT_ALLOWED",
708
- name: "MethodNotAllowedError",
709
- message: "Method not allowed",
820
+ name: "Method Not Allowed Error",
821
+ message: "method not allowed",
710
822
  uiMessage: "This action is not allowed for the requested resource.",
711
823
  type: "http"
712
824
  },
713
825
  NOT_ACCEPTABLE: {
714
826
  httpStatus: 406,
715
827
  code: "NOT_ACCEPTABLE",
716
- name: "NotAcceptableError",
717
- message: "Not acceptable",
828
+ name: "Not Acceptable Error",
829
+ message: "not acceptable",
718
830
  uiMessage: "The requested format is not supported.",
719
831
  type: "http"
720
832
  },
721
833
  PROXY_AUTHENTICATION_REQUIRED: {
722
834
  httpStatus: 407,
723
835
  code: "PROXY_AUTHENTICATION_REQUIRED",
724
- name: "ProxyAuthenticationRequiredError",
725
- message: "Proxy authentication required",
836
+ name: "Proxy Authentication Required Error",
837
+ message: "proxy authentication required",
726
838
  uiMessage: "Proxy authentication is required to access this resource.",
727
839
  type: "http"
728
840
  },
729
841
  REQUEST_TIMEOUT: {
730
842
  httpStatus: 408,
731
843
  code: "REQUEST_TIMEOUT",
732
- name: "RequestTimeoutError",
733
- message: "Request timeout",
844
+ name: "Request Timeout Error",
845
+ message: "request timeout",
734
846
  uiMessage: "The request took too long to complete. Please try again.",
735
847
  type: "http"
736
848
  },
737
849
  CONFLICT: {
738
850
  httpStatus: 409,
739
851
  code: "CONFLICT",
740
- name: "ConflictError",
741
- message: "Conflict",
852
+ name: "Conflict Error",
853
+ message: "conflict",
742
854
  uiMessage: "The request conflicts with the current state. Please refresh and try again.",
743
855
  type: "http"
744
856
  },
745
857
  GONE: {
746
858
  httpStatus: 410,
747
859
  code: "GONE",
748
- name: "GoneError",
749
- message: "Gone",
860
+ name: "Gone Error",
861
+ message: "gone",
750
862
  uiMessage: "This resource is no longer available.",
751
863
  type: "http"
752
864
  },
753
865
  LENGTH_REQUIRED: {
754
866
  httpStatus: 411,
755
867
  code: "LENGTH_REQUIRED",
756
- name: "LengthRequiredError",
757
- message: "Length required",
868
+ name: "Length Required Error",
869
+ message: "length required",
758
870
  uiMessage: "The request is missing required length information.",
759
871
  type: "http"
760
872
  },
761
873
  PRECONDITION_FAILED: {
762
874
  httpStatus: 412,
763
875
  code: "PRECONDITION_FAILED",
764
- name: "PreconditionFailedError",
765
- message: "Precondition failed",
876
+ name: "Precondition Failed Error",
877
+ message: "precondition failed",
766
878
  uiMessage: "A required condition was not met. Please try again.",
767
879
  type: "http"
768
880
  },
769
881
  PAYLOAD_TOO_LARGE: {
770
882
  httpStatus: 413,
771
883
  code: "PAYLOAD_TOO_LARGE",
772
- name: "PayloadTooLargeError",
773
- message: "Payload too large",
884
+ name: "Payload Too Large Error",
885
+ message: "payload too large",
774
886
  uiMessage: "The request is too large. Please reduce the size and try again.",
775
887
  type: "http"
776
888
  },
777
889
  URI_TOO_LONG: {
778
890
  httpStatus: 414,
779
891
  code: "URI_TOO_LONG",
780
- name: "URITooLongError",
892
+ name: "URI Too Long Error",
781
893
  message: "URI too long",
782
894
  uiMessage: "The request URL is too long.",
783
895
  type: "http"
@@ -785,104 +897,104 @@ var PRESETS = {
785
897
  UNSUPPORTED_MEDIA_TYPE: {
786
898
  httpStatus: 415,
787
899
  code: "UNSUPPORTED_MEDIA_TYPE",
788
- name: "UnsupportedMediaTypeError",
789
- message: "Unsupported media type",
900
+ name: "Unsupported Media Type Error",
901
+ message: "unsupported media type",
790
902
  uiMessage: "The file type is not supported.",
791
903
  type: "http"
792
904
  },
793
905
  RANGE_NOT_SATISFIABLE: {
794
906
  httpStatus: 416,
795
907
  code: "RANGE_NOT_SATISFIABLE",
796
- name: "RangeNotSatisfiableError",
797
- message: "Range not satisfiable",
908
+ name: "Range Not Satisfiable Error",
909
+ message: "range not satisfiable",
798
910
  uiMessage: "The requested range cannot be satisfied.",
799
911
  type: "http"
800
912
  },
801
913
  EXPECTATION_FAILED: {
802
914
  httpStatus: 417,
803
915
  code: "EXPECTATION_FAILED",
804
- name: "ExpectationFailedError",
805
- message: "Expectation failed",
916
+ name: "Expectation Failed Error",
917
+ message: "expectation failed",
806
918
  uiMessage: "The server cannot meet the requirements of the request.",
807
919
  type: "http"
808
920
  },
809
921
  IM_A_TEAPOT: {
810
922
  httpStatus: 418,
811
923
  code: "IM_A_TEAPOT",
812
- name: "ImATeapotError",
813
- message: "I'm a teapot",
924
+ name: "Im A Teapot Error",
925
+ message: "i'm a teapot",
814
926
  uiMessage: "I'm a teapot and cannot brew coffee.",
815
927
  type: "http"
816
928
  },
817
929
  UNPROCESSABLE_ENTITY: {
818
930
  httpStatus: 422,
819
931
  code: "UNPROCESSABLE_ENTITY",
820
- name: "UnprocessableEntityError",
821
- message: "Unprocessable entity",
932
+ name: "Unprocessable Entity Error",
933
+ message: "unprocessable entity",
822
934
  uiMessage: "The request contains invalid data. Please check your input.",
823
935
  type: "http"
824
936
  },
825
937
  LOCKED: {
826
938
  httpStatus: 423,
827
939
  code: "LOCKED",
828
- name: "LockedError",
829
- message: "Locked",
940
+ name: "Locked Error",
941
+ message: "locked",
830
942
  uiMessage: "This resource is locked and cannot be modified.",
831
943
  type: "http"
832
944
  },
833
945
  FAILED_DEPENDENCY: {
834
946
  httpStatus: 424,
835
947
  code: "FAILED_DEPENDENCY",
836
- name: "FailedDependencyError",
837
- message: "Failed dependency",
948
+ name: "Failed Dependency Error",
949
+ message: "failed dependency",
838
950
  uiMessage: "The request failed due to a dependency error.",
839
951
  type: "http"
840
952
  },
841
953
  TOO_EARLY: {
842
954
  httpStatus: 425,
843
955
  code: "TOO_EARLY",
844
- name: "TooEarlyError",
845
- message: "Too early",
956
+ name: "Too Early Error",
957
+ message: "too early",
846
958
  uiMessage: "The request was sent too early. Please try again later.",
847
959
  type: "http"
848
960
  },
849
961
  UPGRADE_REQUIRED: {
850
962
  httpStatus: 426,
851
963
  code: "UPGRADE_REQUIRED",
852
- name: "UpgradeRequiredError",
853
- message: "Upgrade required",
964
+ name: "Upgrade Required Error",
965
+ message: "upgrade required",
854
966
  uiMessage: "Please upgrade to continue using this service.",
855
967
  type: "http"
856
968
  },
857
969
  PRECONDITION_REQUIRED: {
858
970
  httpStatus: 428,
859
971
  code: "PRECONDITION_REQUIRED",
860
- name: "PreconditionRequiredError",
861
- message: "Precondition required",
972
+ name: "Precondition Required Error",
973
+ message: "precondition required",
862
974
  uiMessage: "Required conditions are missing from the request.",
863
975
  type: "http"
864
976
  },
865
977
  TOO_MANY_REQUESTS: {
866
978
  httpStatus: 429,
867
979
  code: "TOO_MANY_REQUESTS",
868
- name: "TooManyRequestsError",
869
- message: "Too many requests",
980
+ name: "Too Many Requests Error",
981
+ message: "too many requests",
870
982
  uiMessage: "You have made too many requests. Please wait and try again.",
871
983
  type: "http"
872
984
  },
873
985
  REQUEST_HEADER_FIELDS_TOO_LARGE: {
874
986
  httpStatus: 431,
875
987
  code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
876
- name: "RequestHeaderFieldsTooLargeError",
877
- message: "Request header fields too large",
988
+ name: "Request Header Fields Too Large Error",
989
+ message: "request header fields too large",
878
990
  uiMessage: "The request headers are too large.",
879
991
  type: "http"
880
992
  },
881
993
  UNAVAILABLE_FOR_LEGAL_REASONS: {
882
994
  httpStatus: 451,
883
995
  code: "UNAVAILABLE_FOR_LEGAL_REASONS",
884
- name: "UnavailableForLegalReasonsError",
885
- message: "Unavailable for legal reasons",
996
+ name: "Unavailable For Legal Reasons Error",
997
+ message: "unavailable for legal reasons",
886
998
  uiMessage: "This content is unavailable for legal reasons.",
887
999
  type: "http"
888
1000
  },
@@ -890,47 +1002,47 @@ var PRESETS = {
890
1002
  INTERNAL_SERVER_ERROR: {
891
1003
  httpStatus: 500,
892
1004
  code: "INTERNAL_SERVER_ERROR",
893
- name: "InternalServerError",
894
- message: "Internal server error",
1005
+ name: "Internal Server Error",
1006
+ message: "internal server error",
895
1007
  uiMessage: "An unexpected error occurred. Please try again later.",
896
1008
  type: "http"
897
1009
  },
898
1010
  NOT_IMPLEMENTED: {
899
1011
  httpStatus: 501,
900
1012
  code: "NOT_IMPLEMENTED",
901
- name: "NotImplementedError",
902
- message: "Not implemented",
1013
+ name: "Not Implemented Error",
1014
+ message: "not implemented",
903
1015
  uiMessage: "This feature is not yet available.",
904
1016
  type: "http"
905
1017
  },
906
1018
  BAD_GATEWAY: {
907
1019
  httpStatus: 502,
908
1020
  code: "BAD_GATEWAY",
909
- name: "BadGatewayError",
910
- message: "Bad gateway",
1021
+ name: "Bad Gateway Error",
1022
+ message: "bad gateway",
911
1023
  uiMessage: "Unable to connect to the server. Please try again later.",
912
1024
  type: "http"
913
1025
  },
914
1026
  SERVICE_UNAVAILABLE: {
915
1027
  httpStatus: 503,
916
1028
  code: "SERVICE_UNAVAILABLE",
917
- name: "ServiceUnavailableError",
918
- message: "Service unavailable",
1029
+ name: "Service Unavailable Error",
1030
+ message: "service unavailable",
919
1031
  uiMessage: "The service is temporarily unavailable. Please try again later.",
920
1032
  type: "http"
921
1033
  },
922
1034
  GATEWAY_TIMEOUT: {
923
1035
  httpStatus: 504,
924
1036
  code: "GATEWAY_TIMEOUT",
925
- name: "GatewayTimeoutError",
926
- message: "Gateway timeout",
1037
+ name: "Gateway Timeout Error",
1038
+ message: "gateway timeout",
927
1039
  uiMessage: "The server took too long to respond. Please try again.",
928
1040
  type: "http"
929
1041
  },
930
1042
  HTTP_VERSION_NOT_SUPPORTED: {
931
1043
  httpStatus: 505,
932
1044
  code: "HTTP_VERSION_NOT_SUPPORTED",
933
- name: "HTTPVersionNotSupportedError",
1045
+ name: "HTTP Version Not Supported Error",
934
1046
  message: "HTTP version not supported",
935
1047
  uiMessage: "Your browser version is not supported.",
936
1048
  type: "http"
@@ -938,48 +1050,47 @@ var PRESETS = {
938
1050
  VARIANT_ALSO_NEGOTIATES: {
939
1051
  httpStatus: 506,
940
1052
  code: "VARIANT_ALSO_NEGOTIATES",
941
- name: "VariantAlsoNegotiatesError",
942
- message: "Variant also negotiates",
1053
+ name: "Variant Also Negotiates Error",
1054
+ message: "variant also negotiates",
943
1055
  uiMessage: "The server has an internal configuration error.",
944
1056
  type: "http"
945
1057
  },
946
1058
  INSUFFICIENT_STORAGE: {
947
1059
  httpStatus: 507,
948
1060
  code: "INSUFFICIENT_STORAGE",
949
- name: "InsufficientStorageError",
950
- message: "Insufficient storage",
1061
+ name: "Insufficient Storage Error",
1062
+ message: "insufficient storage",
951
1063
  uiMessage: "The server has insufficient storage to complete the request.",
952
1064
  type: "http"
953
1065
  },
954
1066
  LOOP_DETECTED: {
955
1067
  httpStatus: 508,
956
1068
  code: "LOOP_DETECTED",
957
- name: "LoopDetectedError",
958
- message: "Loop detected",
1069
+ name: "Loop Detected Error",
1070
+ message: "loop detected",
959
1071
  uiMessage: "The server detected an infinite loop.",
960
1072
  type: "http"
961
1073
  },
962
1074
  NOT_EXTENDED: {
963
1075
  httpStatus: 510,
964
1076
  code: "NOT_EXTENDED",
965
- name: "NotExtendedError",
966
- message: "Not extended",
1077
+ name: "Not Extended Error",
1078
+ message: "not extended",
967
1079
  uiMessage: "Additional extensions are required.",
968
1080
  type: "http"
969
1081
  },
970
1082
  NETWORK_AUTHENTICATION_REQUIRED: {
971
1083
  httpStatus: 511,
972
1084
  code: "NETWORK_AUTHENTICATION_REQUIRED",
973
- name: "NetworkAuthenticationRequiredError",
974
- message: "Network authentication required",
1085
+ name: "Network Authentication Required Error",
1086
+ message: "network authentication required",
975
1087
  uiMessage: "Network authentication is required to access this resource.",
976
1088
  type: "http"
977
1089
  }
978
- }
1090
+ };
979
1091
  };
980
1092
 
981
1093
  exports.ErrorX = ErrorX;
982
1094
  exports.HandlingTargets = HandlingTargets;
983
- exports.PRESETS = PRESETS;
984
1095
  //# sourceMappingURL=index.cjs.map
985
1096
  //# sourceMappingURL=index.cjs.map