errorist
v0.2.0
Published
If you don't have an Error, errorist gives you one
Downloads
19
Maintainers
Readme
errorist
If you don't have an Error, errorist gives you one
Install
$ npm install errorist
Usage
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
Error
return
it.
- If the value is a non-
null
object or function- and its
message
property is astring
return
a newError
with themessage
.
- otherwise
- stringify the object into JSON
- upon success
return
the stringified object as theError
'smessage
.
- otherwise
return
an emptyError
instance.
- and its
- Otherwise
return
anError
instance with the primitive,null
, orundefined
value as its message.
License
© 2015 Christopher Hiller. Licensed MIT.