@bombillazo/error-x 0.4.3 → 0.4.5

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.js CHANGED
@@ -10,9 +10,7 @@ var ERROR_X_OPTION_FIELDS = [
10
10
  "uiMessage",
11
11
  "cause",
12
12
  "metadata",
13
- "httpStatus",
14
13
  "type",
15
- "sourceUrl",
16
14
  "docsUrl",
17
15
  "source"
18
16
  ];
@@ -28,14 +26,10 @@ var ErrorX = class _ErrorX extends Error {
28
26
  uiMessage;
29
27
  /** Additional context and metadata associated with the error */
30
28
  metadata;
31
- /** Timestamp when the error was created */
29
+ /** Unix epoch timestamp (milliseconds) when the error was created */
32
30
  timestamp;
33
- /** HTTP status code (100-599) for HTTP-related errors */
34
- httpStatus;
35
31
  /** Error type for categorization */
36
32
  type;
37
- /** Source URL related to the error (API endpoint, page URL, resource URL) */
38
- sourceUrl;
39
33
  /** Documentation URL for this specific error */
40
34
  docsUrl;
41
35
  /** Where the error originated (service name, module, component) */
@@ -92,10 +86,8 @@ var ErrorX = class _ErrorX extends Error {
92
86
  this.code = options.code != null ? String(options.code) : _ErrorX.generateDefaultCode(options.name);
93
87
  this.uiMessage = options.uiMessage;
94
88
  this.metadata = options.metadata;
95
- this.httpStatus = _ErrorX.validateHttpStatus(options.httpStatus);
96
89
  this.type = _ErrorX.validateType(options.type);
97
- this.timestamp = /* @__PURE__ */ new Date();
98
- this.sourceUrl = options.sourceUrl;
90
+ this.timestamp = Date.now();
99
91
  this.source = options.source ?? envConfig?.source;
100
92
  let generatedDocsUrl;
101
93
  if (envConfig?.docsBaseURL && envConfig?.docsMap && this.code) {
@@ -113,8 +105,8 @@ var ErrorX = class _ErrorX extends Error {
113
105
  if (typeof Error.captureStackTrace === "function") {
114
106
  Error.captureStackTrace(this, this.constructor);
115
107
  }
116
- this.stack = _ErrorX.cleanStack(this.stack);
117
108
  }
109
+ this.stack = _ErrorX.cleanStack(this.stack);
118
110
  }
119
111
  /**
120
112
  * Returns the default error name.
@@ -203,22 +195,6 @@ var ErrorX = class _ErrorX extends Error {
203
195
  static resetConfig() {
204
196
  _ErrorX._config = null;
205
197
  }
206
- /**
207
- * Validates HTTP status code to ensure it's within valid range (100-599)
208
- *
209
- * @param status - Status code to validate
210
- * @returns Valid status code or undefined if invalid/not provided
211
- */
212
- static validateHttpStatus(status) {
213
- if (status === void 0 || status === null) {
214
- return void 0;
215
- }
216
- const statusNum = Number(status);
217
- if (Number.isNaN(statusNum) || statusNum < 100 || statusNum > 599) {
218
- return void 0;
219
- }
220
- return Math.floor(statusNum);
221
- }
222
198
  /**
223
199
  * Validates and normalizes the type field
224
200
  *
@@ -370,9 +346,7 @@ var ErrorX = class _ErrorX extends Error {
370
346
  uiMessage: this.uiMessage,
371
347
  cause: this.cause,
372
348
  metadata: { ...this.metadata ?? {}, ...additionalMetadata },
373
- httpStatus: this.httpStatus,
374
349
  type: this.type,
375
- sourceUrl: this.sourceUrl,
376
350
  docsUrl: this.docsUrl,
377
351
  source: this.source
378
352
  };
@@ -420,9 +394,7 @@ var ErrorX = class _ErrorX extends Error {
420
394
  let uiMessage = "";
421
395
  let cause;
422
396
  let metadata = {};
423
- let httpStatus;
424
397
  let type;
425
- let url;
426
398
  let href;
427
399
  let source;
428
400
  if (error) {
@@ -447,26 +419,9 @@ var ErrorX = class _ErrorX extends Error {
447
419
  if ("code" in error && error.code) code = String(error.code);
448
420
  if ("uiMessage" in error && error.uiMessage) uiMessage = String(error.uiMessage);
449
421
  else if ("userMessage" in error && error.userMessage) uiMessage = String(error.userMessage);
450
- let _httpStatus;
451
- if ("httpStatus" in error) {
452
- _httpStatus = error.httpStatus;
453
- } else if ("status" in error) {
454
- _httpStatus = error.status;
455
- } else if ("statusCode" in error) {
456
- _httpStatus = error.statusCode;
457
- }
458
- if (_httpStatus !== void 0 && _httpStatus !== null) {
459
- const num = typeof _httpStatus === "number" ? _httpStatus : Number(_httpStatus);
460
- httpStatus = _ErrorX.validateHttpStatus(num);
461
- }
462
422
  if ("type" in error && error.type) {
463
423
  type = _ErrorX.validateType(String(error.type));
464
424
  }
465
- if ("sourceUrl" in error && error.sourceUrl) {
466
- url = String(error.sourceUrl);
467
- } else if ("url" in error && error.url) {
468
- url = String(error.url);
469
- }
470
425
  if ("docsUrl" in error && error.docsUrl) {
471
426
  href = String(error.docsUrl);
472
427
  } else if ("href" in error && error.href) {
@@ -492,9 +447,7 @@ var ErrorX = class _ErrorX extends Error {
492
447
  if (uiMessage) options.uiMessage = uiMessage;
493
448
  if (cause) options.cause = cause;
494
449
  if (Object.keys(metadata).length > 0) options.metadata = metadata;
495
- if (httpStatus) options.httpStatus = httpStatus;
496
450
  if (type) options.type = type;
497
- if (url) options.sourceUrl = url;
498
451
  if (href) options.docsUrl = href;
499
452
  if (source) options.source = source;
500
453
  return options;
@@ -520,7 +473,7 @@ var ErrorX = class _ErrorX extends Error {
520
473
  * })
521
474
  *
522
475
  * console.log(error.toString())
523
- * // Output: "DatabaseError: Database connection failed. [DB_CONN_FAILED] (2024-01-15T10:30:45.123Z) metadata: {...}"
476
+ * // Output: "DatabaseError: Database connection failed. [DB_CONN_FAILED] 2025-01-15T10:30:45.123Z (1736937045123) metadata: {...}"
524
477
  * ```
525
478
  */
526
479
  toString() {
@@ -529,7 +482,7 @@ var ErrorX = class _ErrorX extends Error {
529
482
  if (this.code && this.code !== "ERROR") {
530
483
  parts.push(`[${this.code}]`);
531
484
  }
532
- parts.push(`(${this.timestamp.toISOString()})`);
485
+ parts.push(`${new Date(this.timestamp).toISOString()} (${this.timestamp})`);
533
486
  if (this.metadata && Object.keys(this.metadata).length > 0) {
534
487
  const metadataStr = safeStringify(this.metadata);
535
488
  parts.push(`metadata: ${metadataStr}`);
@@ -570,17 +523,11 @@ ${this.stack}`;
570
523
  code: this.code,
571
524
  uiMessage: this.uiMessage,
572
525
  metadata: safeMetadata,
573
- timestamp: this.timestamp.toISOString()
526
+ timestamp: this.timestamp
574
527
  };
575
- if (this.httpStatus !== void 0) {
576
- serialized.httpStatus = this.httpStatus;
577
- }
578
528
  if (this.type !== void 0) {
579
529
  serialized.type = this.type;
580
530
  }
581
- if (this.sourceUrl !== void 0) {
582
- serialized.sourceUrl = this.sourceUrl;
583
- }
584
531
  if (this.docsUrl !== void 0) {
585
532
  serialized.docsUrl = this.docsUrl;
586
533
  }
@@ -610,7 +557,7 @@ ${this.stack}`;
610
557
  * code: 'DB_CONN_FAILED',
611
558
  * uiMessage: 'Database is temporarily unavailable',
612
559
  * metadata: { host: 'localhost' },
613
- * timestamp: '2024-01-15T10:30:45.123Z'
560
+ * timestamp: 1705315845123
614
561
  * }
615
562
  *
616
563
  * const error = ErrorX.fromJSON(serializedError)
@@ -623,9 +570,7 @@ ${this.stack}`;
623
570
  name: serialized.name,
624
571
  code: serialized.code,
625
572
  uiMessage: serialized.uiMessage,
626
- httpStatus: serialized.httpStatus,
627
573
  type: serialized.type,
628
- sourceUrl: serialized.sourceUrl,
629
574
  docsUrl: serialized.docsUrl,
630
575
  source: serialized.source,
631
576
  cause: serialized.cause
@@ -637,7 +582,7 @@ ${this.stack}`;
637
582
  if (serialized.stack) {
638
583
  error.stack = serialized.stack;
639
584
  }
640
- error.timestamp = new Date(serialized.timestamp);
585
+ error.timestamp = serialized.timestamp;
641
586
  return error;
642
587
  }
643
588
  };
@@ -646,317 +591,278 @@ ${this.stack}`;
646
591
  var http = {
647
592
  // 4xx Client Errors
648
593
  400: {
649
- httpStatus: 400,
650
594
  code: "BAD_REQUEST",
651
595
  name: "Bad Request Error",
652
596
  message: "Bad request.",
653
597
  uiMessage: "The request could not be processed. Please check your input and try again.",
654
- type: "http"
598
+ metadata: { status: 400 }
655
599
  },
656
600
  401: {
657
- httpStatus: 401,
658
601
  code: "UNAUTHORIZED",
659
602
  name: "Unauthorized Error",
660
603
  message: "Unauthorized.",
661
604
  uiMessage: "Authentication required. Please log in to continue.",
662
- type: "http"
605
+ metadata: { status: 401 }
663
606
  },
664
607
  402: {
665
- httpStatus: 402,
666
608
  code: "PAYMENT_REQUIRED",
667
609
  name: "Payment Required Error",
668
610
  message: "Payment required.",
669
611
  uiMessage: "Payment is required to access this resource.",
670
- type: "http"
612
+ metadata: { status: 402 }
671
613
  },
672
614
  403: {
673
- httpStatus: 403,
674
615
  code: "FORBIDDEN",
675
616
  name: "Forbidden Error",
676
617
  message: "Forbidden.",
677
618
  uiMessage: "You do not have permission to access this resource.",
678
- type: "http"
619
+ metadata: { status: 403 }
679
620
  },
680
621
  404: {
681
- httpStatus: 404,
682
622
  code: "NOT_FOUND",
683
623
  name: "Not Found Error",
684
624
  message: "Not found.",
685
625
  uiMessage: "The requested resource could not be found.",
686
- type: "http"
626
+ metadata: { status: 404 }
687
627
  },
688
628
  405: {
689
- httpStatus: 405,
690
629
  code: "METHOD_NOT_ALLOWED",
691
630
  name: "Method Not Allowed Error",
692
631
  message: "Method not allowed.",
693
632
  uiMessage: "This action is not allowed for the requested resource.",
694
- type: "http"
633
+ metadata: { status: 405 }
695
634
  },
696
635
  406: {
697
- httpStatus: 406,
698
636
  code: "NOT_ACCEPTABLE",
699
637
  name: "Not Acceptable Error",
700
638
  message: "Not acceptable.",
701
639
  uiMessage: "The requested format is not supported.",
702
- type: "http"
640
+ metadata: { status: 406 }
703
641
  },
704
642
  407: {
705
- httpStatus: 407,
706
643
  code: "PROXY_AUTHENTICATION_REQUIRED",
707
644
  name: "Proxy Authentication Required Error",
708
645
  message: "Proxy authentication required.",
709
646
  uiMessage: "Proxy authentication is required to access this resource.",
710
- type: "http"
647
+ metadata: { status: 407 }
711
648
  },
712
649
  408: {
713
- httpStatus: 408,
714
650
  code: "REQUEST_TIMEOUT",
715
651
  name: "Request Timeout Error",
716
652
  message: "Request timeout.",
717
653
  uiMessage: "The request took too long to complete. Please try again.",
718
- type: "http"
654
+ metadata: { status: 408 }
719
655
  },
720
656
  409: {
721
- httpStatus: 409,
722
657
  code: "CONFLICT",
723
658
  name: "Conflict Error",
724
659
  message: "Conflict.",
725
660
  uiMessage: "The request conflicts with the current state. Please refresh and try again.",
726
- type: "http"
661
+ metadata: { status: 409 }
727
662
  },
728
663
  410: {
729
- httpStatus: 410,
730
664
  code: "GONE",
731
665
  name: "Gone Error",
732
666
  message: "Gone.",
733
667
  uiMessage: "This resource is no longer available.",
734
- type: "http"
668
+ metadata: { status: 410 }
735
669
  },
736
670
  411: {
737
- httpStatus: 411,
738
671
  code: "LENGTH_REQUIRED",
739
672
  name: "Length Required Error",
740
673
  message: "Length required.",
741
674
  uiMessage: "The request is missing required length information.",
742
- type: "http"
675
+ metadata: { status: 411 }
743
676
  },
744
677
  412: {
745
- httpStatus: 412,
746
678
  code: "PRECONDITION_FAILED",
747
679
  name: "Precondition Failed Error",
748
680
  message: "Precondition failed.",
749
681
  uiMessage: "A required condition was not met. Please try again.",
750
- type: "http"
682
+ metadata: { status: 412 }
751
683
  },
752
684
  413: {
753
- httpStatus: 413,
754
685
  code: "PAYLOAD_TOO_LARGE",
755
686
  name: "Payload Too Large Error",
756
687
  message: "Payload too large.",
757
688
  uiMessage: "The request is too large. Please reduce the size and try again.",
758
- type: "http"
689
+ metadata: { status: 413 }
759
690
  },
760
691
  414: {
761
- httpStatus: 414,
762
692
  code: "URI_TOO_LONG",
763
693
  name: "URI Too Long Error",
764
694
  message: "URI too long.",
765
695
  uiMessage: "The request URL is too long.",
766
- type: "http"
696
+ metadata: { status: 414 }
767
697
  },
768
698
  415: {
769
- httpStatus: 415,
770
699
  code: "UNSUPPORTED_MEDIA_TYPE",
771
700
  name: "Unsupported Media Type Error",
772
701
  message: "Unsupported media type.",
773
702
  uiMessage: "The file type is not supported.",
774
- type: "http"
703
+ metadata: { status: 415 }
775
704
  },
776
705
  416: {
777
- httpStatus: 416,
778
706
  code: "RANGE_NOT_SATISFIABLE",
779
707
  name: "Range Not Satisfiable Error",
780
708
  message: "Range not satisfiable.",
781
709
  uiMessage: "The requested range cannot be satisfied.",
782
- type: "http"
710
+ metadata: { status: 416 }
783
711
  },
784
712
  417: {
785
- httpStatus: 417,
786
713
  code: "EXPECTATION_FAILED",
787
714
  name: "Expectation Failed Error",
788
715
  message: "Expectation failed.",
789
716
  uiMessage: "The server cannot meet the requirements of the request.",
790
- type: "http"
717
+ metadata: { status: 417 }
791
718
  },
792
719
  418: {
793
- httpStatus: 418,
794
720
  code: "IM_A_TEAPOT",
795
721
  name: "Im A Teapot Error",
796
722
  message: "I'm a teapot.",
797
723
  uiMessage: "I'm a teapot and cannot brew coffee.",
798
- type: "http"
724
+ metadata: { status: 418 }
799
725
  },
800
726
  422: {
801
- httpStatus: 422,
802
727
  code: "UNPROCESSABLE_ENTITY",
803
728
  name: "Unprocessable Entity Error",
804
729
  message: "Unprocessable entity.",
805
730
  uiMessage: "The request contains invalid data. Please check your input.",
806
- type: "http"
731
+ metadata: { status: 422 }
807
732
  },
808
733
  423: {
809
- httpStatus: 423,
810
734
  code: "LOCKED",
811
735
  name: "Locked Error",
812
736
  message: "Locked.",
813
737
  uiMessage: "This resource is locked and cannot be modified.",
814
- type: "http"
738
+ metadata: { status: 423 }
815
739
  },
816
740
  424: {
817
- httpStatus: 424,
818
741
  code: "FAILED_DEPENDENCY",
819
742
  name: "Failed Dependency Error",
820
743
  message: "Failed dependency.",
821
744
  uiMessage: "The request failed due to a dependency error.",
822
- type: "http"
745
+ metadata: { status: 424 }
823
746
  },
824
747
  425: {
825
- httpStatus: 425,
826
748
  code: "TOO_EARLY",
827
749
  name: "Too Early Error",
828
750
  message: "Too early.",
829
751
  uiMessage: "The request was sent too early. Please try again later.",
830
- type: "http"
752
+ metadata: { status: 425 }
831
753
  },
832
754
  426: {
833
- httpStatus: 426,
834
755
  code: "UPGRADE_REQUIRED",
835
756
  name: "Upgrade Required Error",
836
757
  message: "Upgrade required.",
837
758
  uiMessage: "Please upgrade to continue using this service.",
838
- type: "http"
759
+ metadata: { status: 426 }
839
760
  },
840
761
  428: {
841
- httpStatus: 428,
842
762
  code: "PRECONDITION_REQUIRED",
843
763
  name: "Precondition Required Error",
844
764
  message: "Precondition required.",
845
765
  uiMessage: "Required conditions are missing from the request.",
846
- type: "http"
766
+ metadata: { status: 428 }
847
767
  },
848
768
  429: {
849
- httpStatus: 429,
850
769
  code: "TOO_MANY_REQUESTS",
851
770
  name: "Too Many Requests Error",
852
771
  message: "Too many requests.",
853
772
  uiMessage: "You have made too many requests. Please wait and try again.",
854
- type: "http"
773
+ metadata: { status: 429 }
855
774
  },
856
775
  431: {
857
- httpStatus: 431,
858
776
  code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
859
777
  name: "Request Header Fields Too Large Error",
860
778
  message: "Request header fields too large.",
861
779
  uiMessage: "The request headers are too large.",
862
- type: "http"
780
+ metadata: { status: 431 }
863
781
  },
864
782
  451: {
865
- httpStatus: 451,
866
783
  code: "UNAVAILABLE_FOR_LEGAL_REASONS",
867
784
  name: "Unavailable For Legal Reasons Error",
868
785
  message: "Unavailable for legal reasons.",
869
786
  uiMessage: "This content is unavailable for legal reasons.",
870
- type: "http"
787
+ metadata: { status: 451 }
871
788
  },
872
789
  // 5xx Server Errors
873
790
  500: {
874
- httpStatus: 500,
875
791
  code: "INTERNAL_SERVER_ERROR",
876
792
  name: "Internal Server Error",
877
793
  message: "Internal server error.",
878
794
  uiMessage: "An unexpected error occurred. Please try again later.",
879
- type: "http"
795
+ metadata: { status: 500 }
880
796
  },
881
797
  501: {
882
- httpStatus: 501,
883
798
  code: "NOT_IMPLEMENTED",
884
799
  name: "Not Implemented Error",
885
800
  message: "Not implemented.",
886
801
  uiMessage: "This feature is not yet available.",
887
- type: "http"
802
+ metadata: { status: 501 }
888
803
  },
889
804
  502: {
890
- httpStatus: 502,
891
805
  code: "BAD_GATEWAY",
892
806
  name: "Bad Gateway Error",
893
807
  message: "Bad gateway.",
894
808
  uiMessage: "Unable to connect to the server. Please try again later.",
895
- type: "http"
809
+ metadata: { status: 502 }
896
810
  },
897
811
  503: {
898
- httpStatus: 503,
899
812
  code: "SERVICE_UNAVAILABLE",
900
813
  name: "Service Unavailable Error",
901
814
  message: "Service unavailable.",
902
815
  uiMessage: "The service is temporarily unavailable. Please try again later.",
903
- type: "http"
816
+ metadata: { status: 503 }
904
817
  },
905
818
  504: {
906
- httpStatus: 504,
907
819
  code: "GATEWAY_TIMEOUT",
908
820
  name: "Gateway Timeout Error",
909
821
  message: "Gateway timeout.",
910
822
  uiMessage: "The server took too long to respond. Please try again.",
911
- type: "http"
823
+ metadata: { status: 504 }
912
824
  },
913
825
  505: {
914
- httpStatus: 505,
915
826
  code: "HTTP_VERSION_NOT_SUPPORTED",
916
827
  name: "HTTP Version Not Supported Error",
917
828
  message: "HTTP version not supported.",
918
829
  uiMessage: "Your browser version is not supported.",
919
- type: "http"
830
+ metadata: { status: 505 }
920
831
  },
921
832
  506: {
922
- httpStatus: 506,
923
833
  code: "VARIANT_ALSO_NEGOTIATES",
924
834
  name: "Variant Also Negotiates Error",
925
835
  message: "Variant also negotiates.",
926
836
  uiMessage: "The server has an internal configuration error.",
927
- type: "http"
837
+ metadata: { status: 506 }
928
838
  },
929
839
  507: {
930
- httpStatus: 507,
931
840
  code: "INSUFFICIENT_STORAGE",
932
841
  name: "Insufficient Storage Error",
933
842
  message: "Insufficient storage.",
934
843
  uiMessage: "The server has insufficient storage to complete the request.",
935
- type: "http"
844
+ metadata: { status: 507 }
936
845
  },
937
846
  508: {
938
- httpStatus: 508,
939
847
  code: "LOOP_DETECTED",
940
848
  name: "Loop Detected Error",
941
849
  message: "Loop detected.",
942
850
  uiMessage: "The server detected an infinite loop.",
943
- type: "http"
851
+ metadata: { status: 508 }
944
852
  },
945
853
  510: {
946
- httpStatus: 510,
947
854
  code: "NOT_EXTENDED",
948
855
  name: "Not Extended Error",
949
856
  message: "Not extended.",
950
857
  uiMessage: "Additional extensions are required.",
951
- type: "http"
858
+ metadata: { status: 510 }
952
859
  },
953
860
  511: {
954
- httpStatus: 511,
955
861
  code: "NETWORK_AUTHENTICATION_REQUIRED",
956
862
  name: "Network Authentication Required Error",
957
863
  message: "Network authentication required.",
958
864
  uiMessage: "Network authentication is required to access this resource.",
959
- type: "http"
865
+ metadata: { status: 511 }
960
866
  }
961
867
  };
962
868