@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.cjs CHANGED
@@ -16,9 +16,7 @@ var ERROR_X_OPTION_FIELDS = [
16
16
  "uiMessage",
17
17
  "cause",
18
18
  "metadata",
19
- "httpStatus",
20
19
  "type",
21
- "sourceUrl",
22
20
  "docsUrl",
23
21
  "source"
24
22
  ];
@@ -36,12 +34,8 @@ var ErrorX = class _ErrorX extends Error {
36
34
  metadata;
37
35
  /** Unix epoch timestamp (milliseconds) when the error was created */
38
36
  timestamp;
39
- /** HTTP status code (100-599) for HTTP-related errors */
40
- httpStatus;
41
37
  /** Error type for categorization */
42
38
  type;
43
- /** Source URL related to the error (API endpoint, page URL, resource URL) */
44
- sourceUrl;
45
39
  /** Documentation URL for this specific error */
46
40
  docsUrl;
47
41
  /** Where the error originated (service name, module, component) */
@@ -98,10 +92,8 @@ var ErrorX = class _ErrorX extends Error {
98
92
  this.code = options.code != null ? String(options.code) : _ErrorX.generateDefaultCode(options.name);
99
93
  this.uiMessage = options.uiMessage;
100
94
  this.metadata = options.metadata;
101
- this.httpStatus = _ErrorX.validateHttpStatus(options.httpStatus);
102
95
  this.type = _ErrorX.validateType(options.type);
103
96
  this.timestamp = Date.now();
104
- this.sourceUrl = options.sourceUrl;
105
97
  this.source = options.source ?? envConfig?.source;
106
98
  let generatedDocsUrl;
107
99
  if (envConfig?.docsBaseURL && envConfig?.docsMap && this.code) {
@@ -209,22 +201,6 @@ var ErrorX = class _ErrorX extends Error {
209
201
  static resetConfig() {
210
202
  _ErrorX._config = null;
211
203
  }
212
- /**
213
- * Validates HTTP status code to ensure it's within valid range (100-599)
214
- *
215
- * @param status - Status code to validate
216
- * @returns Valid status code or undefined if invalid/not provided
217
- */
218
- static validateHttpStatus(status) {
219
- if (status === void 0 || status === null) {
220
- return void 0;
221
- }
222
- const statusNum = Number(status);
223
- if (Number.isNaN(statusNum) || statusNum < 100 || statusNum > 599) {
224
- return void 0;
225
- }
226
- return Math.floor(statusNum);
227
- }
228
204
  /**
229
205
  * Validates and normalizes the type field
230
206
  *
@@ -376,9 +352,7 @@ var ErrorX = class _ErrorX extends Error {
376
352
  uiMessage: this.uiMessage,
377
353
  cause: this.cause,
378
354
  metadata: { ...this.metadata ?? {}, ...additionalMetadata },
379
- httpStatus: this.httpStatus,
380
355
  type: this.type,
381
- sourceUrl: this.sourceUrl,
382
356
  docsUrl: this.docsUrl,
383
357
  source: this.source
384
358
  };
@@ -413,7 +387,8 @@ var ErrorX = class _ErrorX extends Error {
413
387
  /**
414
388
  * Converts unknown input into ErrorXOptions with intelligent property extraction.
415
389
  * Handles strings, regular Error objects, API response objects, and unknown values.
416
- * This is a private helper method used by both the constructor and toErrorX.
390
+ * Extracts metadata directly from objects if present, without wrapping.
391
+ * This is a private helper method used by ErrorX.from().
417
392
  *
418
393
  * @param error - Value to convert to ErrorXOptions
419
394
  * @returns ErrorXOptions object with extracted properties
@@ -426,15 +401,12 @@ var ErrorX = class _ErrorX extends Error {
426
401
  let uiMessage = "";
427
402
  let cause;
428
403
  let metadata = {};
429
- let httpStatus;
430
404
  let type;
431
- let url;
432
405
  let href;
433
406
  let source;
434
407
  if (error) {
435
408
  if (typeof error === "string") {
436
409
  message = error;
437
- metadata = { originalError: error };
438
410
  } else if (error instanceof Error) {
439
411
  name = error.name;
440
412
  message = error.message;
@@ -453,26 +425,9 @@ var ErrorX = class _ErrorX extends Error {
453
425
  if ("code" in error && error.code) code = String(error.code);
454
426
  if ("uiMessage" in error && error.uiMessage) uiMessage = String(error.uiMessage);
455
427
  else if ("userMessage" in error && error.userMessage) uiMessage = String(error.userMessage);
456
- let _httpStatus;
457
- if ("httpStatus" in error) {
458
- _httpStatus = error.httpStatus;
459
- } else if ("status" in error) {
460
- _httpStatus = error.status;
461
- } else if ("statusCode" in error) {
462
- _httpStatus = error.statusCode;
463
- }
464
- if (_httpStatus !== void 0 && _httpStatus !== null) {
465
- const num = typeof _httpStatus === "number" ? _httpStatus : Number(_httpStatus);
466
- httpStatus = _ErrorX.validateHttpStatus(num);
467
- }
468
428
  if ("type" in error && error.type) {
469
429
  type = _ErrorX.validateType(String(error.type));
470
430
  }
471
- if ("sourceUrl" in error && error.sourceUrl) {
472
- url = String(error.sourceUrl);
473
- } else if ("url" in error && error.url) {
474
- url = String(error.url);
475
- }
476
431
  if ("docsUrl" in error && error.docsUrl) {
477
432
  href = String(error.docsUrl);
478
433
  } else if ("href" in error && error.href) {
@@ -487,7 +442,9 @@ var ErrorX = class _ErrorX extends Error {
487
442
  } else if ("component" in error && error.component) {
488
443
  source = String(error.component);
489
444
  }
490
- metadata = { originalError: error };
445
+ if ("metadata" in error && typeof error.metadata === "object" && error.metadata !== null) {
446
+ metadata = error.metadata;
447
+ }
491
448
  }
492
449
  }
493
450
  const options = {
@@ -498,9 +455,7 @@ var ErrorX = class _ErrorX extends Error {
498
455
  if (uiMessage) options.uiMessage = uiMessage;
499
456
  if (cause) options.cause = cause;
500
457
  if (Object.keys(metadata).length > 0) options.metadata = metadata;
501
- if (httpStatus) options.httpStatus = httpStatus;
502
458
  if (type) options.type = type;
503
- if (url) options.sourceUrl = url;
504
459
  if (href) options.docsUrl = href;
505
460
  if (source) options.source = source;
506
461
  return options;
@@ -578,15 +533,9 @@ ${this.stack}`;
578
533
  metadata: safeMetadata,
579
534
  timestamp: this.timestamp
580
535
  };
581
- if (this.httpStatus !== void 0) {
582
- serialized.httpStatus = this.httpStatus;
583
- }
584
536
  if (this.type !== void 0) {
585
537
  serialized.type = this.type;
586
538
  }
587
- if (this.sourceUrl !== void 0) {
588
- serialized.sourceUrl = this.sourceUrl;
589
- }
590
539
  if (this.docsUrl !== void 0) {
591
540
  serialized.docsUrl = this.docsUrl;
592
541
  }
@@ -629,9 +578,7 @@ ${this.stack}`;
629
578
  name: serialized.name,
630
579
  code: serialized.code,
631
580
  uiMessage: serialized.uiMessage,
632
- httpStatus: serialized.httpStatus,
633
581
  type: serialized.type,
634
- sourceUrl: serialized.sourceUrl,
635
582
  docsUrl: serialized.docsUrl,
636
583
  source: serialized.source,
637
584
  cause: serialized.cause
@@ -652,317 +599,278 @@ ${this.stack}`;
652
599
  var http = {
653
600
  // 4xx Client Errors
654
601
  400: {
655
- httpStatus: 400,
656
602
  code: "BAD_REQUEST",
657
603
  name: "Bad Request Error",
658
604
  message: "Bad request.",
659
605
  uiMessage: "The request could not be processed. Please check your input and try again.",
660
- type: "http"
606
+ metadata: { status: 400 }
661
607
  },
662
608
  401: {
663
- httpStatus: 401,
664
609
  code: "UNAUTHORIZED",
665
610
  name: "Unauthorized Error",
666
611
  message: "Unauthorized.",
667
612
  uiMessage: "Authentication required. Please log in to continue.",
668
- type: "http"
613
+ metadata: { status: 401 }
669
614
  },
670
615
  402: {
671
- httpStatus: 402,
672
616
  code: "PAYMENT_REQUIRED",
673
617
  name: "Payment Required Error",
674
618
  message: "Payment required.",
675
619
  uiMessage: "Payment is required to access this resource.",
676
- type: "http"
620
+ metadata: { status: 402 }
677
621
  },
678
622
  403: {
679
- httpStatus: 403,
680
623
  code: "FORBIDDEN",
681
624
  name: "Forbidden Error",
682
625
  message: "Forbidden.",
683
626
  uiMessage: "You do not have permission to access this resource.",
684
- type: "http"
627
+ metadata: { status: 403 }
685
628
  },
686
629
  404: {
687
- httpStatus: 404,
688
630
  code: "NOT_FOUND",
689
631
  name: "Not Found Error",
690
632
  message: "Not found.",
691
633
  uiMessage: "The requested resource could not be found.",
692
- type: "http"
634
+ metadata: { status: 404 }
693
635
  },
694
636
  405: {
695
- httpStatus: 405,
696
637
  code: "METHOD_NOT_ALLOWED",
697
638
  name: "Method Not Allowed Error",
698
639
  message: "Method not allowed.",
699
640
  uiMessage: "This action is not allowed for the requested resource.",
700
- type: "http"
641
+ metadata: { status: 405 }
701
642
  },
702
643
  406: {
703
- httpStatus: 406,
704
644
  code: "NOT_ACCEPTABLE",
705
645
  name: "Not Acceptable Error",
706
646
  message: "Not acceptable.",
707
647
  uiMessage: "The requested format is not supported.",
708
- type: "http"
648
+ metadata: { status: 406 }
709
649
  },
710
650
  407: {
711
- httpStatus: 407,
712
651
  code: "PROXY_AUTHENTICATION_REQUIRED",
713
652
  name: "Proxy Authentication Required Error",
714
653
  message: "Proxy authentication required.",
715
654
  uiMessage: "Proxy authentication is required to access this resource.",
716
- type: "http"
655
+ metadata: { status: 407 }
717
656
  },
718
657
  408: {
719
- httpStatus: 408,
720
658
  code: "REQUEST_TIMEOUT",
721
659
  name: "Request Timeout Error",
722
660
  message: "Request timeout.",
723
661
  uiMessage: "The request took too long to complete. Please try again.",
724
- type: "http"
662
+ metadata: { status: 408 }
725
663
  },
726
664
  409: {
727
- httpStatus: 409,
728
665
  code: "CONFLICT",
729
666
  name: "Conflict Error",
730
667
  message: "Conflict.",
731
668
  uiMessage: "The request conflicts with the current state. Please refresh and try again.",
732
- type: "http"
669
+ metadata: { status: 409 }
733
670
  },
734
671
  410: {
735
- httpStatus: 410,
736
672
  code: "GONE",
737
673
  name: "Gone Error",
738
674
  message: "Gone.",
739
675
  uiMessage: "This resource is no longer available.",
740
- type: "http"
676
+ metadata: { status: 410 }
741
677
  },
742
678
  411: {
743
- httpStatus: 411,
744
679
  code: "LENGTH_REQUIRED",
745
680
  name: "Length Required Error",
746
681
  message: "Length required.",
747
682
  uiMessage: "The request is missing required length information.",
748
- type: "http"
683
+ metadata: { status: 411 }
749
684
  },
750
685
  412: {
751
- httpStatus: 412,
752
686
  code: "PRECONDITION_FAILED",
753
687
  name: "Precondition Failed Error",
754
688
  message: "Precondition failed.",
755
689
  uiMessage: "A required condition was not met. Please try again.",
756
- type: "http"
690
+ metadata: { status: 412 }
757
691
  },
758
692
  413: {
759
- httpStatus: 413,
760
693
  code: "PAYLOAD_TOO_LARGE",
761
694
  name: "Payload Too Large Error",
762
695
  message: "Payload too large.",
763
696
  uiMessage: "The request is too large. Please reduce the size and try again.",
764
- type: "http"
697
+ metadata: { status: 413 }
765
698
  },
766
699
  414: {
767
- httpStatus: 414,
768
700
  code: "URI_TOO_LONG",
769
701
  name: "URI Too Long Error",
770
702
  message: "URI too long.",
771
703
  uiMessage: "The request URL is too long.",
772
- type: "http"
704
+ metadata: { status: 414 }
773
705
  },
774
706
  415: {
775
- httpStatus: 415,
776
707
  code: "UNSUPPORTED_MEDIA_TYPE",
777
708
  name: "Unsupported Media Type Error",
778
709
  message: "Unsupported media type.",
779
710
  uiMessage: "The file type is not supported.",
780
- type: "http"
711
+ metadata: { status: 415 }
781
712
  },
782
713
  416: {
783
- httpStatus: 416,
784
714
  code: "RANGE_NOT_SATISFIABLE",
785
715
  name: "Range Not Satisfiable Error",
786
716
  message: "Range not satisfiable.",
787
717
  uiMessage: "The requested range cannot be satisfied.",
788
- type: "http"
718
+ metadata: { status: 416 }
789
719
  },
790
720
  417: {
791
- httpStatus: 417,
792
721
  code: "EXPECTATION_FAILED",
793
722
  name: "Expectation Failed Error",
794
723
  message: "Expectation failed.",
795
724
  uiMessage: "The server cannot meet the requirements of the request.",
796
- type: "http"
725
+ metadata: { status: 417 }
797
726
  },
798
727
  418: {
799
- httpStatus: 418,
800
728
  code: "IM_A_TEAPOT",
801
729
  name: "Im A Teapot Error",
802
730
  message: "I'm a teapot.",
803
731
  uiMessage: "I'm a teapot and cannot brew coffee.",
804
- type: "http"
732
+ metadata: { status: 418 }
805
733
  },
806
734
  422: {
807
- httpStatus: 422,
808
735
  code: "UNPROCESSABLE_ENTITY",
809
736
  name: "Unprocessable Entity Error",
810
737
  message: "Unprocessable entity.",
811
738
  uiMessage: "The request contains invalid data. Please check your input.",
812
- type: "http"
739
+ metadata: { status: 422 }
813
740
  },
814
741
  423: {
815
- httpStatus: 423,
816
742
  code: "LOCKED",
817
743
  name: "Locked Error",
818
744
  message: "Locked.",
819
745
  uiMessage: "This resource is locked and cannot be modified.",
820
- type: "http"
746
+ metadata: { status: 423 }
821
747
  },
822
748
  424: {
823
- httpStatus: 424,
824
749
  code: "FAILED_DEPENDENCY",
825
750
  name: "Failed Dependency Error",
826
751
  message: "Failed dependency.",
827
752
  uiMessage: "The request failed due to a dependency error.",
828
- type: "http"
753
+ metadata: { status: 424 }
829
754
  },
830
755
  425: {
831
- httpStatus: 425,
832
756
  code: "TOO_EARLY",
833
757
  name: "Too Early Error",
834
758
  message: "Too early.",
835
759
  uiMessage: "The request was sent too early. Please try again later.",
836
- type: "http"
760
+ metadata: { status: 425 }
837
761
  },
838
762
  426: {
839
- httpStatus: 426,
840
763
  code: "UPGRADE_REQUIRED",
841
764
  name: "Upgrade Required Error",
842
765
  message: "Upgrade required.",
843
766
  uiMessage: "Please upgrade to continue using this service.",
844
- type: "http"
767
+ metadata: { status: 426 }
845
768
  },
846
769
  428: {
847
- httpStatus: 428,
848
770
  code: "PRECONDITION_REQUIRED",
849
771
  name: "Precondition Required Error",
850
772
  message: "Precondition required.",
851
773
  uiMessage: "Required conditions are missing from the request.",
852
- type: "http"
774
+ metadata: { status: 428 }
853
775
  },
854
776
  429: {
855
- httpStatus: 429,
856
777
  code: "TOO_MANY_REQUESTS",
857
778
  name: "Too Many Requests Error",
858
779
  message: "Too many requests.",
859
780
  uiMessage: "You have made too many requests. Please wait and try again.",
860
- type: "http"
781
+ metadata: { status: 429 }
861
782
  },
862
783
  431: {
863
- httpStatus: 431,
864
784
  code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
865
785
  name: "Request Header Fields Too Large Error",
866
786
  message: "Request header fields too large.",
867
787
  uiMessage: "The request headers are too large.",
868
- type: "http"
788
+ metadata: { status: 431 }
869
789
  },
870
790
  451: {
871
- httpStatus: 451,
872
791
  code: "UNAVAILABLE_FOR_LEGAL_REASONS",
873
792
  name: "Unavailable For Legal Reasons Error",
874
793
  message: "Unavailable for legal reasons.",
875
794
  uiMessage: "This content is unavailable for legal reasons.",
876
- type: "http"
795
+ metadata: { status: 451 }
877
796
  },
878
797
  // 5xx Server Errors
879
798
  500: {
880
- httpStatus: 500,
881
799
  code: "INTERNAL_SERVER_ERROR",
882
800
  name: "Internal Server Error",
883
801
  message: "Internal server error.",
884
802
  uiMessage: "An unexpected error occurred. Please try again later.",
885
- type: "http"
803
+ metadata: { status: 500 }
886
804
  },
887
805
  501: {
888
- httpStatus: 501,
889
806
  code: "NOT_IMPLEMENTED",
890
807
  name: "Not Implemented Error",
891
808
  message: "Not implemented.",
892
809
  uiMessage: "This feature is not yet available.",
893
- type: "http"
810
+ metadata: { status: 501 }
894
811
  },
895
812
  502: {
896
- httpStatus: 502,
897
813
  code: "BAD_GATEWAY",
898
814
  name: "Bad Gateway Error",
899
815
  message: "Bad gateway.",
900
816
  uiMessage: "Unable to connect to the server. Please try again later.",
901
- type: "http"
817
+ metadata: { status: 502 }
902
818
  },
903
819
  503: {
904
- httpStatus: 503,
905
820
  code: "SERVICE_UNAVAILABLE",
906
821
  name: "Service Unavailable Error",
907
822
  message: "Service unavailable.",
908
823
  uiMessage: "The service is temporarily unavailable. Please try again later.",
909
- type: "http"
824
+ metadata: { status: 503 }
910
825
  },
911
826
  504: {
912
- httpStatus: 504,
913
827
  code: "GATEWAY_TIMEOUT",
914
828
  name: "Gateway Timeout Error",
915
829
  message: "Gateway timeout.",
916
830
  uiMessage: "The server took too long to respond. Please try again.",
917
- type: "http"
831
+ metadata: { status: 504 }
918
832
  },
919
833
  505: {
920
- httpStatus: 505,
921
834
  code: "HTTP_VERSION_NOT_SUPPORTED",
922
835
  name: "HTTP Version Not Supported Error",
923
836
  message: "HTTP version not supported.",
924
837
  uiMessage: "Your browser version is not supported.",
925
- type: "http"
838
+ metadata: { status: 505 }
926
839
  },
927
840
  506: {
928
- httpStatus: 506,
929
841
  code: "VARIANT_ALSO_NEGOTIATES",
930
842
  name: "Variant Also Negotiates Error",
931
843
  message: "Variant also negotiates.",
932
844
  uiMessage: "The server has an internal configuration error.",
933
- type: "http"
845
+ metadata: { status: 506 }
934
846
  },
935
847
  507: {
936
- httpStatus: 507,
937
848
  code: "INSUFFICIENT_STORAGE",
938
849
  name: "Insufficient Storage Error",
939
850
  message: "Insufficient storage.",
940
851
  uiMessage: "The server has insufficient storage to complete the request.",
941
- type: "http"
852
+ metadata: { status: 507 }
942
853
  },
943
854
  508: {
944
- httpStatus: 508,
945
855
  code: "LOOP_DETECTED",
946
856
  name: "Loop Detected Error",
947
857
  message: "Loop detected.",
948
858
  uiMessage: "The server detected an infinite loop.",
949
- type: "http"
859
+ metadata: { status: 508 }
950
860
  },
951
861
  510: {
952
- httpStatus: 510,
953
862
  code: "NOT_EXTENDED",
954
863
  name: "Not Extended Error",
955
864
  message: "Not extended.",
956
865
  uiMessage: "Additional extensions are required.",
957
- type: "http"
866
+ metadata: { status: 510 }
958
867
  },
959
868
  511: {
960
- httpStatus: 511,
961
869
  code: "NETWORK_AUTHENTICATION_REQUIRED",
962
870
  name: "Network Authentication Required Error",
963
871
  message: "Network authentication required.",
964
872
  uiMessage: "Network authentication is required to access this resource.",
965
- type: "http"
873
+ metadata: { status: 511 }
966
874
  }
967
875
  };
968
876