123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 'use strict';
- var getFunctionName = require('get-func-name');
- function compatibleInstance(thrown, errorLike) {
- return errorLike instanceof Error && thrown === errorLike;
- }
- function compatibleConstructor(thrown, errorLike) {
- if (errorLike instanceof Error) {
-
- return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
- } else if (errorLike.prototype instanceof Error || errorLike === Error) {
-
- return thrown.constructor === errorLike || thrown instanceof errorLike;
- }
- return false;
- }
- function compatibleMessage(thrown, errMatcher) {
- var comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
- if (errMatcher instanceof RegExp) {
- return errMatcher.test(comparisonString);
- } else if (typeof errMatcher === 'string') {
- return comparisonString.indexOf(errMatcher) !== -1;
- }
- return false;
- }
- function getConstructorName(errorLike) {
- var constructorName = errorLike;
- if (errorLike instanceof Error) {
- constructorName = getFunctionName(errorLike.constructor);
- } else if (typeof errorLike === 'function') {
-
-
-
- constructorName = getFunctionName(errorLike);
- if (constructorName === '') {
- var newConstructorName = getFunctionName(new errorLike());
- constructorName = newConstructorName || constructorName;
- }
- }
- return constructorName;
- }
- function getMessage(errorLike) {
- var msg = '';
- if (errorLike && errorLike.message) {
- msg = errorLike.message;
- } else if (typeof errorLike === 'string') {
- msg = errorLike;
- }
- return msg;
- }
- module.exports = {
- compatibleInstance: compatibleInstance,
- compatibleConstructor: compatibleConstructor,
- compatibleMessage: compatibleMessage,
- getMessage: getMessage,
- getConstructorName: getConstructorName,
- };
|