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 |
|
---|
16 | module.exports = function (FULL_NAME, wrapper, FORCED, IS_AGGREGATE_ERROR) {
|
---|
17 | var STACK_TRACE_LIMIT = 'stackTraceLimit';
|
---|
18 | var OPTIONS_POSITION = IS_AGGREGATE_ERROR ? 2 : 1;
|
---|
19 | var path = FULL_NAME.split('.');
|
---|
20 | var ERROR_NAME = path[path.length - 1];
|
---|
21 | var OriginalError = getBuiltIn.apply(null, path);
|
---|
22 |
|
---|
23 | if (!OriginalError) return;
|
---|
24 |
|
---|
25 | var OriginalErrorPrototype = OriginalError.prototype;
|
---|
26 |
|
---|
27 | // V8 9.3- bug https://bugs.chromium.org/p/v8/issues/detail?id=12006
|
---|
28 | if (!IS_PURE && hasOwn(OriginalErrorPrototype, 'cause')) delete OriginalErrorPrototype.cause;
|
---|
29 |
|
---|
30 | if (!FORCED) return OriginalError;
|
---|
31 |
|
---|
32 | var BaseError = getBuiltIn('Error');
|
---|
33 |
|
---|
34 | var WrappedError = wrapper(function (a, b) {
|
---|
35 | var message = normalizeStringArgument(IS_AGGREGATE_ERROR ? b : a, undefined);
|
---|
36 | var result = IS_AGGREGATE_ERROR ? new OriginalError(a) : new OriginalError();
|
---|
37 | if (message !== undefined) createNonEnumerableProperty(result, 'message', message);
|
---|
38 | installErrorStack(result, WrappedError, result.stack, 2);
|
---|
39 | if (this && isPrototypeOf(OriginalErrorPrototype, this)) inheritIfRequired(result, this, WrappedError);
|
---|
40 | if (arguments.length > OPTIONS_POSITION) installErrorCause(result, arguments[OPTIONS_POSITION]);
|
---|
41 | return result;
|
---|
42 | });
|
---|
43 |
|
---|
44 | WrappedError.prototype = OriginalErrorPrototype;
|
---|
45 |
|
---|
46 | if (ERROR_NAME !== 'Error') {
|
---|
47 | if (setPrototypeOf) setPrototypeOf(WrappedError, BaseError);
|
---|
48 | else copyConstructorProperties(WrappedError, BaseError, { name: true });
|
---|
49 | } else if (DESCRIPTORS && STACK_TRACE_LIMIT in OriginalError) {
|
---|
50 | proxyAccessor(WrappedError, OriginalError, STACK_TRACE_LIMIT);
|
---|
51 | proxyAccessor(WrappedError, OriginalError, 'prepareStackTrace');
|
---|
52 | }
|
---|
53 |
|
---|
54 | copyConstructorProperties(WrappedError, OriginalError);
|
---|
55 |
|
---|
56 | if (!IS_PURE) try {
|
---|
57 | // Safari 13- bug: WebAssembly errors does not have a proper `.name`
|
---|
58 | if (OriginalErrorPrototype.name !== ERROR_NAME) {
|
---|
59 | createNonEnumerableProperty(OriginalErrorPrototype, 'name', ERROR_NAME);
|
---|
60 | }
|
---|
61 | OriginalErrorPrototype.constructor = WrappedError;
|
---|
62 | } catch (error) { /* empty */ }
|
---|
63 |
|
---|
64 | return WrappedError;
|
---|
65 | };
|
---|