You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

is.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. var isPrototype = require("../prototype/is")
  3. , isPlainObject = require("../plain-object/is");
  4. var objectToString = Object.prototype.toString;
  5. // Recognize host specific errors (e.g. DOMException)
  6. var errorTaggedStringRe = /^\[object .*(?:Error|Exception)\]$/
  7. , errorNameRe = /^[^\s]*(?:Error|Exception)$/;
  8. module.exports = function (value) {
  9. if (!value) return false;
  10. var name;
  11. // Sanity check (reject objects which do not expose common Error interface)
  12. try {
  13. name = value.name;
  14. if (typeof name !== "string") return false;
  15. if (typeof value.message !== "string") return false;
  16. } catch (error) {
  17. return false;
  18. }
  19. // Ensure its a native-like Error object
  20. // (has [[ErrorData]] slot, or was created to resemble one)
  21. // Note: It's not a 100% bulletproof check of confirming that as:
  22. // - In ES2015+ string tag can be overriden via Symbol.toStringTag property
  23. // - Host errors do not share native error tag. Still we rely on assumption that
  24. // tag for each error will end either with `Error` or `Exception` string
  25. // - In pre ES2015 era, no custom errors will share the error tag.
  26. if (!errorTaggedStringRe.test(objectToString.call(value))) {
  27. // Definitely not an ES2015 error instance, but could still be an error
  28. // (created via e.g. CustomError.prototype = Object.create(Error.prototype))
  29. try {
  30. if (name !== value.constructor.name) return false;
  31. } catch (error) {
  32. return false;
  33. }
  34. if (!errorNameRe.test(name)) return false;
  35. if (isPlainObject(value)) return false;
  36. }
  37. return !isPrototype(value);
  38. };