esnext.error.is-error.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  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. var $isError = $Error.isError;
  14. var FORCED = !$isError || !PROTOTYPE_SETTING_AVAILABLE || fails(function () {
  15. // Bun, isNativeError-based implementations, some buggy structuredClone-based implementations, etc.
  16. // https://github.com/oven-sh/bun/issues/15821
  17. return (DOMException && !$isError(new DOMException(DOM_EXCEPTION))) ||
  18. // structuredClone-based implementations
  19. // eslint-disable-next-line es/no-error-cause -- detection
  20. !$isError(new $Error(ERROR, { cause: function () { /* empty */ } })) ||
  21. // instanceof-based and FF Error#stack-based implementations
  22. $isError(getBuiltIn('Object', 'create')($Error.prototype));
  23. });
  24. // `Error.isError` method
  25. // https://github.com/tc39/proposal-is-error
  26. $({ target: 'Error', stat: true, sham: true, forced: FORCED }, {
  27. isError: function isError(arg) {
  28. if (!isObject(arg)) return false;
  29. var tag = classof(arg);
  30. return tag === ERROR || tag === DOM_EXCEPTION;
  31. }
  32. });