@bombillazo/error-x 0.4.2 → 0.4.4

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/README.md CHANGED
@@ -63,11 +63,11 @@ throw new ErrorX({
63
63
  })
64
64
 
65
65
  // Using HTTP presets
66
- throw new ErrorX(http.notFound)
66
+ throw new ErrorX(http[404])
67
67
 
68
68
  // Customizing presets
69
69
  throw new ErrorX({
70
- ...http.unauthorized,
70
+ ...http[401],
71
71
  message: 'Session expired',
72
72
  metadata: { userId: 123 }
73
73
  })
@@ -104,7 +104,7 @@ All parameters are optional. ErrorX uses sensible defaults:
104
104
  | sourceUrl | `string \| undefined` | `undefined` | URL related to the error (API endpoint, page URL, resource URL) |
105
105
  | docsUrl | `string \| undefined` | `undefined` or auto-generated | Documentation URL for this specific error |
106
106
  | source | `string \| undefined` | `undefined` or from config | Where the error originated (service name, module, component) |
107
- | timestamp | `Date` | `new Date()` | When the error was created (read-only) |
107
+ | timestamp | `number` | `Date.now()` | Unix epoch timestamp in milliseconds when error was created |
108
108
  | stack | `string` | Auto-generated | Stack trace with preservation and cleaning (inherited from Error) |
109
109
 
110
110
  ### HTTP Error Presets
@@ -115,12 +115,12 @@ ErrorX provides pre-configured error templates via the `http` export:
115
115
  import { ErrorX, http } from '@bombillazo/error-x'
116
116
 
117
117
  // Use preset directly
118
- throw new ErrorX(http.notFound)
118
+ throw new ErrorX(http[404])
119
119
  // Result: 404 error with message "Not found.", code "NOT_FOUND", etc.
120
120
 
121
121
  // Override specific fields
122
122
  throw new ErrorX({
123
- ...http.notFound,
123
+ ...http[404],
124
124
  message: 'User not found',
125
125
  metadata: { userId: 123 }
126
126
  })
@@ -130,7 +130,7 @@ try {
130
130
  // some operation
131
131
  } catch (originalError) {
132
132
  throw new ErrorX({
133
- ...http.internalServerError,
133
+ ...http[500],
134
134
  cause: originalError,
135
135
  metadata: { operation: 'database-query' }
136
136
  })
@@ -139,7 +139,7 @@ try {
139
139
 
140
140
  #### Available Presets
141
141
 
142
- All presets use **camelCase naming** and include:
142
+ All presets are indexed by **HTTP status code** (numeric keys) and include:
143
143
  - `httpStatus`: HTTP status code
144
144
  - `code`: Error code in UPPER_SNAKE_CASE
145
145
  - `name`: Descriptive error name
@@ -149,10 +149,10 @@ All presets use **camelCase naming** and include:
149
149
 
150
150
  **4xx Client Errors:**
151
151
 
152
- `badRequest`, `unauthorized`, `paymentRequired`, `forbidden`, `notFound`, `methodNotAllowed`, `notAcceptable`, `proxyAuthenticationRequired`, `requestTimeout`, `conflict`, `gone`, `lengthRequired`, `preconditionFailed`, `payloadTooLarge`, `uriTooLong`, `unsupportedMediaType`, `rangeNotSatisfiable`, `expectationFailed`, `imATeapot`, `unprocessableEntity`, `locked`, `failedDependency`, `tooEarly`, `upgradeRequired`, `preconditionRequired`, `tooManyRequests`, `requestHeaderFieldsTooLarge`, `unavailableForLegalReasons`
152
+ `400`, `401`, `402`, `403`, `404`, `405`, `406`, `407`, `408`, `409`, `410`, `411`, `412`, `413`, `414`, `415`, `416`, `417`, `418`, `422`, `423`, `424`, `425`, `426`, `428`, `429`, `431`, `451`
153
153
 
154
154
  **5xx Server Errors:**
155
- `internalServerError`, `notImplemented`, `badGateway`, `serviceUnavailable`, `gatewayTimeout`, `httpVersionNotSupported`, `variantAlsoNegotiates`, `insufficientStorage`, `loopDetected`, `notExtended`, `networkAuthenticationRequired`
155
+ `500`, `501`, `502`, `503`, `504`, `505`, `506`, `507`, `508`, `510`, `511`
156
156
 
157
157
  #### Creating Your Own Presets
158
158
 
@@ -344,7 +344,7 @@ async function fetchUser(id: string) {
344
344
  const response = await fetch(`/api/users/${id}`)
345
345
  if (!response.ok) {
346
346
  throw new ErrorX({
347
- ...http[response.status === 404 ? 'notFound' : 'internalServerError'],
347
+ ...http[response.status === 404 ? 404 : 500],
348
348
  metadata: { status: response.status, statusText: response.statusText }
349
349
  })
350
350
  }
package/dist/index.cjs CHANGED
@@ -34,7 +34,7 @@ var ErrorX = class _ErrorX extends Error {
34
34
  uiMessage;
35
35
  /** Additional context and metadata associated with the error */
36
36
  metadata;
37
- /** Timestamp when the error was created */
37
+ /** Unix epoch timestamp (milliseconds) when the error was created */
38
38
  timestamp;
39
39
  /** HTTP status code (100-599) for HTTP-related errors */
40
40
  httpStatus;
@@ -100,7 +100,7 @@ var ErrorX = class _ErrorX extends Error {
100
100
  this.metadata = options.metadata;
101
101
  this.httpStatus = _ErrorX.validateHttpStatus(options.httpStatus);
102
102
  this.type = _ErrorX.validateType(options.type);
103
- this.timestamp = /* @__PURE__ */ new Date();
103
+ this.timestamp = Date.now();
104
104
  this.sourceUrl = options.sourceUrl;
105
105
  this.source = options.source ?? envConfig?.source;
106
106
  let generatedDocsUrl;
@@ -119,8 +119,8 @@ var ErrorX = class _ErrorX extends Error {
119
119
  if (typeof Error.captureStackTrace === "function") {
120
120
  Error.captureStackTrace(this, this.constructor);
121
121
  }
122
- this.stack = _ErrorX.cleanStack(this.stack);
123
122
  }
123
+ this.stack = _ErrorX.cleanStack(this.stack);
124
124
  }
125
125
  /**
126
126
  * Returns the default error name.
@@ -526,7 +526,7 @@ var ErrorX = class _ErrorX extends Error {
526
526
  * })
527
527
  *
528
528
  * console.log(error.toString())
529
- * // Output: "DatabaseError: Database connection failed. [DB_CONN_FAILED] (2024-01-15T10:30:45.123Z) metadata: {...}"
529
+ * // Output: "DatabaseError: Database connection failed. [DB_CONN_FAILED] 2025-01-15T10:30:45.123Z (1736937045123) metadata: {...}"
530
530
  * ```
531
531
  */
532
532
  toString() {
@@ -535,7 +535,7 @@ var ErrorX = class _ErrorX extends Error {
535
535
  if (this.code && this.code !== "ERROR") {
536
536
  parts.push(`[${this.code}]`);
537
537
  }
538
- parts.push(`(${this.timestamp.toISOString()})`);
538
+ parts.push(`${new Date(this.timestamp).toISOString()} (${this.timestamp})`);
539
539
  if (this.metadata && Object.keys(this.metadata).length > 0) {
540
540
  const metadataStr = safeStringify__default.default(this.metadata);
541
541
  parts.push(`metadata: ${metadataStr}`);
@@ -576,7 +576,7 @@ ${this.stack}`;
576
576
  code: this.code,
577
577
  uiMessage: this.uiMessage,
578
578
  metadata: safeMetadata,
579
- timestamp: this.timestamp.toISOString()
579
+ timestamp: this.timestamp
580
580
  };
581
581
  if (this.httpStatus !== void 0) {
582
582
  serialized.httpStatus = this.httpStatus;
@@ -616,7 +616,7 @@ ${this.stack}`;
616
616
  * code: 'DB_CONN_FAILED',
617
617
  * uiMessage: 'Database is temporarily unavailable',
618
618
  * metadata: { host: 'localhost' },
619
- * timestamp: '2024-01-15T10:30:45.123Z'
619
+ * timestamp: 1705315845123
620
620
  * }
621
621
  *
622
622
  * const error = ErrorX.fromJSON(serializedError)
@@ -643,7 +643,7 @@ ${this.stack}`;
643
643
  if (serialized.stack) {
644
644
  error.stack = serialized.stack;
645
645
  }
646
- error.timestamp = new Date(serialized.timestamp);
646
+ error.timestamp = serialized.timestamp;
647
647
  return error;
648
648
  }
649
649
  };
@@ -651,7 +651,7 @@ ${this.stack}`;
651
651
  // src/presets.ts
652
652
  var http = {
653
653
  // 4xx Client Errors
654
- badRequest: {
654
+ 400: {
655
655
  httpStatus: 400,
656
656
  code: "BAD_REQUEST",
657
657
  name: "Bad Request Error",
@@ -659,7 +659,7 @@ var http = {
659
659
  uiMessage: "The request could not be processed. Please check your input and try again.",
660
660
  type: "http"
661
661
  },
662
- unauthorized: {
662
+ 401: {
663
663
  httpStatus: 401,
664
664
  code: "UNAUTHORIZED",
665
665
  name: "Unauthorized Error",
@@ -667,7 +667,7 @@ var http = {
667
667
  uiMessage: "Authentication required. Please log in to continue.",
668
668
  type: "http"
669
669
  },
670
- paymentRequired: {
670
+ 402: {
671
671
  httpStatus: 402,
672
672
  code: "PAYMENT_REQUIRED",
673
673
  name: "Payment Required Error",
@@ -675,7 +675,7 @@ var http = {
675
675
  uiMessage: "Payment is required to access this resource.",
676
676
  type: "http"
677
677
  },
678
- forbidden: {
678
+ 403: {
679
679
  httpStatus: 403,
680
680
  code: "FORBIDDEN",
681
681
  name: "Forbidden Error",
@@ -683,7 +683,7 @@ var http = {
683
683
  uiMessage: "You do not have permission to access this resource.",
684
684
  type: "http"
685
685
  },
686
- notFound: {
686
+ 404: {
687
687
  httpStatus: 404,
688
688
  code: "NOT_FOUND",
689
689
  name: "Not Found Error",
@@ -691,7 +691,7 @@ var http = {
691
691
  uiMessage: "The requested resource could not be found.",
692
692
  type: "http"
693
693
  },
694
- methodNotAllowed: {
694
+ 405: {
695
695
  httpStatus: 405,
696
696
  code: "METHOD_NOT_ALLOWED",
697
697
  name: "Method Not Allowed Error",
@@ -699,7 +699,7 @@ var http = {
699
699
  uiMessage: "This action is not allowed for the requested resource.",
700
700
  type: "http"
701
701
  },
702
- notAcceptable: {
702
+ 406: {
703
703
  httpStatus: 406,
704
704
  code: "NOT_ACCEPTABLE",
705
705
  name: "Not Acceptable Error",
@@ -707,7 +707,7 @@ var http = {
707
707
  uiMessage: "The requested format is not supported.",
708
708
  type: "http"
709
709
  },
710
- proxyAuthenticationRequired: {
710
+ 407: {
711
711
  httpStatus: 407,
712
712
  code: "PROXY_AUTHENTICATION_REQUIRED",
713
713
  name: "Proxy Authentication Required Error",
@@ -715,7 +715,7 @@ var http = {
715
715
  uiMessage: "Proxy authentication is required to access this resource.",
716
716
  type: "http"
717
717
  },
718
- requestTimeout: {
718
+ 408: {
719
719
  httpStatus: 408,
720
720
  code: "REQUEST_TIMEOUT",
721
721
  name: "Request Timeout Error",
@@ -723,7 +723,7 @@ var http = {
723
723
  uiMessage: "The request took too long to complete. Please try again.",
724
724
  type: "http"
725
725
  },
726
- conflict: {
726
+ 409: {
727
727
  httpStatus: 409,
728
728
  code: "CONFLICT",
729
729
  name: "Conflict Error",
@@ -731,7 +731,7 @@ var http = {
731
731
  uiMessage: "The request conflicts with the current state. Please refresh and try again.",
732
732
  type: "http"
733
733
  },
734
- gone: {
734
+ 410: {
735
735
  httpStatus: 410,
736
736
  code: "GONE",
737
737
  name: "Gone Error",
@@ -739,7 +739,7 @@ var http = {
739
739
  uiMessage: "This resource is no longer available.",
740
740
  type: "http"
741
741
  },
742
- lengthRequired: {
742
+ 411: {
743
743
  httpStatus: 411,
744
744
  code: "LENGTH_REQUIRED",
745
745
  name: "Length Required Error",
@@ -747,7 +747,7 @@ var http = {
747
747
  uiMessage: "The request is missing required length information.",
748
748
  type: "http"
749
749
  },
750
- preconditionFailed: {
750
+ 412: {
751
751
  httpStatus: 412,
752
752
  code: "PRECONDITION_FAILED",
753
753
  name: "Precondition Failed Error",
@@ -755,7 +755,7 @@ var http = {
755
755
  uiMessage: "A required condition was not met. Please try again.",
756
756
  type: "http"
757
757
  },
758
- payloadTooLarge: {
758
+ 413: {
759
759
  httpStatus: 413,
760
760
  code: "PAYLOAD_TOO_LARGE",
761
761
  name: "Payload Too Large Error",
@@ -763,7 +763,7 @@ var http = {
763
763
  uiMessage: "The request is too large. Please reduce the size and try again.",
764
764
  type: "http"
765
765
  },
766
- uriTooLong: {
766
+ 414: {
767
767
  httpStatus: 414,
768
768
  code: "URI_TOO_LONG",
769
769
  name: "URI Too Long Error",
@@ -771,7 +771,7 @@ var http = {
771
771
  uiMessage: "The request URL is too long.",
772
772
  type: "http"
773
773
  },
774
- unsupportedMediaType: {
774
+ 415: {
775
775
  httpStatus: 415,
776
776
  code: "UNSUPPORTED_MEDIA_TYPE",
777
777
  name: "Unsupported Media Type Error",
@@ -779,7 +779,7 @@ var http = {
779
779
  uiMessage: "The file type is not supported.",
780
780
  type: "http"
781
781
  },
782
- rangeNotSatisfiable: {
782
+ 416: {
783
783
  httpStatus: 416,
784
784
  code: "RANGE_NOT_SATISFIABLE",
785
785
  name: "Range Not Satisfiable Error",
@@ -787,7 +787,7 @@ var http = {
787
787
  uiMessage: "The requested range cannot be satisfied.",
788
788
  type: "http"
789
789
  },
790
- expectationFailed: {
790
+ 417: {
791
791
  httpStatus: 417,
792
792
  code: "EXPECTATION_FAILED",
793
793
  name: "Expectation Failed Error",
@@ -795,7 +795,7 @@ var http = {
795
795
  uiMessage: "The server cannot meet the requirements of the request.",
796
796
  type: "http"
797
797
  },
798
- imATeapot: {
798
+ 418: {
799
799
  httpStatus: 418,
800
800
  code: "IM_A_TEAPOT",
801
801
  name: "Im A Teapot Error",
@@ -803,7 +803,7 @@ var http = {
803
803
  uiMessage: "I'm a teapot and cannot brew coffee.",
804
804
  type: "http"
805
805
  },
806
- unprocessableEntity: {
806
+ 422: {
807
807
  httpStatus: 422,
808
808
  code: "UNPROCESSABLE_ENTITY",
809
809
  name: "Unprocessable Entity Error",
@@ -811,7 +811,7 @@ var http = {
811
811
  uiMessage: "The request contains invalid data. Please check your input.",
812
812
  type: "http"
813
813
  },
814
- locked: {
814
+ 423: {
815
815
  httpStatus: 423,
816
816
  code: "LOCKED",
817
817
  name: "Locked Error",
@@ -819,7 +819,7 @@ var http = {
819
819
  uiMessage: "This resource is locked and cannot be modified.",
820
820
  type: "http"
821
821
  },
822
- failedDependency: {
822
+ 424: {
823
823
  httpStatus: 424,
824
824
  code: "FAILED_DEPENDENCY",
825
825
  name: "Failed Dependency Error",
@@ -827,7 +827,7 @@ var http = {
827
827
  uiMessage: "The request failed due to a dependency error.",
828
828
  type: "http"
829
829
  },
830
- tooEarly: {
830
+ 425: {
831
831
  httpStatus: 425,
832
832
  code: "TOO_EARLY",
833
833
  name: "Too Early Error",
@@ -835,7 +835,7 @@ var http = {
835
835
  uiMessage: "The request was sent too early. Please try again later.",
836
836
  type: "http"
837
837
  },
838
- upgradeRequired: {
838
+ 426: {
839
839
  httpStatus: 426,
840
840
  code: "UPGRADE_REQUIRED",
841
841
  name: "Upgrade Required Error",
@@ -843,7 +843,7 @@ var http = {
843
843
  uiMessage: "Please upgrade to continue using this service.",
844
844
  type: "http"
845
845
  },
846
- preconditionRequired: {
846
+ 428: {
847
847
  httpStatus: 428,
848
848
  code: "PRECONDITION_REQUIRED",
849
849
  name: "Precondition Required Error",
@@ -851,7 +851,7 @@ var http = {
851
851
  uiMessage: "Required conditions are missing from the request.",
852
852
  type: "http"
853
853
  },
854
- tooManyRequests: {
854
+ 429: {
855
855
  httpStatus: 429,
856
856
  code: "TOO_MANY_REQUESTS",
857
857
  name: "Too Many Requests Error",
@@ -859,7 +859,7 @@ var http = {
859
859
  uiMessage: "You have made too many requests. Please wait and try again.",
860
860
  type: "http"
861
861
  },
862
- requestHeaderFieldsTooLarge: {
862
+ 431: {
863
863
  httpStatus: 431,
864
864
  code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
865
865
  name: "Request Header Fields Too Large Error",
@@ -867,7 +867,7 @@ var http = {
867
867
  uiMessage: "The request headers are too large.",
868
868
  type: "http"
869
869
  },
870
- unavailableForLegalReasons: {
870
+ 451: {
871
871
  httpStatus: 451,
872
872
  code: "UNAVAILABLE_FOR_LEGAL_REASONS",
873
873
  name: "Unavailable For Legal Reasons Error",
@@ -876,7 +876,7 @@ var http = {
876
876
  type: "http"
877
877
  },
878
878
  // 5xx Server Errors
879
- internalServerError: {
879
+ 500: {
880
880
  httpStatus: 500,
881
881
  code: "INTERNAL_SERVER_ERROR",
882
882
  name: "Internal Server Error",
@@ -884,7 +884,7 @@ var http = {
884
884
  uiMessage: "An unexpected error occurred. Please try again later.",
885
885
  type: "http"
886
886
  },
887
- notImplemented: {
887
+ 501: {
888
888
  httpStatus: 501,
889
889
  code: "NOT_IMPLEMENTED",
890
890
  name: "Not Implemented Error",
@@ -892,7 +892,7 @@ var http = {
892
892
  uiMessage: "This feature is not yet available.",
893
893
  type: "http"
894
894
  },
895
- badGateway: {
895
+ 502: {
896
896
  httpStatus: 502,
897
897
  code: "BAD_GATEWAY",
898
898
  name: "Bad Gateway Error",
@@ -900,7 +900,7 @@ var http = {
900
900
  uiMessage: "Unable to connect to the server. Please try again later.",
901
901
  type: "http"
902
902
  },
903
- serviceUnavailable: {
903
+ 503: {
904
904
  httpStatus: 503,
905
905
  code: "SERVICE_UNAVAILABLE",
906
906
  name: "Service Unavailable Error",
@@ -908,7 +908,7 @@ var http = {
908
908
  uiMessage: "The service is temporarily unavailable. Please try again later.",
909
909
  type: "http"
910
910
  },
911
- gatewayTimeout: {
911
+ 504: {
912
912
  httpStatus: 504,
913
913
  code: "GATEWAY_TIMEOUT",
914
914
  name: "Gateway Timeout Error",
@@ -916,7 +916,7 @@ var http = {
916
916
  uiMessage: "The server took too long to respond. Please try again.",
917
917
  type: "http"
918
918
  },
919
- httpVersionNotSupported: {
919
+ 505: {
920
920
  httpStatus: 505,
921
921
  code: "HTTP_VERSION_NOT_SUPPORTED",
922
922
  name: "HTTP Version Not Supported Error",
@@ -924,7 +924,7 @@ var http = {
924
924
  uiMessage: "Your browser version is not supported.",
925
925
  type: "http"
926
926
  },
927
- variantAlsoNegotiates: {
927
+ 506: {
928
928
  httpStatus: 506,
929
929
  code: "VARIANT_ALSO_NEGOTIATES",
930
930
  name: "Variant Also Negotiates Error",
@@ -932,7 +932,7 @@ var http = {
932
932
  uiMessage: "The server has an internal configuration error.",
933
933
  type: "http"
934
934
  },
935
- insufficientStorage: {
935
+ 507: {
936
936
  httpStatus: 507,
937
937
  code: "INSUFFICIENT_STORAGE",
938
938
  name: "Insufficient Storage Error",
@@ -940,7 +940,7 @@ var http = {
940
940
  uiMessage: "The server has insufficient storage to complete the request.",
941
941
  type: "http"
942
942
  },
943
- loopDetected: {
943
+ 508: {
944
944
  httpStatus: 508,
945
945
  code: "LOOP_DETECTED",
946
946
  name: "Loop Detected Error",
@@ -948,7 +948,7 @@ var http = {
948
948
  uiMessage: "The server detected an infinite loop.",
949
949
  type: "http"
950
950
  },
951
- notExtended: {
951
+ 510: {
952
952
  httpStatus: 510,
953
953
  code: "NOT_EXTENDED",
954
954
  name: "Not Extended Error",
@@ -956,7 +956,7 @@ var http = {
956
956
  uiMessage: "Additional extensions are required.",
957
957
  type: "http"
958
958
  },
959
- networkAuthenticationRequired: {
959
+ 511: {
960
960
  httpStatus: 511,
961
961
  code: "NETWORK_AUTHENTICATION_REQUIRED",
962
962
  name: "Network Authentication Required Error",