Skip to content

Commit

Permalink
add Error#cause
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 10, 2021
1 parent bfa96a5 commit 139799a
Show file tree
Hide file tree
Showing 16 changed files with 271 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,11 @@ export const data = {
},
// TODO: Remove from `core-js@4`
'esnext.aggregate-error': null,
'esnext.aggregate-error.cause': {
chrome: '94',
firefox: '91',
safari: '15.0',
},
'esnext.array.from-async': {
},
// TODO: Remove from `core-js@4`
Expand Down Expand Up @@ -1493,6 +1498,11 @@ 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
4 changes: 4 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ export default {
'esnext.array.from-async',
'esnext.typed-array.from-async',
],
3.19: [
'esnext.aggregate-error.cause',
'esnext.error.cause',
],
};
1 change: 1 addition & 0 deletions packages/core-js/features/aggregate-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
require('../modules/esnext.aggregate-error');

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

module.exports = parent;
4 changes: 4 additions & 0 deletions packages/core-js/features/error/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../modules/esnext.error.cause');
var path = require('../../internals/path');

module.exports = path;
4 changes: 4 additions & 0 deletions packages/core-js/features/error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../modules/esnext.error.cause');
var path = require('../../internals/path');

module.exports = path;
39 changes: 39 additions & 0 deletions packages/core-js/internals/wrap-error-constructor-with-cause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
var getBuiltIn = require('../internals/get-built-in');
var global = require('../internals/global');
var hasOwn = require('../internals/has-own-property');
var copyConstructorProperties = require('../internals/copy-constructor-properties');
var inheritIfRequired = require('../internals/inherit-if-required');
var installErrorCause = require('../internals/install-error-cause');
var IS_PURE = require('../internals/is-pure');

module.exports = function (ERROR_NAME, wrapper, FORCED, IS_AGGREGATE_ERROR) {
var OPTIONS_POSITION = IS_AGGREGATE_ERROR ? 2 : 1;
var OriginalError = getBuiltIn(ERROR_NAME);

if (!OriginalError) return;

var OriginalErrorPrototype = OriginalError.prototype;

// V8 9.3- bug https://bugs.chromium.org/p/v8/issues/detail?id=12006
if (!IS_PURE && hasOwn(OriginalErrorPrototype, 'cause')) delete OriginalErrorPrototype.cause;

if (!FORCED) return OriginalError;

var WrappedError = wrapper(function () {
var result = OriginalError.apply(this, arguments);
if (this && this !== global) inheritIfRequired(result, this, WrappedError);
if (arguments.length > OPTIONS_POSITION) installErrorCause(result, arguments[OPTIONS_POSITION]);
return result;
});

WrappedError.prototype = OriginalErrorPrototype;

copyConstructorProperties(WrappedError, OriginalError);

if (!IS_PURE) try {
OriginalErrorPrototype.constructor = WrappedError;
} catch (error) { /* empty */ }

return WrappedError;
};
14 changes: 14 additions & 0 deletions packages/core-js/modules/esnext.aggregate-error.cause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var wrapErrorConstructorWithCause = require('../internals/wrap-error-constructor-with-cause');

var AGGREGATE_ERROR = 'AggregateError';
var $AggregateError = getBuiltIn(AGGREGATE_ERROR);
var FORCED = $AggregateError && $AggregateError([1], 'e', { cause: 7 }).cause !== 7;

$({ global: true, forced: FORCED }, {
AggregateError: wrapErrorConstructorWithCause(AGGREGATE_ERROR, function (init) {
// eslint-disable-next-line no-unused-vars -- required for functions `.length`
return function AggregateError(errors, message) { return init.apply(this, arguments); };
}, FORCED, true)
});
29 changes: 29 additions & 0 deletions packages/core-js/modules/esnext.error.cause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable no-unused-vars -- required for functions `.length` */
var $ = require('../internals/export');
var wrapErrorConstructorWithCause = require('../internals/wrap-error-constructor-with-cause');

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),
EvalError: wrapErrorConstructorWithCause('EvalError', function (init) {
return function EvalError(message) { return init.apply(this, arguments); };
}, FORCED),
RangeError: wrapErrorConstructorWithCause('RangeError', function (init) {
return function RangeError(message) { return init.apply(this, arguments); };
}, FORCED),
ReferenceError: wrapErrorConstructorWithCause('ReferenceError', function (init) {
return function ReferenceError(message) { return init.apply(this, arguments); };
}, FORCED),
SyntaxError: wrapErrorConstructorWithCause('SyntaxError', function (init) {
return function SyntaxError(message) { return init.apply(this, arguments); };
}, FORCED),
TypeError: wrapErrorConstructorWithCause('TypeError', function (init) {
return function TypeError(message) { return init.apply(this, arguments); };
}, FORCED),
URIError: wrapErrorConstructorWithCause('URIError', function (init) {
return function URIError(message) { return init.apply(this, arguments); };
}, FORCED)
});
3 changes: 3 additions & 0 deletions packages/core-js/proposals/error-cause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// https://github.com/tc39/proposal-error-cause
require('../modules/esnext.aggregate-error.cause');
require('../modules/esnext.error.cause');
1 change: 1 addition & 0 deletions packages/core-js/stage/3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('../proposals/array-find-from-last');
require('../proposals/error-cause');
var parent = require('./4');

module.exports = parent;
3 changes: 3 additions & 0 deletions tests/commonjs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ for (PATH of ['core-js-pure', 'core-js']) {
load(NS, 'bigint');
ok(typeof load(NS, 'composite-key')({}, 1, {}) === 'object');
ok(typeof load(NS, 'composite-symbol')({}, 1, {}) === 'symbol');
ok(load(NS, 'error/constructor').Error(1, { cause: 7 }).cause === 7);
ok(load(NS, 'error').Error(1, { cause: 7 }).cause === 7);
ok(typeof load(NS, 'iterator') === 'function');
ok(typeof load(NS, 'iterator/as-indexed-pairs') === 'function');
ok(typeof load(NS, 'iterator/drop') === 'function');
Expand Down Expand Up @@ -750,6 +752,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
load('proposals/collection-of-from');
load('proposals/decorators');
load('proposals/efficient-64-bit-arithmetic');
load('proposals/error-cause');
load('proposals/global-this');
load('proposals/iterator-helpers');
load('proposals/keys-composition');
Expand Down
8 changes: 8 additions & 0 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,10 @@ GLOBAL.tests = {
&& set.add({}) == set
&& set[Symbol.toStringTag];
}],
'esnext.aggregate-error.cause': function () {
return AggregateError([1], 'e', { cause: 7 }).cause === 7
&& !('cause' in AggregateError.prototype);
},
'esnext.array.from-async': function () {
return Array.fromAsync;
},
Expand Down Expand Up @@ -1267,6 +1271,10 @@ GLOBAL.tests = {
'esnext.composite-key': function () {
return compositeKey;
},
'esnext.error.cause': function () {
return Error('e', { cause: 7 }).cause === 7
&& !('cause' in Error.prototype);
},
'esnext.composite-symbol': function () {
return compositeSymbol;
},
Expand Down
31 changes: 31 additions & 0 deletions tests/pure/es.aggregate-error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AggregateError from 'core-js-pure/es/aggregate-error';
import Symbol from 'core-js-pure/es/symbol';
import create from 'core-js-pure/es/object/create';

QUnit.test('AggregateError', assert => {
assert.isFunction(AggregateError);
Expand All @@ -12,4 +13,34 @@ QUnit.test('AggregateError', assert => {
assert.same(AggregateError([1], 'foo').message, 'foo');
assert.deepEqual(AggregateError([1, 2, 3]).errors, [1, 2, 3]);
assert.throws(() => AggregateError([1], Symbol()), 'throws on symbol as a message');

assert.ok(AggregateError([1], 1) instanceof AggregateError, 'no cause, without new');
assert.ok(new AggregateError([1], 1) instanceof AggregateError, 'no cause, with new');

assert.ok(AggregateError([1], 1, {}) instanceof AggregateError, 'with options, without new');
assert.ok(new AggregateError([1], 1, {}) instanceof AggregateError, 'with options, with new');

assert.ok(AggregateError([1], 1, 'foo') instanceof AggregateError, 'non-object options, without new');
assert.ok(new AggregateError([1], 1, 'foo') instanceof AggregateError, 'non-object options, with new');

assert.same(AggregateError([1], 1, { cause: 7 }).cause, 7, 'cause, without new');
assert.same(new AggregateError([1], 1, { cause: 7 }).cause, 7, 'cause, with new');

assert.same(AggregateError([1], 1, create({ cause: 7 })).cause, 7, 'prototype cause, without new');
assert.same(new AggregateError([1], 1, create({ cause: 7 })).cause, 7, 'prototype cause, with new');

let error = AggregateError([1], 1, { cause: 7 });
assert.deepEqual(error.errors, [1]);
assert.same(error.name, 'AggregateError', 'instance name');
assert.same(error.message, '1', 'instance message');
assert.same(error.cause, 7, 'instance cause');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(error.hasOwnProperty('cause'), 'cause is own');

error = AggregateError([1]);
assert.deepEqual(error.errors, [1]);
assert.same(error.message, '', 'default instance message');
assert.same(error.cause, undefined, 'default instance cause undefined');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(!error.hasOwnProperty('cause'), 'default instance cause missed');
});
39 changes: 39 additions & 0 deletions tests/pure/esnext.error.cause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import path from 'core-js-pure/features/error';
import create from 'core-js-pure/es/object/create';

for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
QUnit.test(`${ ERROR_NAME } constructor with 'cause' param`, assert => {
const $Error = path[ERROR_NAME];
assert.isFunction($Error);
assert.arity($Error, 1);
assert.name($Error, ERROR_NAME);

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

assert.ok($Error(1, {}) instanceof $Error, 'with options, without new');
assert.ok(new $Error(1, {}) instanceof $Error, 'with options, with new');

assert.ok($Error(1, 'foo') instanceof $Error, 'non-object options, without new');
assert.ok(new $Error(1, 'foo') instanceof $Error, 'non-object options, with new');

assert.same($Error(1, { cause: 7 }).cause, 7, 'cause, without new');
assert.same(new $Error(1, { cause: 7 }).cause, 7, 'cause, with new');

assert.same($Error(1, create({ cause: 7 })).cause, 7, 'prototype cause, without new');
assert.same(new $Error(1, create({ cause: 7 })).cause, 7, 'prototype cause, with new');

let error = $Error(1, { cause: 7 });
assert.same(error.name, ERROR_NAME, 'instance name');
assert.same(error.message, '1', 'instance message');
assert.same(error.cause, 7, 'instance cause');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(error.hasOwnProperty('cause'), 'cause is own');

error = $Error();
assert.same(error.message, '', 'default instance message');
assert.same(error.cause, undefined, 'default instance cause undefined');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(!error.hasOwnProperty('cause'), 'default instance cause missed');
});
}
36 changes: 36 additions & 0 deletions tests/tests/es.aggregate-error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { create } = Object;

QUnit.test('AggregateError', assert => {
assert.isFunction(AggregateError);
assert.arity(AggregateError, 2);
Expand All @@ -10,4 +12,38 @@ QUnit.test('AggregateError', assert => {
assert.same(AggregateError([1], 'foo').message, 'foo');
assert.deepEqual(AggregateError([1, 2, 3]).errors, [1, 2, 3]);
assert.throws(() => AggregateError([1], Symbol()), 'throws on symbol as a message');

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

assert.ok(AggregateError([1], 1) instanceof AggregateError, 'no cause, without new');
assert.ok(new AggregateError([1], 1) instanceof AggregateError, 'no cause, with new');

assert.ok(AggregateError([1], 1, {}) instanceof AggregateError, 'with options, without new');
assert.ok(new AggregateError([1], 1, {}) instanceof AggregateError, 'with options, with new');

assert.ok(AggregateError([1], 1, 'foo') instanceof AggregateError, 'non-object options, without new');
assert.ok(new AggregateError([1], 1, 'foo') instanceof AggregateError, 'non-object options, with new');

assert.same(AggregateError([1], 1, { cause: 7 }).cause, 7, 'cause, without new');
assert.same(new AggregateError([1], 1, { cause: 7 }).cause, 7, 'cause, with new');

assert.same(AggregateError([1], 1, create({ cause: 7 })).cause, 7, 'prototype cause, without new');
assert.same(new AggregateError([1], 1, create({ cause: 7 })).cause, 7, 'prototype cause, with new');

let error = AggregateError([1], 1, { cause: 7 });
assert.deepEqual(error.errors, [1]);
assert.same(error.name, 'AggregateError', 'instance name');
assert.same(error.message, '1', 'instance message');
assert.same(error.cause, 7, 'instance cause');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(error.hasOwnProperty('cause'), 'cause is own');

error = AggregateError([1]);
assert.deepEqual(error.errors, [1]);
assert.same(error.message, '', 'default instance message');
assert.same(error.cause, undefined, 'default instance cause undefined');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(!error.hasOwnProperty('cause'), 'default instance cause missed');
});
45 changes: 45 additions & 0 deletions tests/tests/esnext.error.cause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GLOBAL } from '../helpers/constants';

const { create } = Object;

for (const ERROR_NAME of ['Error', 'EvalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError']) {
QUnit.test(`${ ERROR_NAME } constructor with 'cause' param`, assert => {
const $Error = GLOBAL[ERROR_NAME];
assert.isFunction($Error);
assert.arity($Error, 1);
assert.name($Error, ERROR_NAME);
assert.looksNative($Error);

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');

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

assert.ok($Error(1, {}) instanceof $Error, 'with options, without new');
assert.ok(new $Error(1, {}) instanceof $Error, 'with options, with new');

assert.ok($Error(1, 'foo') instanceof $Error, 'non-object options, without new');
assert.ok(new $Error(1, 'foo') instanceof $Error, 'non-object options, with new');

assert.same($Error(1, { cause: 7 }).cause, 7, 'cause, without new');
assert.same(new $Error(1, { cause: 7 }).cause, 7, 'cause, with new');

assert.same($Error(1, create({ cause: 7 })).cause, 7, 'prototype cause, without new');
assert.same(new $Error(1, create({ cause: 7 })).cause, 7, 'prototype cause, with new');

let error = $Error(1, { cause: 7 });
assert.same(error.name, ERROR_NAME, 'instance name');
assert.same(error.message, '1', 'instance message');
assert.same(error.cause, 7, 'instance cause');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(error.hasOwnProperty('cause'), 'cause is own');

error = $Error();
assert.same(error.message, '', 'default instance message');
assert.same(error.cause, undefined, 'default instance cause undefined');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.ok(!error.hasOwnProperty('cause'), 'default instance cause missed');
});
}

0 comments on commit 139799a

Please sign in to comment.