@bombillazo/error-x 0.4.4 → 0.4.6

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
  ];
@@ -30,12 +28,8 @@ var ErrorX = class _ErrorX extends Error {
30
28
  metadata;
31
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
90
  this.timestamp = Date.now();
98
- this.sourceUrl = options.sourceUrl;
99
91
  this.source = options.source ?? envConfig?.source;
100
92
  let generatedDocsUrl;
101
93
  if (envConfig?.docsBaseURL && envConfig?.docsMap && this.code) {
@@ -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
  };
@@ -407,7 +381,8 @@ var ErrorX = class _ErrorX extends Error {
407
381
  /**
408
382
  * Converts unknown input into ErrorXOptions with intelligent property extraction.
409
383
  * Handles strings, regular Error objects, API response objects, and unknown values.
410
- * This is a private helper method used by both the constructor and toErrorX.
384
+ * Extracts metadata directly from objects if present, without wrapping.
385
+ * This is a private helper method used by ErrorX.from().
411
386
  *
412
387
  * @param error - Value to convert to ErrorXOptions
413
388
  * @returns ErrorXOptions object with extracted properties
@@ -420,15 +395,12 @@ var ErrorX = class _ErrorX extends Error {
420
395
  let uiMessage = "";
421
396
  let cause;
422
397
  let metadata = {};
423
- let httpStatus;
424
398
  let type;
425
- let url;
426
399
  let href;
427
400
  let source;
428
401
  if (error) {
429
402
  if (typeof error === "string") {
430
403
  message = error;
431
- metadata = { originalError: error };
432
404
  } else if (error instanceof Error) {
433
405
  name = error.name;
434
406
  message = error.message;
@@ -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) {
@@ -481,7 +436,9 @@ var ErrorX = class _ErrorX extends Error {
481
436
  } else if ("component" in error && error.component) {
482
437
  source = String(error.component);
483
438
  }
484
- metadata = { originalError: error };
439
+ if ("metadata" in error && typeof error.metadata === "object" && error.metadata !== null) {
440
+ metadata = error.metadata;
441
+ }
485
442
  }
486
443
  }
487
444
  const options = {
@@ -492,9 +449,7 @@ var ErrorX = class _ErrorX extends Error {
492
449
  if (uiMessage) options.uiMessage = uiMessage;
493
450
  if (cause) options.cause = cause;
494
451
  if (Object.keys(metadata).length > 0) options.metadata = metadata;
495
- if (httpStatus) options.httpStatus = httpStatus;
496
452
  if (type) options.type = type;
497
- if (url) options.sourceUrl = url;
498
453
  if (href) options.docsUrl = href;
499
454
  if (source) options.source = source;
500
455
  return options;
@@ -572,15 +527,9 @@ ${this.stack}`;
572
527
  metadata: safeMetadata,
573
528
  timestamp: this.timestamp
574
529
  };
575
- if (this.httpStatus !== void 0) {
576
- serialized.httpStatus = this.httpStatus;
577
- }
578
530
  if (this.type !== void 0) {
579
531
  serialized.type = this.type;
580
532
  }
581
- if (this.sourceUrl !== void 0) {
582
- serialized.sourceUrl = this.sourceUrl;
583
- }
584
533
  if (this.docsUrl !== void 0) {
585
534
  serialized.docsUrl = this.docsUrl;
586
535
  }
@@ -623,9 +572,7 @@ ${this.stack}`;
623
572
  name: serialized.name,
624
573
  code: serialized.code,
625
574
  uiMessage: serialized.uiMessage,
626
- httpStatus: serialized.httpStatus,
627
575
  type: serialized.type,
628
- sourceUrl: serialized.sourceUrl,
629
576
  docsUrl: serialized.docsUrl,
630
577
  source: serialized.source,
631
578
  cause: serialized.cause
@@ -646,317 +593,278 @@ ${this.stack}`;
646
593
  var http = {
647
594
  // 4xx Client Errors
648
595
  400: {
649
- httpStatus: 400,
650
596
  code: "BAD_REQUEST",
651
597
  name: "Bad Request Error",
652
598
  message: "Bad request.",
653
599
  uiMessage: "The request could not be processed. Please check your input and try again.",
654
- type: "http"
600
+ metadata: { status: 400 }
655
601
  },
656
602
  401: {
657
- httpStatus: 401,
658
603
  code: "UNAUTHORIZED",
659
604
  name: "Unauthorized Error",
660
605
  message: "Unauthorized.",
661
606
  uiMessage: "Authentication required. Please log in to continue.",
662
- type: "http"
607
+ metadata: { status: 401 }
663
608
  },
664
609
  402: {
665
- httpStatus: 402,
666
610
  code: "PAYMENT_REQUIRED",
667
611
  name: "Payment Required Error",
668
612
  message: "Payment required.",
669
613
  uiMessage: "Payment is required to access this resource.",
670
- type: "http"
614
+ metadata: { status: 402 }
671
615
  },
672
616
  403: {
673
- httpStatus: 403,
674
617
  code: "FORBIDDEN",
675
618
  name: "Forbidden Error",
676
619
  message: "Forbidden.",
677
620
  uiMessage: "You do not have permission to access this resource.",
678
- type: "http"
621
+ metadata: { status: 403 }
679
622
  },
680
623
  404: {
681
- httpStatus: 404,
682
624
  code: "NOT_FOUND",
683
625
  name: "Not Found Error",
684
626
  message: "Not found.",
685
627
  uiMessage: "The requested resource could not be found.",
686
- type: "http"
628
+ metadata: { status: 404 }
687
629
  },
688
630
  405: {
689
- httpStatus: 405,
690
631
  code: "METHOD_NOT_ALLOWED",
691
632
  name: "Method Not Allowed Error",
692
633
  message: "Method not allowed.",
693
634
  uiMessage: "This action is not allowed for the requested resource.",
694
- type: "http"
635
+ metadata: { status: 405 }
695
636
  },
696
637
  406: {
697
- httpStatus: 406,
698
638
  code: "NOT_ACCEPTABLE",
699
639
  name: "Not Acceptable Error",
700
640
  message: "Not acceptable.",
701
641
  uiMessage: "The requested format is not supported.",
702
- type: "http"
642
+ metadata: { status: 406 }
703
643
  },
704
644
  407: {
705
- httpStatus: 407,
706
645
  code: "PROXY_AUTHENTICATION_REQUIRED",
707
646
  name: "Proxy Authentication Required Error",
708
647
  message: "Proxy authentication required.",
709
648
  uiMessage: "Proxy authentication is required to access this resource.",
710
- type: "http"
649
+ metadata: { status: 407 }
711
650
  },
712
651
  408: {
713
- httpStatus: 408,
714
652
  code: "REQUEST_TIMEOUT",
715
653
  name: "Request Timeout Error",
716
654
  message: "Request timeout.",
717
655
  uiMessage: "The request took too long to complete. Please try again.",
718
- type: "http"
656
+ metadata: { status: 408 }
719
657
  },
720
658
  409: {
721
- httpStatus: 409,
722
659
  code: "CONFLICT",
723
660
  name: "Conflict Error",
724
661
  message: "Conflict.",
725
662
  uiMessage: "The request conflicts with the current state. Please refresh and try again.",
726
- type: "http"
663
+ metadata: { status: 409 }
727
664
  },
728
665
  410: {
729
- httpStatus: 410,
730
666
  code: "GONE",
731
667
  name: "Gone Error",
732
668
  message: "Gone.",
733
669
  uiMessage: "This resource is no longer available.",
734
- type: "http"
670
+ metadata: { status: 410 }
735
671
  },
736
672
  411: {
737
- httpStatus: 411,
738
673
  code: "LENGTH_REQUIRED",
739
674
  name: "Length Required Error",
740
675
  message: "Length required.",
741
676
  uiMessage: "The request is missing required length information.",
742
- type: "http"
677
+ metadata: { status: 411 }
743
678
  },
744
679
  412: {
745
- httpStatus: 412,
746
680
  code: "PRECONDITION_FAILED",
747
681
  name: "Precondition Failed Error",
748
682
  message: "Precondition failed.",
749
683
  uiMessage: "A required condition was not met. Please try again.",
750
- type: "http"
684
+ metadata: { status: 412 }
751
685
  },
752
686
  413: {
753
- httpStatus: 413,
754
687
  code: "PAYLOAD_TOO_LARGE",
755
688
  name: "Payload Too Large Error",
756
689
  message: "Payload too large.",
757
690
  uiMessage: "The request is too large. Please reduce the size and try again.",
758
- type: "http"
691
+ metadata: { status: 413 }
759
692
  },
760
693
  414: {
761
- httpStatus: 414,
762
694
  code: "URI_TOO_LONG",
763
695
  name: "URI Too Long Error",
764
696
  message: "URI too long.",
765
697
  uiMessage: "The request URL is too long.",
766
- type: "http"
698
+ metadata: { status: 414 }
767
699
  },
768
700
  415: {
769
- httpStatus: 415,
770
701
  code: "UNSUPPORTED_MEDIA_TYPE",
771
702
  name: "Unsupported Media Type Error",
772
703
  message: "Unsupported media type.",
773
704
  uiMessage: "The file type is not supported.",
774
- type: "http"
705
+ metadata: { status: 415 }
775
706
  },
776
707
  416: {
777
- httpStatus: 416,
778
708
  code: "RANGE_NOT_SATISFIABLE",
779
709
  name: "Range Not Satisfiable Error",
780
710
  message: "Range not satisfiable.",
781
711
  uiMessage: "The requested range cannot be satisfied.",
782
- type: "http"
712
+ metadata: { status: 416 }
783
713
  },
784
714
  417: {
785
- httpStatus: 417,
786
715
  code: "EXPECTATION_FAILED",
787
716
  name: "Expectation Failed Error",
788
717
  message: "Expectation failed.",
789
718
  uiMessage: "The server cannot meet the requirements of the request.",
790
- type: "http"
719
+ metadata: { status: 417 }
791
720
  },
792
721
  418: {
793
- httpStatus: 418,
794
722
  code: "IM_A_TEAPOT",
795
723
  name: "Im A Teapot Error",
796
724
  message: "I'm a teapot.",
797
725
  uiMessage: "I'm a teapot and cannot brew coffee.",
798
- type: "http"
726
+ metadata: { status: 418 }
799
727
  },
800
728
  422: {
801
- httpStatus: 422,
802
729
  code: "UNPROCESSABLE_ENTITY",
803
730
  name: "Unprocessable Entity Error",
804
731
  message: "Unprocessable entity.",
805
732
  uiMessage: "The request contains invalid data. Please check your input.",
806
- type: "http"
733
+ metadata: { status: 422 }
807
734
  },
808
735
  423: {
809
- httpStatus: 423,
810
736
  code: "LOCKED",
811
737
  name: "Locked Error",
812
738
  message: "Locked.",
813
739
  uiMessage: "This resource is locked and cannot be modified.",
814
- type: "http"
740
+ metadata: { status: 423 }
815
741
  },
816
742
  424: {
817
- httpStatus: 424,
818
743
  code: "FAILED_DEPENDENCY",
819
744
  name: "Failed Dependency Error",
820
745
  message: "Failed dependency.",
821
746
  uiMessage: "The request failed due to a dependency error.",
822
- type: "http"
747
+ metadata: { status: 424 }
823
748
  },
824
749
  425: {
825
- httpStatus: 425,
826
750
  code: "TOO_EARLY",
827
751
  name: "Too Early Error",
828
752
  message: "Too early.",
829
753
  uiMessage: "The request was sent too early. Please try again later.",
830
- type: "http"
754
+ metadata: { status: 425 }
831
755
  },
832
756
  426: {
833
- httpStatus: 426,
834
757
  code: "UPGRADE_REQUIRED",
835
758
  name: "Upgrade Required Error",
836
759
  message: "Upgrade required.",
837
760
  uiMessage: "Please upgrade to continue using this service.",
838
- type: "http"
761
+ metadata: { status: 426 }
839
762
  },
840
763
  428: {
841
- httpStatus: 428,
842
764
  code: "PRECONDITION_REQUIRED",
843
765
  name: "Precondition Required Error",
844
766
  message: "Precondition required.",
845
767
  uiMessage: "Required conditions are missing from the request.",
846
- type: "http"
768
+ metadata: { status: 428 }
847
769
  },
848
770
  429: {
849
- httpStatus: 429,
850
771
  code: "TOO_MANY_REQUESTS",
851
772
  name: "Too Many Requests Error",
852
773
  message: "Too many requests.",
853
774
  uiMessage: "You have made too many requests. Please wait and try again.",
854
- type: "http"
775
+ metadata: { status: 429 }
855
776
  },
856
777
  431: {
857
- httpStatus: 431,
858
778
  code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
859
779
  name: "Request Header Fields Too Large Error",
860
780
  message: "Request header fields too large.",
861
781
  uiMessage: "The request headers are too large.",
862
- type: "http"
782
+ metadata: { status: 431 }
863
783
  },
864
784
  451: {
865
- httpStatus: 451,
866
785
  code: "UNAVAILABLE_FOR_LEGAL_REASONS",
867
786
  name: "Unavailable For Legal Reasons Error",
868
787
  message: "Unavailable for legal reasons.",
869
788
  uiMessage: "This content is unavailable for legal reasons.",
870
- type: "http"
789
+ metadata: { status: 451 }
871
790
  },
872
791
  // 5xx Server Errors
873
792
  500: {
874
- httpStatus: 500,
875
793
  code: "INTERNAL_SERVER_ERROR",
876
794
  name: "Internal Server Error",
877
795
  message: "Internal server error.",
878
796
  uiMessage: "An unexpected error occurred. Please try again later.",
879
- type: "http"
797
+ metadata: { status: 500 }
880
798
  },
881
799
  501: {
882
- httpStatus: 501,
883
800
  code: "NOT_IMPLEMENTED",
884
801
  name: "Not Implemented Error",
885
802
  message: "Not implemented.",
886
803
  uiMessage: "This feature is not yet available.",
887
- type: "http"
804
+ metadata: { status: 501 }
888
805
  },
889
806
  502: {
890
- httpStatus: 502,
891
807
  code: "BAD_GATEWAY",
892
808
  name: "Bad Gateway Error",
893
809
  message: "Bad gateway.",
894
810
  uiMessage: "Unable to connect to the server. Please try again later.",
895
- type: "http"
811
+ metadata: { status: 502 }
896
812
  },
897
813
  503: {
898
- httpStatus: 503,
899
814
  code: "SERVICE_UNAVAILABLE",
900
815
  name: "Service Unavailable Error",
901
816
  message: "Service unavailable.",
902
817
  uiMessage: "The service is temporarily unavailable. Please try again later.",
903
- type: "http"
818
+ metadata: { status: 503 }
904
819
  },
905
820
  504: {
906
- httpStatus: 504,
907
821
  code: "GATEWAY_TIMEOUT",
908
822
  name: "Gateway Timeout Error",
909
823
  message: "Gateway timeout.",
910
824
  uiMessage: "The server took too long to respond. Please try again.",
911
- type: "http"
825
+ metadata: { status: 504 }
912
826
  },
913
827
  505: {
914
- httpStatus: 505,
915
828
  code: "HTTP_VERSION_NOT_SUPPORTED",
916
829
  name: "HTTP Version Not Supported Error",
917
830
  message: "HTTP version not supported.",
918
831
  uiMessage: "Your browser version is not supported.",
919
- type: "http"
832
+ metadata: { status: 505 }
920
833
  },
921
834
  506: {
922
- httpStatus: 506,
923
835
  code: "VARIANT_ALSO_NEGOTIATES",
924
836
  name: "Variant Also Negotiates Error",
925
837
  message: "Variant also negotiates.",
926
838
  uiMessage: "The server has an internal configuration error.",
927
- type: "http"
839
+ metadata: { status: 506 }
928
840
  },
929
841
  507: {
930
- httpStatus: 507,
931
842
  code: "INSUFFICIENT_STORAGE",
932
843
  name: "Insufficient Storage Error",
933
844
  message: "Insufficient storage.",
934
845
  uiMessage: "The server has insufficient storage to complete the request.",
935
- type: "http"
846
+ metadata: { status: 507 }
936
847
  },
937
848
  508: {
938
- httpStatus: 508,
939
849
  code: "LOOP_DETECTED",
940
850
  name: "Loop Detected Error",
941
851
  message: "Loop detected.",
942
852
  uiMessage: "The server detected an infinite loop.",
943
- type: "http"
853
+ metadata: { status: 508 }
944
854
  },
945
855
  510: {
946
- httpStatus: 510,
947
856
  code: "NOT_EXTENDED",
948
857
  name: "Not Extended Error",
949
858
  message: "Not extended.",
950
859
  uiMessage: "Additional extensions are required.",
951
- type: "http"
860
+ metadata: { status: 510 }
952
861
  },
953
862
  511: {
954
- httpStatus: 511,
955
863
  code: "NETWORK_AUTHENTICATION_REQUIRED",
956
864
  name: "Network Authentication Required Error",
957
865
  message: "Network authentication required.",
958
866
  uiMessage: "Network authentication is required to access this resource.",
959
- type: "http"
867
+ metadata: { status: 511 }
960
868
  }
961
869
  };
962
870