-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
packages/core-js/internals/wrap-error-constructor-with-cause.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} |