1+ function isErrorInstance ( obj ) {
2+ // eslint-disable-next-line prefer-reflect
3+ return obj instanceof Error || Object . prototype . toString . call ( obj ) === '[object Error]' ;
4+ }
5+
6+ function isErrorClass ( obj ) {
7+ return obj === Error || ( typeof obj === 'function' && obj . name === 'Error' ) ;
8+ }
9+
10+ function isRegExp ( obj ) {
11+ // eslint-disable-next-line prefer-reflect
12+ return Object . prototype . toString . call ( obj ) === '[object RegExp]' ;
13+ }
14+
115/**
216 * ### .compatibleInstance(thrown, errorLike)
317 *
1327 */
1428
1529function compatibleInstance ( thrown , errorLike ) {
16- return errorLike instanceof Error && thrown === errorLike ;
30+ return isErrorInstance ( errorLike ) && thrown === errorLike ;
1731}
1832
1933/**
@@ -33,10 +47,10 @@ function compatibleInstance(thrown, errorLike) {
3347 */
3448
3549function compatibleConstructor ( thrown , errorLike ) {
36- if ( errorLike instanceof Error ) {
50+ if ( isErrorInstance ( errorLike ) ) {
3751 // If `errorLike` is an instance of any error we compare their constructors
3852 return thrown . constructor === errorLike . constructor || thrown instanceof errorLike . constructor ;
39- } else if ( errorLike . prototype instanceof Error || errorLike === Error ) {
53+ } else if ( isErrorClass ( Object . getPrototypeOf ( errorLike ) ) || isErrorClass ( errorLike ) ) {
4054 // If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
4155 return thrown . constructor === errorLike || thrown instanceof errorLike ;
4256 }
@@ -60,7 +74,7 @@ function compatibleConstructor(thrown, errorLike) {
6074
6175function compatibleMessage ( thrown , errMatcher ) {
6276 const comparisonString = typeof thrown === 'string' ? thrown : thrown . message ;
63- if ( errMatcher instanceof RegExp ) {
77+ if ( isRegExp ( errMatcher ) ) {
6478 return errMatcher . test ( comparisonString ) ;
6579 } else if ( typeof errMatcher === 'string' ) {
6680 return comparisonString . indexOf ( errMatcher ) !== - 1 ; // eslint-disable-line no-magic-numbers
@@ -82,7 +96,7 @@ function compatibleMessage(thrown, errMatcher) {
8296
8397function getConstructorName ( errorLike ) {
8498 let constructorName = errorLike ;
85- if ( errorLike instanceof Error ) {
99+ if ( isErrorInstance ( errorLike ) ) {
86100 constructorName = errorLike . constructor . name ;
87101 } else if ( typeof errorLike === 'function' ) {
88102 // If `err` is not an instance of Error it is an error constructor itself or another function.
0 commit comments