Error comparison and information related utility for node and the browser.
Check-Error is a module which you can use to retrieve an Error's information such as its message or constructor name and also to check whether two Errors are compatible based on their messages, constructors or even instances.
check-error is available on npm. To install it, type:
$ npm install check-error You can also use it within the browser; install via npm and use the check-error.js file found within the download. For example:
<script src="./node_modules/check-error/check-error.js"></script>The primary export of check-error is an object which has the following methods:
compatibleInstance(err, errorLike)- Checks if an error is compatible with anothererrorLikeobject. IferrorLikeis an error instance we do a strict comparison, otherwise we returnfalseby default, because instances of objects can only be compatible if they're both error instances.compatibleConstructor(err, errorLike)- Checks if an error's constructor is compatible with anothererrorLikeobject. Iferrhas the same constructor aserrorLikeor iferris an instance oferrorLike.compatibleMessage(err, errMatcher)- Checks if an error message is compatible with anerrMatcherRegExp or String (we check if the message contains the String).getConstructorName(errorLike)- Retrieves the name of a constructor, an error's constructor orerrorLikeitself if it's not an error instance or constructor.getMessage(err)- Retrieves the message of an error orerritself if it's a String. Iferrorerr.messageis undefined we return an empty String.
var checkError = require('check-error');var checkError = require('check-error'); var funcThatThrows = function() { throw new TypeError('I am a TypeError') }; var caughtErr; try { funcThatThrows(); } catch(e) { caughtErr = e; } var sameInstance = caughtErr; checkError.compatibleInstance(caughtErr, sameInstance); // true checkError.compatibleInstance(caughtErr, new TypeError('Another error')); // falsevar checkError = require('check-error'); var funcThatThrows = function() { throw new TypeError('I am a TypeError') }; var caughtErr; try { funcThatThrows(); } catch(e) { caughtErr = e; } checkError.compatibleConstructor(caughtErr, Error); // true checkError.compatibleConstructor(caughtErr, TypeError); // true checkError.compatibleConstructor(caughtErr, RangeError); // falsevar checkError = require('check-error'); var funcThatThrows = function() { throw new TypeError('I am a TypeError') }; var caughtErr; try { funcThatThrows(); } catch(e) { caughtErr = e; } var sameInstance = caughtErr; checkError.compatibleMessage(caughtErr, /TypeError$/); // true checkError.compatibleMessage(caughtErr, 'I am a'); // true checkError.compatibleMessage(caughtErr, /unicorn/); // false checkError.compatibleMessage(caughtErr, 'I do not exist'); // falsevar checkError = require('check-error'); var funcThatThrows = function() { throw new TypeError('I am a TypeError') }; var caughtErr; try { funcThatThrows(); } catch(e) { caughtErr = e; } var sameInstance = caughtErr; checkError.getConstructorName(caughtErr) // 'TypeError'var checkError = require('check-error'); var funcThatThrows = function() { throw new TypeError('I am a TypeError') }; var caughtErr; try { funcThatThrows(); } catch(e) { caughtErr = e; } var sameInstance = caughtErr; checkError.getMessage(caughtErr) // 'I am a TypeError'