isError.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var isObjectLike = require('./isObjectLike');
  2. /** `Object#toString` result references. */
  3. var errorTag = '[object Error]';
  4. /** Used for built-in method references. */
  5. var objectProto = Object.prototype;
  6. /**
  7. * Used to resolve the
  8. * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  9. * of values.
  10. */
  11. var objectToString = objectProto.toString;
  12. /**
  13. * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`,
  14. * `SyntaxError`, `TypeError`, or `URIError` object.
  15. *
  16. * @static
  17. * @memberOf _
  18. * @since 3.0.0
  19. * @category Lang
  20. * @param {*} value The value to check.
  21. * @returns {boolean} Returns `true` if `value` is an error object,
  22. * else `false`.
  23. * @example
  24. *
  25. * _.isError(new Error);
  26. * // => true
  27. *
  28. * _.isError(Error);
  29. * // => false
  30. */
  31. function isError(value) {
  32. if (!isObjectLike(value)) {
  33. return false;
  34. }
  35. return (objectToString.call(value) == errorTag) ||
  36. (typeof value.message == 'string' && typeof value.name == 'string');
  37. }
  38. module.exports = isError;