@bombillazo/error-x 0.4.1 → 0.4.3

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
@@ -175,7 +175,8 @@ var ErrorX = class _ErrorX extends Error {
175
175
  * docsMap: {
176
176
  * 'AUTH_FAILED': 'authentication-errors',
177
177
  * 'DB_ERROR': 'database-errors'
178
- * }
178
+ * },
179
+ * cleanStackDelimiter: 'app-entry-point' // Trim stack traces after this line
179
180
  * })
180
181
  * ```
181
182
  */
@@ -189,6 +190,19 @@ var ErrorX = class _ErrorX extends Error {
189
190
  static getConfig() {
190
191
  return _ErrorX._config;
191
192
  }
193
+ /**
194
+ * Reset global configuration to null.
195
+ * Useful for testing or when you want to clear all configuration.
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * ErrorX.resetConfig()
200
+ * const config = ErrorX.getConfig() // null
201
+ * ```
202
+ */
203
+ static resetConfig() {
204
+ _ErrorX._config = null;
205
+ }
192
206
  /**
193
207
  * Validates HTTP status code to ensure it's within valid range (100-599)
194
208
  *
@@ -276,13 +290,24 @@ var ErrorX = class _ErrorX extends Error {
276
290
  return [newErrorFirstLine, ...originalStackTrace].join("\n");
277
291
  }
278
292
  /**
279
- * Cleans the stack trace by removing ErrorX internal method calls.
293
+ * Cleans a stack trace by removing ErrorX internal method calls and optionally trimming after a delimiter.
280
294
  * This provides cleaner stack traces that focus on user code.
281
295
  *
282
- * @param stack - Raw stack trace to clean
283
- * @returns Cleaned stack trace without ErrorX internal calls
296
+ * @param stack - Raw stack trace string to clean
297
+ * @param delimiter - Optional delimiter to trim stack trace after (overrides config delimiter)
298
+ * @returns Cleaned stack trace without internal calls and optionally trimmed after delimiter
299
+ *
300
+ * @example
301
+ * ```typescript
302
+ * // Clean with pattern-based removal only
303
+ * const cleaned = ErrorX.cleanStack(error.stack)
304
+ *
305
+ * // Clean and trim after delimiter
306
+ * const trimmed = ErrorX.cleanStack(error.stack, 'my-app-entry')
307
+ * // Returns stack trace starting after the line containing 'my-app-entry'
308
+ * ```
284
309
  */
285
- static cleanStack(stack) {
310
+ static cleanStack(stack, delimiter) {
286
311
  if (!stack) return "";
287
312
  const config = _ErrorX.getConfig();
288
313
  const cleanStackConfig = config?.cleanStack ?? true;
@@ -306,30 +331,15 @@ var ErrorX = class _ErrorX extends Error {
306
331
  }
307
332
  cleanedLines.push(line);
308
333
  }
309
- return cleanedLines.join("\n");
310
- }
311
- /**
312
- * Processes an error's stack trace to trim it after a specified delimiter.
313
- * Useful for removing irrelevant stack frames before a specific function.
314
- *
315
- * @param error - Error whose stack to process
316
- * @param delimiter - String to search for in stack lines
317
- * @returns Processed stack trace starting after the delimiter
318
- *
319
- * @example
320
- * ```typescript
321
- * const processed = ErrorX.processErrorStack(error, 'my-app-entry')
322
- * // Returns stack trace starting after the line containing 'my-app-entry'
323
- * ```
324
- */
325
- static processErrorStack(error, delimiter) {
326
- let stack = error.stack ?? "";
327
- const stackLines = stack.split("\n");
328
- const delimiterIndex = stackLines.findIndex((line) => line.includes(delimiter));
329
- if (delimiterIndex !== -1) {
330
- stack = stackLines.slice(delimiterIndex + 1).join("\n");
334
+ let cleanedStack = cleanedLines.join("\n");
335
+ const activeDelimiter = delimiter ?? config?.cleanStackDelimiter;
336
+ if (activeDelimiter) {
337
+ const delimiterIndex = cleanedLines.findIndex((line) => line.includes(activeDelimiter));
338
+ if (delimiterIndex !== -1) {
339
+ cleanedStack = cleanedLines.slice(delimiterIndex + 1).join("\n");
340
+ }
331
341
  }
332
- return stack;
342
+ return cleanedStack;
333
343
  }
334
344
  /**
335
345
  * Creates a new ErrorX instance with additional metadata merged with existing metadata.
@@ -494,43 +504,6 @@ var ErrorX = class _ErrorX extends Error {
494
504
  const options = _ErrorX.convertUnknownToOptions(error);
495
505
  return new _ErrorX(options);
496
506
  }
497
- /**
498
- * Creates a new ErrorX instance with cleaned stack trace using the specified delimiter.
499
- * Returns the same instance if no delimiter is provided or no stack is available.
500
- *
501
- * @param delimiter - Optional string to search for in stack lines
502
- * @returns New ErrorX instance with cleaned stack trace, or the same instance if no cleaning needed
503
- *
504
- * @example
505
- * ```typescript
506
- * const error = new ErrorX({ message: 'Database error' })
507
- * const cleanedError = error.cleanStackTrace('database-layer')
508
- * // Returns new ErrorX with stack trace starting after 'database-layer'
509
- * ```
510
- */
511
- cleanStackTrace(delimiter) {
512
- if (delimiter && this.stack) {
513
- const options = {
514
- message: this.message,
515
- name: this.name,
516
- code: this.code,
517
- uiMessage: this.uiMessage,
518
- cause: this.cause,
519
- httpStatus: this.httpStatus,
520
- type: this.type,
521
- sourceUrl: this.sourceUrl,
522
- docsUrl: this.docsUrl,
523
- source: this.source
524
- };
525
- if (this.metadata !== void 0) {
526
- options.metadata = this.metadata;
527
- }
528
- const newError = new _ErrorX(options);
529
- newError.stack = _ErrorX.processErrorStack(this, delimiter);
530
- return newError;
531
- }
532
- return this;
533
- }
534
507
  /**
535
508
  * Converts the ErrorX instance to a detailed string representation.
536
509
  * Includes error name, message, code, timestamp, metadata, and stack trace.
@@ -672,7 +645,7 @@ ${this.stack}`;
672
645
  // src/presets.ts
673
646
  var http = {
674
647
  // 4xx Client Errors
675
- badRequest: {
648
+ 400: {
676
649
  httpStatus: 400,
677
650
  code: "BAD_REQUEST",
678
651
  name: "Bad Request Error",
@@ -680,7 +653,7 @@ var http = {
680
653
  uiMessage: "The request could not be processed. Please check your input and try again.",
681
654
  type: "http"
682
655
  },
683
- unauthorized: {
656
+ 401: {
684
657
  httpStatus: 401,
685
658
  code: "UNAUTHORIZED",
686
659
  name: "Unauthorized Error",
@@ -688,7 +661,7 @@ var http = {
688
661
  uiMessage: "Authentication required. Please log in to continue.",
689
662
  type: "http"
690
663
  },
691
- paymentRequired: {
664
+ 402: {
692
665
  httpStatus: 402,
693
666
  code: "PAYMENT_REQUIRED",
694
667
  name: "Payment Required Error",
@@ -696,7 +669,7 @@ var http = {
696
669
  uiMessage: "Payment is required to access this resource.",
697
670
  type: "http"
698
671
  },
699
- forbidden: {
672
+ 403: {
700
673
  httpStatus: 403,
701
674
  code: "FORBIDDEN",
702
675
  name: "Forbidden Error",
@@ -704,7 +677,7 @@ var http = {
704
677
  uiMessage: "You do not have permission to access this resource.",
705
678
  type: "http"
706
679
  },
707
- notFound: {
680
+ 404: {
708
681
  httpStatus: 404,
709
682
  code: "NOT_FOUND",
710
683
  name: "Not Found Error",
@@ -712,7 +685,7 @@ var http = {
712
685
  uiMessage: "The requested resource could not be found.",
713
686
  type: "http"
714
687
  },
715
- methodNotAllowed: {
688
+ 405: {
716
689
  httpStatus: 405,
717
690
  code: "METHOD_NOT_ALLOWED",
718
691
  name: "Method Not Allowed Error",
@@ -720,7 +693,7 @@ var http = {
720
693
  uiMessage: "This action is not allowed for the requested resource.",
721
694
  type: "http"
722
695
  },
723
- notAcceptable: {
696
+ 406: {
724
697
  httpStatus: 406,
725
698
  code: "NOT_ACCEPTABLE",
726
699
  name: "Not Acceptable Error",
@@ -728,7 +701,7 @@ var http = {
728
701
  uiMessage: "The requested format is not supported.",
729
702
  type: "http"
730
703
  },
731
- proxyAuthenticationRequired: {
704
+ 407: {
732
705
  httpStatus: 407,
733
706
  code: "PROXY_AUTHENTICATION_REQUIRED",
734
707
  name: "Proxy Authentication Required Error",
@@ -736,7 +709,7 @@ var http = {
736
709
  uiMessage: "Proxy authentication is required to access this resource.",
737
710
  type: "http"
738
711
  },
739
- requestTimeout: {
712
+ 408: {
740
713
  httpStatus: 408,
741
714
  code: "REQUEST_TIMEOUT",
742
715
  name: "Request Timeout Error",
@@ -744,7 +717,7 @@ var http = {
744
717
  uiMessage: "The request took too long to complete. Please try again.",
745
718
  type: "http"
746
719
  },
747
- conflict: {
720
+ 409: {
748
721
  httpStatus: 409,
749
722
  code: "CONFLICT",
750
723
  name: "Conflict Error",
@@ -752,7 +725,7 @@ var http = {
752
725
  uiMessage: "The request conflicts with the current state. Please refresh and try again.",
753
726
  type: "http"
754
727
  },
755
- gone: {
728
+ 410: {
756
729
  httpStatus: 410,
757
730
  code: "GONE",
758
731
  name: "Gone Error",
@@ -760,7 +733,7 @@ var http = {
760
733
  uiMessage: "This resource is no longer available.",
761
734
  type: "http"
762
735
  },
763
- lengthRequired: {
736
+ 411: {
764
737
  httpStatus: 411,
765
738
  code: "LENGTH_REQUIRED",
766
739
  name: "Length Required Error",
@@ -768,7 +741,7 @@ var http = {
768
741
  uiMessage: "The request is missing required length information.",
769
742
  type: "http"
770
743
  },
771
- preconditionFailed: {
744
+ 412: {
772
745
  httpStatus: 412,
773
746
  code: "PRECONDITION_FAILED",
774
747
  name: "Precondition Failed Error",
@@ -776,7 +749,7 @@ var http = {
776
749
  uiMessage: "A required condition was not met. Please try again.",
777
750
  type: "http"
778
751
  },
779
- payloadTooLarge: {
752
+ 413: {
780
753
  httpStatus: 413,
781
754
  code: "PAYLOAD_TOO_LARGE",
782
755
  name: "Payload Too Large Error",
@@ -784,7 +757,7 @@ var http = {
784
757
  uiMessage: "The request is too large. Please reduce the size and try again.",
785
758
  type: "http"
786
759
  },
787
- uriTooLong: {
760
+ 414: {
788
761
  httpStatus: 414,
789
762
  code: "URI_TOO_LONG",
790
763
  name: "URI Too Long Error",
@@ -792,7 +765,7 @@ var http = {
792
765
  uiMessage: "The request URL is too long.",
793
766
  type: "http"
794
767
  },
795
- unsupportedMediaType: {
768
+ 415: {
796
769
  httpStatus: 415,
797
770
  code: "UNSUPPORTED_MEDIA_TYPE",
798
771
  name: "Unsupported Media Type Error",
@@ -800,7 +773,7 @@ var http = {
800
773
  uiMessage: "The file type is not supported.",
801
774
  type: "http"
802
775
  },
803
- rangeNotSatisfiable: {
776
+ 416: {
804
777
  httpStatus: 416,
805
778
  code: "RANGE_NOT_SATISFIABLE",
806
779
  name: "Range Not Satisfiable Error",
@@ -808,7 +781,7 @@ var http = {
808
781
  uiMessage: "The requested range cannot be satisfied.",
809
782
  type: "http"
810
783
  },
811
- expectationFailed: {
784
+ 417: {
812
785
  httpStatus: 417,
813
786
  code: "EXPECTATION_FAILED",
814
787
  name: "Expectation Failed Error",
@@ -816,7 +789,7 @@ var http = {
816
789
  uiMessage: "The server cannot meet the requirements of the request.",
817
790
  type: "http"
818
791
  },
819
- imATeapot: {
792
+ 418: {
820
793
  httpStatus: 418,
821
794
  code: "IM_A_TEAPOT",
822
795
  name: "Im A Teapot Error",
@@ -824,7 +797,7 @@ var http = {
824
797
  uiMessage: "I'm a teapot and cannot brew coffee.",
825
798
  type: "http"
826
799
  },
827
- unprocessableEntity: {
800
+ 422: {
828
801
  httpStatus: 422,
829
802
  code: "UNPROCESSABLE_ENTITY",
830
803
  name: "Unprocessable Entity Error",
@@ -832,7 +805,7 @@ var http = {
832
805
  uiMessage: "The request contains invalid data. Please check your input.",
833
806
  type: "http"
834
807
  },
835
- locked: {
808
+ 423: {
836
809
  httpStatus: 423,
837
810
  code: "LOCKED",
838
811
  name: "Locked Error",
@@ -840,7 +813,7 @@ var http = {
840
813
  uiMessage: "This resource is locked and cannot be modified.",
841
814
  type: "http"
842
815
  },
843
- failedDependency: {
816
+ 424: {
844
817
  httpStatus: 424,
845
818
  code: "FAILED_DEPENDENCY",
846
819
  name: "Failed Dependency Error",
@@ -848,7 +821,7 @@ var http = {
848
821
  uiMessage: "The request failed due to a dependency error.",
849
822
  type: "http"
850
823
  },
851
- tooEarly: {
824
+ 425: {
852
825
  httpStatus: 425,
853
826
  code: "TOO_EARLY",
854
827
  name: "Too Early Error",
@@ -856,7 +829,7 @@ var http = {
856
829
  uiMessage: "The request was sent too early. Please try again later.",
857
830
  type: "http"
858
831
  },
859
- upgradeRequired: {
832
+ 426: {
860
833
  httpStatus: 426,
861
834
  code: "UPGRADE_REQUIRED",
862
835
  name: "Upgrade Required Error",
@@ -864,7 +837,7 @@ var http = {
864
837
  uiMessage: "Please upgrade to continue using this service.",
865
838
  type: "http"
866
839
  },
867
- preconditionRequired: {
840
+ 428: {
868
841
  httpStatus: 428,
869
842
  code: "PRECONDITION_REQUIRED",
870
843
  name: "Precondition Required Error",
@@ -872,7 +845,7 @@ var http = {
872
845
  uiMessage: "Required conditions are missing from the request.",
873
846
  type: "http"
874
847
  },
875
- tooManyRequests: {
848
+ 429: {
876
849
  httpStatus: 429,
877
850
  code: "TOO_MANY_REQUESTS",
878
851
  name: "Too Many Requests Error",
@@ -880,7 +853,7 @@ var http = {
880
853
  uiMessage: "You have made too many requests. Please wait and try again.",
881
854
  type: "http"
882
855
  },
883
- requestHeaderFieldsTooLarge: {
856
+ 431: {
884
857
  httpStatus: 431,
885
858
  code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
886
859
  name: "Request Header Fields Too Large Error",
@@ -888,7 +861,7 @@ var http = {
888
861
  uiMessage: "The request headers are too large.",
889
862
  type: "http"
890
863
  },
891
- unavailableForLegalReasons: {
864
+ 451: {
892
865
  httpStatus: 451,
893
866
  code: "UNAVAILABLE_FOR_LEGAL_REASONS",
894
867
  name: "Unavailable For Legal Reasons Error",
@@ -897,7 +870,7 @@ var http = {
897
870
  type: "http"
898
871
  },
899
872
  // 5xx Server Errors
900
- internalServerError: {
873
+ 500: {
901
874
  httpStatus: 500,
902
875
  code: "INTERNAL_SERVER_ERROR",
903
876
  name: "Internal Server Error",
@@ -905,7 +878,7 @@ var http = {
905
878
  uiMessage: "An unexpected error occurred. Please try again later.",
906
879
  type: "http"
907
880
  },
908
- notImplemented: {
881
+ 501: {
909
882
  httpStatus: 501,
910
883
  code: "NOT_IMPLEMENTED",
911
884
  name: "Not Implemented Error",
@@ -913,7 +886,7 @@ var http = {
913
886
  uiMessage: "This feature is not yet available.",
914
887
  type: "http"
915
888
  },
916
- badGateway: {
889
+ 502: {
917
890
  httpStatus: 502,
918
891
  code: "BAD_GATEWAY",
919
892
  name: "Bad Gateway Error",
@@ -921,7 +894,7 @@ var http = {
921
894
  uiMessage: "Unable to connect to the server. Please try again later.",
922
895
  type: "http"
923
896
  },
924
- serviceUnavailable: {
897
+ 503: {
925
898
  httpStatus: 503,
926
899
  code: "SERVICE_UNAVAILABLE",
927
900
  name: "Service Unavailable Error",
@@ -929,7 +902,7 @@ var http = {
929
902
  uiMessage: "The service is temporarily unavailable. Please try again later.",
930
903
  type: "http"
931
904
  },
932
- gatewayTimeout: {
905
+ 504: {
933
906
  httpStatus: 504,
934
907
  code: "GATEWAY_TIMEOUT",
935
908
  name: "Gateway Timeout Error",
@@ -937,7 +910,7 @@ var http = {
937
910
  uiMessage: "The server took too long to respond. Please try again.",
938
911
  type: "http"
939
912
  },
940
- httpVersionNotSupported: {
913
+ 505: {
941
914
  httpStatus: 505,
942
915
  code: "HTTP_VERSION_NOT_SUPPORTED",
943
916
  name: "HTTP Version Not Supported Error",
@@ -945,7 +918,7 @@ var http = {
945
918
  uiMessage: "Your browser version is not supported.",
946
919
  type: "http"
947
920
  },
948
- variantAlsoNegotiates: {
921
+ 506: {
949
922
  httpStatus: 506,
950
923
  code: "VARIANT_ALSO_NEGOTIATES",
951
924
  name: "Variant Also Negotiates Error",
@@ -953,7 +926,7 @@ var http = {
953
926
  uiMessage: "The server has an internal configuration error.",
954
927
  type: "http"
955
928
  },
956
- insufficientStorage: {
929
+ 507: {
957
930
  httpStatus: 507,
958
931
  code: "INSUFFICIENT_STORAGE",
959
932
  name: "Insufficient Storage Error",
@@ -961,7 +934,7 @@ var http = {
961
934
  uiMessage: "The server has insufficient storage to complete the request.",
962
935
  type: "http"
963
936
  },
964
- loopDetected: {
937
+ 508: {
965
938
  httpStatus: 508,
966
939
  code: "LOOP_DETECTED",
967
940
  name: "Loop Detected Error",
@@ -969,7 +942,7 @@ var http = {
969
942
  uiMessage: "The server detected an infinite loop.",
970
943
  type: "http"
971
944
  },
972
- notExtended: {
945
+ 510: {
973
946
  httpStatus: 510,
974
947
  code: "NOT_EXTENDED",
975
948
  name: "Not Extended Error",
@@ -977,7 +950,7 @@ var http = {
977
950
  uiMessage: "Additional extensions are required.",
978
951
  type: "http"
979
952
  },
980
- networkAuthenticationRequired: {
953
+ 511: {
981
954
  httpStatus: 511,
982
955
  code: "NETWORK_AUTHENTICATION_REQUIRED",
983
956
  name: "Network Authentication Required Error",