-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors
Error.cause
into _InstallErrorCause
- Loading branch information
1 parent
1695c17
commit 91a4700
Showing
4 changed files
with
93 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
dependencies = [ | ||
"_ESAbstract.CreateMethodProperty", | ||
"_ESAbstract.CreateNonEnumerableDataPropertyOrThrow", | ||
"_ESAbstract.Get", | ||
"_ESAbstract.HasProperty", | ||
"_ESAbstract.Type", | ||
"Object.setPrototypeOf", | ||
] | ||
spec = "https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-installerrorcause" | ||
docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause" | ||
|
||
[browsers] | ||
android = "*" | ||
bb = "*" | ||
chrome = "*" | ||
edge = "*" | ||
edge_mob = "*" | ||
firefox = "*" | ||
firefox_mob = "*" | ||
ie = "*" | ||
ie_mob = "*" | ||
opera = "*" | ||
op_mob = "*" | ||
op_mini = "*" | ||
safari = "*" | ||
ios_saf = "*" | ||
samsung_mob = "*" |
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,63 @@ | ||
/* global CreateMethodProperty, CreateNonEnumerableDataPropertyOrThrow, Get, HasProperty, Type */ | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
var _GetErrorConstructorWithCauseInstalled; | ||
|
||
(function () { | ||
// 20.5.8.1 InstallErrorCause ( O, options ) | ||
function InstallErrorCause(O, options) { | ||
// 1. If options is an Object and ? HasProperty(options, "cause") is true, then | ||
if (Type(options) === 'object' && HasProperty(options, 'cause')) { | ||
// a. Let cause be ? Get(options, "cause"). | ||
var cause = Get(options, 'cause'); | ||
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause). | ||
CreateNonEnumerableDataPropertyOrThrow(O, 'cause', cause); | ||
} | ||
// 2. Return unused. | ||
} | ||
|
||
// based on https://github.com/es-shims/error-cause/blob/de17ea05/Error/implementation.js#L12-L20 | ||
function _makeErrorConstructor (name, _Error) { | ||
var _NativeError = _nativeErrors[name]; | ||
var arity = _Error.length; | ||
return function () { | ||
var O = arity === 2 | ||
? _NativeError.call(null, arguments[0], arguments[1]) | ||
: _NativeError.call(null, arguments[0]); | ||
InstallErrorCause(O, arguments.length > arity && arguments[arity]); | ||
CreateMethodProperty(O, 'constructor', _Error); | ||
return O; | ||
} | ||
} | ||
|
||
// based on https://github.com/es-shims/error-cause/blob/de17ea05/Error/implementation.js#L22-L29 | ||
function _inheritErrorPrototype (name, _Error) { | ||
Object.setPrototypeOf(_Error, self.Error); | ||
_Error.prototype = _nativeErrors[name].prototype; | ||
Object.defineProperty(_Error, 'prototype', { writable: false }); | ||
return _Error; | ||
} | ||
|
||
var _newErrors = { | ||
Error: function Error (_message) { return _errorConstructors.Error.apply(null, arguments); }, | ||
EvalError: function EvalError (_message) { return _errorConstructors.EvalError.apply(null, arguments); }, | ||
RangeError: function RangeError (_message) { return _errorConstructors.RangeError.apply(null, arguments); }, | ||
ReferenceError: function ReferenceError (_message) { return _errorConstructors.ReferenceError.apply(null, arguments); }, | ||
SyntaxError: function SyntaxError (_message) { return _errorConstructors.SyntaxError.apply(null, arguments); }, | ||
TypeError: function TypeError (_message) { return _errorConstructors.TypeError.apply(null, arguments); }, | ||
URIError: function URIError (_message) { return _errorConstructors.URIError.apply(null, arguments); }, | ||
AggregateError: function AggregateError (_errors, _message) { return _errorConstructors.AggregateError.apply(null, arguments); } | ||
}; | ||
|
||
var _nativeErrors = {}; | ||
var _errorConstructors = {}; | ||
|
||
_GetErrorConstructorWithCauseInstalled = function (name) { | ||
_nativeErrors[name] = self[name]; | ||
_errorConstructors[name] = _makeErrorConstructor(name, _newErrors[name]); | ||
if (name !== 'Error') { | ||
_inheritErrorPrototype(name, _newErrors[name]); | ||
} | ||
return _newErrors[name]; | ||
} | ||
})(); |