Error-Ninja
Backend error handling can be a pain, it's tempting to just pass strings to callbacks so it's easier to handle, but using a proper Error object is a better way and Error Ninja can help with this.
What's the Point?
- Outputs a human-readable error message in the console.
- Lets you set an error ID to make it easy to create and identify the error.
- Makes it easier to debug errors by outputting additional data.
- Makes it easier to handle errors by adding extra properties to the error.
- Throws just like a plain old JavaScript Error.
- Allows you to detect if a variable is a JavaScript error or an ErrorNinja error.
Define Your Error Messages
First, define a hash of error IDs and error messages.
const ErrorNinja = require('error-ninja').define({
INVALID_BLOG_ID: 'The given blog ID does not exist!',
ATTACHMENT_TOO_BIG: 'You cannot upload an attachment that big!',
SOME_ERROR: 'Woah, this code is buggy!',
});
Create and Throw Errors
Now you can create an error. You can throw this error just like any other.
const err = new ErrorNinja('INVALID_BLOG_ID');
throw err;
Include Data in the Error
If you need to include some extra properties in the error you can specify the second parameter. By default this data will be output to the console if the error is thrown, and you'll also be able to access it when handling your error.
const err = new ErrorNinja('ATTACHMENT_TOO_BIG', { fileSize: 17483, maxSize: 1024, name: 'crash.log' });
throw err;
Turn Off Data Console Output
To prevent the extra error data being output in the console you can do one of two things:
- Pass false as the 3rd argument when creating an error:
const err = new ErrorNinja('SOME_ERROR', { abc: 'do-not-output' }, false);
- Or you can turn off data output for all errors created by this instance of Error Ninja:
const ErrorNinja = require('error-ninja').define({ ... }, { outputData: false });
Access Useful Properties in the Error
Apart from the usual error properties you can also access the following additional properties that should help with handling your errors.
const err = new ErrorNinja('ATTACHMENT_TOO_BIG', { fileSize: 17483, maxSize: 1024, name: 'crash.log' });
err.id;
err.isError;
err.isNinja;
err.human;
err.data;
Convert an existing error to an ErrorNinja
You can convert ordinary errors created by the Node core or other modules to ErrorNinja errors without throwing them:
doSomethingAsync((err, result) => {
err = ErrorNinja.convert(err, 'MY_ERROR_ID');
ErrorNinja.isNinja(err);
});
Or you can convert and throw them immediately. The ErrorNinja.throw()
function returns a converter function which will
convert the err
passed to the catch block.
doSomethingAsync()
.then(result => {
})
.catch(err => ErrorNinja.throw('MY_ERROR_ID'));
How to Tell if a Variable is an Error?
ErrorNinja provides two methods to detect if a variable is an error, or more specifically an ErrorNinja error.
const err1 = new Error();
if (ErrorNinja.isError(err1)) { ... }
const err2 = new ErrorNinja('...');
if (ErrorNinja.isNinja(err2)) { ... }