es.error.is-error.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var isObject = require('../internals/is-object');
  5. var classof = require('../internals/classof');
  6. var fails = require('../internals/fails');
  7. var ERROR = 'Error';
  8. var DOM_EXCEPTION = 'DOMException';
  9. // eslint-disable-next-line es/no-object-setprototypeof, no-proto -- safe
  10. var PROTOTYPE_SETTING_AVAILABLE = Object.setPrototypeOf || ({}).__proto__;
  11. var DOMException = getBuiltIn(DOM_EXCEPTION);
  12. var $Error = Error;
  13. // eslint-disable-next-line es/no-error-iserror -- safe
  14. var $isError = $Error.isError;
  15. var FORCED = !$isError || !PROTOTYPE_SETTING_AVAILABLE || fails(function () {
  16. // Bun, isNativeError-based implementations, some buggy structuredClone-based implementations, etc.
  17. // https://github.com/oven-sh/bun/issues/15821
  18. return (DOMException && !$isError(new DOMException(DOM_EXCEPTION))) ||
  19. // structuredClone-based implementations
  20. // eslint-disable-next-line es/no-error-cause -- detection
  21. !$isError(new $Error(ERROR, { cause: function () { /* empty */ } })) ||
  22. // instanceof-based and FF Error#stack-based implementations
  23. $isError(getBuiltIn('Object', 'create')($Error.prototype));
  24. });
  25. // `Error.isError` method
  26. // https://tc39.es/ecma262/#sec-error.iserror
  27. $({ target: 'Error', stat: true, sham: true, forced: FORCED }, {
  28. isError: function isError(arg) {
  29. if (!isObject(arg)) return false;
  30. var tag = classof(arg);
  31. return tag === ERROR || tag === DOM_EXCEPTION;
  32. }
  33. });