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.
136 lines
4.2 KiB
136 lines
4.2 KiB
1 month ago
|
function isErrorInstance(obj) {
|
||
|
// eslint-disable-next-line prefer-reflect
|
||
|
return obj instanceof Error || Object.prototype.toString.call(obj) === '[object Error]';
|
||
|
}
|
||
|
|
||
|
function isRegExp(obj) {
|
||
|
// eslint-disable-next-line prefer-reflect
|
||
|
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ### .compatibleInstance(thrown, errorLike)
|
||
|
*
|
||
|
* Checks if two instances are compatible (strict equal).
|
||
|
* Returns false if errorLike is not an instance of Error, because instances
|
||
|
* can only be compatible if they're both error instances.
|
||
|
*
|
||
|
* @name compatibleInstance
|
||
|
* @param {Error} thrown error
|
||
|
* @param {Error|ErrorConstructor} errorLike object to compare against
|
||
|
* @namespace Utils
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function compatibleInstance(thrown, errorLike) {
|
||
|
return isErrorInstance(errorLike) && thrown === errorLike;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ### .compatibleConstructor(thrown, errorLike)
|
||
|
*
|
||
|
* Checks if two constructors are compatible.
|
||
|
* This function can receive either an error constructor or
|
||
|
* an error instance as the `errorLike` argument.
|
||
|
* Constructors are compatible if they're the same or if one is
|
||
|
* an instance of another.
|
||
|
*
|
||
|
* @name compatibleConstructor
|
||
|
* @param {Error} thrown error
|
||
|
* @param {Error|ErrorConstructor} errorLike object to compare against
|
||
|
* @namespace Utils
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function compatibleConstructor(thrown, errorLike) {
|
||
|
if (isErrorInstance(errorLike)) {
|
||
|
// If `errorLike` is an instance of any error we compare their constructors
|
||
|
return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
|
||
|
} else if ((typeof errorLike === 'object' || typeof errorLike === 'function') && errorLike.prototype) {
|
||
|
// If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
|
||
|
return thrown.constructor === errorLike || thrown instanceof errorLike;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ### .compatibleMessage(thrown, errMatcher)
|
||
|
*
|
||
|
* Checks if an error's message is compatible with a matcher (String or RegExp).
|
||
|
* If the message contains the String or passes the RegExp test,
|
||
|
* it is considered compatible.
|
||
|
*
|
||
|
* @name compatibleMessage
|
||
|
* @param {Error} thrown error
|
||
|
* @param {String|RegExp} errMatcher to look for into the message
|
||
|
* @namespace Utils
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function compatibleMessage(thrown, errMatcher) {
|
||
|
const comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
|
||
|
if (isRegExp(errMatcher)) {
|
||
|
return errMatcher.test(comparisonString);
|
||
|
} else if (typeof errMatcher === 'string') {
|
||
|
return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ### .getConstructorName(errorLike)
|
||
|
*
|
||
|
* Gets the constructor name for an Error instance or constructor itself.
|
||
|
*
|
||
|
* @name getConstructorName
|
||
|
* @param {Error|ErrorConstructor} errorLike
|
||
|
* @namespace Utils
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function getConstructorName(errorLike) {
|
||
|
let constructorName = errorLike;
|
||
|
if (isErrorInstance(errorLike)) {
|
||
|
constructorName = errorLike.constructor.name;
|
||
|
} else if (typeof errorLike === 'function') {
|
||
|
// If `err` is not an instance of Error it is an error constructor itself or another function.
|
||
|
// If we've got a common function we get its name, otherwise we may need to create a new instance
|
||
|
// of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
|
||
|
constructorName = errorLike.name;
|
||
|
if (constructorName === '') {
|
||
|
const newConstructorName = (new errorLike().name); // eslint-disable-line new-cap
|
||
|
constructorName = newConstructorName || constructorName;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return constructorName;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ### .getMessage(errorLike)
|
||
|
*
|
||
|
* Gets the error message from an error.
|
||
|
* If `err` is a String itself, we return it.
|
||
|
* If the error has no message, we return an empty string.
|
||
|
*
|
||
|
* @name getMessage
|
||
|
* @param {Error|String} errorLike
|
||
|
* @namespace Utils
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function getMessage(errorLike) {
|
||
|
let msg = '';
|
||
|
if (errorLike && errorLike.message) {
|
||
|
msg = errorLike.message;
|
||
|
} else if (typeof errorLike === 'string') {
|
||
|
msg = errorLike;
|
||
|
}
|
||
|
|
||
|
return msg;
|
||
|
}
|
||
|
|
||
|
export { compatibleInstance, compatibleConstructor, compatibleMessage, getMessage, getConstructorName };
|