0.2.0 • Published 9 years ago
errorist v0.2.0
errorist

If you don't have an Error, errorist gives you one
Install
$ npm install erroristUsage
var errorist = require('errorist');
function asyncFunc(callback) {
process.nextTick(function() {
callback('this is not an Error; it is a string');
});
}
asyncFunc(function(err) {
if (err) {
// you would never do a thing like throw a string, right?
throw errorist(err);
}
});
// or with promises
require('bluebird').promisify(asyncFunc)()
.catch(err) {
// stack trace FTW
throw errorist(err);
});Notes
errorist's flow is a bit like this:
- If the value is an
Errorreturnit.
- If the value is a non-
nullobject or function- and its
messageproperty is astringreturna newErrorwith themessage.
- otherwise
- stringify the object into JSON
- upon success
returnthe stringified object as theError'smessage.
- otherwise
returnan emptyErrorinstance.
- and its
Otherwise
returnanErrorinstance with the primitive,null, orundefinedvalue as its message.
License
© 2015 Christopher Hiller. Licensed MIT.