/**
* 检查`value`是否是`Error`,`EvalError`,`RangeError`,`ReferenceError`,`SyntaxError`,`TypeError`, `URIError`对象
* Checks if `value` is an `Error`,`EvalError`,`RangeError`,`ReferenceError`,`SyntaxError`,`TypeError`, `URIError`object
*
* @param {*} value The value to check
* @returns {boolean} Returns `true` if `value` is an error object, else `false`
* @example
* isError(new Error)
* // => true
* isError(Error)
* // => false
*/
const errorTag = "[object Error]"
const domExcTag = "[object DOMException]"
const objectTag = "[object Object]"
function isObjectLike(value) {
return typeof value == "object" && value !== null;
}
function isPlainObject(value) {
if (
!isObjectLike(value) ||
Object.prototype.toString.call(value) !== objectTag
) {
return false
}
const proto = Object.getPrototypeOf(value)
if (proto === null) {
return true
}
const Ctor =
Object.prototype.hasOwnProperty.call(proto, "constructor") &&
proto.constructor
return (
typeof Ctor === "function" &&
Ctor instanceof Ctor &&
Function.prototype.toString.call(Ctor) ===
Function.prototype.toString.call(Object)
)
}
function isError(value) {
if (!isObjectLike(value)) {
return false
}
const tag = Object.prototype.toString.call(value)
return (
tag === errorTag ||
tag === domExcTag ||
(typeof value.message === "string" &&
typeof value.message === "string" &&
!isPlainObject(value))
)
}
export default isError