Skip to content

Commit

Permalink
error constructors should have Error or at least inherit static pro…
Browse files Browse the repository at this point in the history
…perties
  • Loading branch information
zloirock committed Oct 10, 2021
1 parent 139799a commit 7f40d86
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,11 @@ export const data = {
safari: '10.0',
rhino: '1.7.13',
},
'esnext.error.cause': {
chrome: '94',
firefox: '91',
safari: '15.0',
},
// TODO: Remove from `core-js@4`
'esnext.aggregate-error': null,
'esnext.aggregate-error.cause': {
Expand Down Expand Up @@ -1498,11 +1503,6 @@ export const data = {
},
'esnext.composite-symbol': {
},
'esnext.error.cause': {
chrome: '94',
firefox: '91',
safari: '15.0',
},
// TODO: Remove from `core-js@4`
'esnext.global-this': null,
'esnext.iterator.constructor': {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default {
'esnext.typed-array.from-async',
],
3.19: [
'esnext.aggregate-error.cause',
'esnext.error.cause',
'esnext.aggregate-error.cause',
],
};
3 changes: 2 additions & 1 deletion packages/core-js/features/aggregate-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require('../modules/esnext.error.cause');
// TODO: remove from `core-js@4`
require('../modules/esnext.aggregate-error');
require('../modules/esnext.aggregate-error.cause');

var parent = require('../stable/aggregate-error');
require('../modules/esnext.aggregate-error.cause');

module.exports = parent;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var getBuiltIn = require('../internals/get-built-in');
var global = require('../internals/global');
var hasOwn = require('../internals/has-own-property');
var setPrototypeOf = require('../internals/object-set-prototype-of');
var copyConstructorProperties = require('../internals/copy-constructor-properties');
var inheritIfRequired = require('../internals/inherit-if-required');
var installErrorCause = require('../internals/install-error-cause');
Expand All @@ -20,6 +21,8 @@ module.exports = function (ERROR_NAME, wrapper, FORCED, IS_AGGREGATE_ERROR) {

if (!FORCED) return OriginalError;

var BaseError = getBuiltIn('Error');

var WrappedError = wrapper(function () {
var result = OriginalError.apply(this, arguments);
if (this && this !== global) inheritIfRequired(result, this, WrappedError);
Expand All @@ -29,6 +32,11 @@ module.exports = function (ERROR_NAME, wrapper, FORCED, IS_AGGREGATE_ERROR) {

WrappedError.prototype = OriginalErrorPrototype;

if (OriginalError !== BaseError) {
if (setPrototypeOf) setPrototypeOf(WrappedError, BaseError);
else copyConstructorProperties(WrappedError, BaseError);
}

copyConstructorProperties(WrappedError, OriginalError);

if (!IS_PURE) try {
Expand Down
5 changes: 4 additions & 1 deletion packages/core-js/modules/esnext.error.cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ var FORCED = Error('e', { cause: 7 }).cause !== 7;
$({ global: true, forced: FORCED }, {
Error: wrapErrorConstructorWithCause('Error', function (init) {
return function Error(message) { return init.apply(this, arguments); };
}, FORCED),
}, FORCED)
});

$({ global: true, forced: FORCED }, {
EvalError: wrapErrorConstructorWithCause('EvalError', function (init) {
return function EvalError(message) { return init.apply(this, arguments); };
}, FORCED),
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/proposals/error-cause.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// https://github.com/tc39/proposal-error-cause
require('../modules/esnext.aggregate-error.cause');
require('../modules/esnext.error.cause');
require('../modules/esnext.aggregate-error.cause');
7 changes: 7 additions & 0 deletions tests/pure/esnext.error.cause.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PROTO } from '../helpers/constants';

import path from 'core-js-pure/features/error';
import create from 'core-js-pure/es/object/create';

Expand All @@ -8,6 +10,11 @@ for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError',
assert.arity($Error, 1);
assert.name($Error, ERROR_NAME);

if (PROTO && $Error !== path.Error) {
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(path.Error.isPrototypeOf($Error), 'constructor has `Error` in the prototype chain');
}

assert.ok($Error(1) instanceof $Error, 'no cause, without new');
assert.ok(new $Error(1) instanceof $Error, 'no cause, with new');

Expand Down
7 changes: 6 additions & 1 deletion tests/tests/esnext.error.cause.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GLOBAL } from '../helpers/constants';
import { GLOBAL, PROTO } from '../helpers/constants';

const { create } = Object;

Expand All @@ -10,6 +10,11 @@ for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError',
assert.name($Error, ERROR_NAME);
assert.looksNative($Error);

if (PROTO && $Error !== Error) {
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(Error.isPrototypeOf($Error), 'constructor has `Error` in the prototype chain');
}

assert.same($Error.prototype.constructor, $Error, 'prototype constructor');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(!$Error.prototype.hasOwnProperty('cause'), 'prototype hasn`t cause');
Expand Down

0 comments on commit 7f40d86

Please sign in to comment.