From 696feb81c016e313f5920f69c0507161c350cd6b Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Thu, 26 Dec 2024 00:13:25 +0700 Subject: [PATCH] add `Error.isError` --- packages/core-js-compat/src/data.mjs | 2 ++ .../src/modules-by-versions.mjs | 3 ++ packages/core-js/actual/error/index.js | 2 ++ packages/core-js/actual/error/is-error.js | 6 ++++ packages/core-js/full/error/is-error.js | 4 +++ .../core-js/modules/esnext.error.is-error.js | 36 +++++++++++++++++++ packages/core-js/proposals/is-error.js | 3 ++ packages/core-js/stage/3.js | 1 + 8 files changed, 57 insertions(+) create mode 100644 packages/core-js/actual/error/is-error.js create mode 100644 packages/core-js/full/error/is-error.js create mode 100644 packages/core-js/modules/esnext.error.is-error.js create mode 100644 packages/core-js/proposals/is-error.js diff --git a/packages/core-js-compat/src/data.mjs b/packages/core-js-compat/src/data.mjs index a145e799ee6b..a1bc3fd5efd5 100644 --- a/packages/core-js-compat/src/data.mjs +++ b/packages/core-js-compat/src/data.mjs @@ -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': { diff --git a/packages/core-js-compat/src/modules-by-versions.mjs b/packages/core-js-compat/src/modules-by-versions.mjs index 12dd18e2d15b..cdccfbe34e7f 100644 --- a/packages/core-js-compat/src/modules-by-versions.mjs +++ b/packages/core-js-compat/src/modules-by-versions.mjs @@ -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', + ], }; diff --git a/packages/core-js/actual/error/index.js b/packages/core-js/actual/error/index.js index 7d3ad1c9a62f..2c0da9fac71e 100644 --- a/packages/core-js/actual/error/index.js +++ b/packages/core-js/actual/error/index.js @@ -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; diff --git a/packages/core-js/actual/error/is-error.js b/packages/core-js/actual/error/is-error.js new file mode 100644 index 000000000000..623bca15bac4 --- /dev/null +++ b/packages/core-js/actual/error/is-error.js @@ -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; diff --git a/packages/core-js/full/error/is-error.js b/packages/core-js/full/error/is-error.js new file mode 100644 index 000000000000..1d2e20530fef --- /dev/null +++ b/packages/core-js/full/error/is-error.js @@ -0,0 +1,4 @@ +'use strict'; +var parent = require('../../actual/error/is-error'); + +module.exports = parent; diff --git a/packages/core-js/modules/esnext.error.is-error.js b/packages/core-js/modules/esnext.error.is-error.js new file mode 100644 index 000000000000..6d4f6a4813f1 --- /dev/null +++ b/packages/core-js/modules/esnext.error.is-error.js @@ -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; + } +}); diff --git a/packages/core-js/proposals/is-error.js b/packages/core-js/proposals/is-error.js new file mode 100644 index 000000000000..f15865884044 --- /dev/null +++ b/packages/core-js/proposals/is-error.js @@ -0,0 +1,3 @@ +'use strict'; +// https://github.com/tc39/proposal-is-error +require('../modules/esnext.error.is-error'); diff --git a/packages/core-js/stage/3.js b/packages/core-js/stage/3.js index 0b381e2cda30..3ea2ec129391 100644 --- a/packages/core-js/stage/3.js +++ b/packages/core-js/stage/3.js @@ -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');