extend-error.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.extendError = void 0;
  4. const isomorphic_node_1 = require("./isomorphic.node");
  5. const stack_1 = require("./stack");
  6. const to_json_1 = require("./to-json");
  7. const protectedProps = ["name", "message", "stack"];
  8. /**
  9. * Extends the new error with the properties of the original error and the `props` object.
  10. *
  11. * @param newError - The error object to extend
  12. * @param originalError - The original error object, if any
  13. * @param props - Additional properties to add, if any
  14. */
  15. function extendError(error, originalError, props) {
  16. let onoError = error;
  17. extendStack(onoError, originalError);
  18. // Copy properties from the original error
  19. if (originalError && typeof originalError === "object") {
  20. mergeErrors(onoError, originalError);
  21. }
  22. // The default `toJSON` method doesn't output props like `name`, `message`, `stack`, etc.
  23. // So replace it with one that outputs every property of the error.
  24. onoError.toJSON = to_json_1.toJSON;
  25. // On Node.js, add support for the `util.inspect()` method
  26. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  27. if (isomorphic_node_1.addInspectMethod) {
  28. isomorphic_node_1.addInspectMethod(onoError);
  29. }
  30. // Finally, copy custom properties that were specified by the user.
  31. // These props OVERWRITE any previous props
  32. if (props && typeof props === "object") {
  33. Object.assign(onoError, props);
  34. }
  35. return onoError;
  36. }
  37. exports.extendError = extendError;
  38. /**
  39. * Extend the error stack to include its cause
  40. */
  41. function extendStack(newError, originalError) {
  42. let stackProp = Object.getOwnPropertyDescriptor(newError, "stack");
  43. if (stack_1.isLazyStack(stackProp)) {
  44. stack_1.lazyJoinStacks(stackProp, newError, originalError);
  45. }
  46. else if (stack_1.isWritableStack(stackProp)) {
  47. newError.stack = stack_1.joinStacks(newError, originalError);
  48. }
  49. }
  50. /**
  51. * Merges properties of the original error with the new error.
  52. *
  53. * @param newError - The error object to extend
  54. * @param originalError - The original error object, if any
  55. */
  56. function mergeErrors(newError, originalError) {
  57. // Get the original error's keys
  58. // NOTE: We specifically exclude properties that we have already set on the new error.
  59. // This is _especially_ important for the `stack` property, because this property has
  60. // a lazy getter in some environments
  61. let keys = to_json_1.getDeepKeys(originalError, protectedProps);
  62. // HACK: We have to cast the errors to `any` so we can use symbol indexers.
  63. // see https://github.com/Microsoft/TypeScript/issues/1863
  64. let _newError = newError;
  65. let _originalError = originalError;
  66. for (let key of keys) {
  67. if (_newError[key] === undefined) {
  68. try {
  69. _newError[key] = _originalError[key];
  70. }
  71. catch (e) {
  72. // This property is read-only, so it can't be copied
  73. }
  74. }
  75. }
  76. }
  77. //# sourceMappingURL=extend-error.js.map