Skip to content

Commit

Permalink
add Error.isError
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 25, 2024
1 parent 97df970 commit 696feb8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,8 @@ export const data = {
// reverted in https://issues.chromium.org/issues/42203506#comment25
// chrome: '133',
},
'esnext.error.is-error': {
},
'esnext.function.demethodize': {
},
'esnext.function.is-callable': {
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,7 @@ export default {
'esnext.weak-map.get-or-insert',
'esnext.weak-map.get-or-insert-computed',
],
'3.40': [
'esnext.error.is-error',
],
};
2 changes: 2 additions & 0 deletions packages/core-js/actual/error/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
var parent = require('../../stable/error');
require('../../modules/es.object.create');
require('../../modules/esnext.error.is-error');

module.exports = parent;
6 changes: 6 additions & 0 deletions packages/core-js/actual/error/is-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
require('../../modules/es.object.create');
require('../../modules/esnext.error.is-error');
var path = require('../../internals/path');

module.exports = path.Error.isError;
4 changes: 4 additions & 0 deletions packages/core-js/full/error/is-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
var parent = require('../../actual/error/is-error');

module.exports = parent;
36 changes: 36 additions & 0 deletions packages/core-js/modules/esnext.error.is-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var isObject = require('../internals/is-object');
var classof = require('../internals/classof');
var fails = require('../internals/fails');

var ERROR = 'Error';
var DOM_EXCEPTION = 'DOMException';
// eslint-disable-next-line es/no-object-setprototypeof, no-proto -- safe
var PROTOTYPE_SETTING_AVAILABLE = Object.setPrototypeOf || ({}).__proto__;

var DOMException = getBuiltIn(DOM_EXCEPTION);
var $Error = Error;
var $isError = $Error.isError;

var FORCED = !$isError || !PROTOTYPE_SETTING_AVAILABLE || fails(function () {
// Bun, isNativeError-based implementations, some buggy structuredClone-based implementations, etc.
// https://github.com/oven-sh/bun/issues/15821
return (DOMException && !$isError(new DOMException(DOM_EXCEPTION))) ||
// structuredClone-based implementations
// eslint-disable-next-line es/no-error-cause -- detection
!$isError(new $Error(ERROR, { cause: function () { /* empty */ } })) ||
// instanceof-based and FF Error#stack-based implementations
$isError(getBuiltIn('Object', 'create')($Error.prototype));
});

// `Error.isError` method
// https://github.com/tc39/proposal-is-error
$({ target: 'Error', stat: true, sham: true, forced: FORCED }, {
isError: function isError(arg) {
if (!isObject(arg)) return false;
var tag = classof(arg);
return tag === ERROR || tag === DOM_EXCEPTION;
}
});
3 changes: 3 additions & 0 deletions packages/core-js/proposals/is-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
// https://github.com/tc39/proposal-is-error
require('../modules/esnext.error.is-error');
1 change: 1 addition & 0 deletions packages/core-js/stage/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require('../proposals/array-from-async-stage-2');
require('../proposals/decorator-metadata-v2');
require('../proposals/explicit-resource-management');
require('../proposals/float16');
require('../proposals/is-error');
require('../proposals/json-parse-with-source');
require('../proposals/math-sum');
require('../proposals/regexp-escaping');
Expand Down

0 comments on commit 696feb8

Please sign in to comment.