wrap-error-constructor-with-cause.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. var getBuiltIn = require('../internals/get-built-in');
  3. var hasOwn = require('../internals/has-own-property');
  4. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  5. var isPrototypeOf = require('../internals/object-is-prototype-of');
  6. var setPrototypeOf = require('../internals/object-set-prototype-of');
  7. var copyConstructorProperties = require('../internals/copy-constructor-properties');
  8. var proxyAccessor = require('../internals/proxy-accessor');
  9. var inheritIfRequired = require('../internals/inherit-if-required');
  10. var normalizeStringArgument = require('../internals/normalize-string-argument');
  11. var installErrorCause = require('../internals/install-error-cause');
  12. var installErrorStack = require('../internals/error-stack-install');
  13. var DESCRIPTORS = require('../internals/descriptors');
  14. var IS_PURE = require('../internals/is-pure');
  15. module.exports = function (FULL_NAME, wrapper, FORCED, IS_AGGREGATE_ERROR) {
  16. var STACK_TRACE_LIMIT = 'stackTraceLimit';
  17. var OPTIONS_POSITION = IS_AGGREGATE_ERROR ? 2 : 1;
  18. var path = FULL_NAME.split('.');
  19. var ERROR_NAME = path[path.length - 1];
  20. var OriginalError = getBuiltIn.apply(null, path);
  21. if (!OriginalError) return;
  22. var OriginalErrorPrototype = OriginalError.prototype;
  23. // V8 9.3- bug https://bugs.chromium.org/p/v8/issues/detail?id=12006
  24. if (!IS_PURE && hasOwn(OriginalErrorPrototype, 'cause')) delete OriginalErrorPrototype.cause;
  25. if (!FORCED) return OriginalError;
  26. var BaseError = getBuiltIn('Error');
  27. var WrappedError = wrapper(function (a, b) {
  28. var message = normalizeStringArgument(IS_AGGREGATE_ERROR ? b : a, undefined);
  29. var result = IS_AGGREGATE_ERROR ? new OriginalError(a) : new OriginalError();
  30. if (message !== undefined) createNonEnumerableProperty(result, 'message', message);
  31. installErrorStack(result, WrappedError, result.stack, 2);
  32. if (this && isPrototypeOf(OriginalErrorPrototype, this)) inheritIfRequired(result, this, WrappedError);
  33. if (arguments.length > OPTIONS_POSITION) installErrorCause(result, arguments[OPTIONS_POSITION]);
  34. return result;
  35. });
  36. WrappedError.prototype = OriginalErrorPrototype;
  37. if (ERROR_NAME !== 'Error') {
  38. if (setPrototypeOf) setPrototypeOf(WrappedError, BaseError);
  39. else copyConstructorProperties(WrappedError, BaseError, { name: true });
  40. } else if (DESCRIPTORS && STACK_TRACE_LIMIT in OriginalError) {
  41. proxyAccessor(WrappedError, OriginalError, STACK_TRACE_LIMIT);
  42. proxyAccessor(WrappedError, OriginalError, 'prepareStackTrace');
  43. }
  44. copyConstructorProperties(WrappedError, OriginalError);
  45. if (!IS_PURE) try {
  46. // Safari 13- bug: WebAssembly errors does not have a proper `.name`
  47. if (OriginalErrorPrototype.name !== ERROR_NAME) {
  48. createNonEnumerableProperty(OriginalErrorPrototype, 'name', ERROR_NAME);
  49. }
  50. OriginalErrorPrototype.constructor = WrappedError;
  51. } catch (error) { /* empty */ }
  52. return WrappedError;
  53. };