inlight-media/fancy-errors

Name: fancy-errors

Owner: Inlight

Description: Node.js developer's Swiss Army knife for those damned errors.

Created: 2015-11-25 04:38:22.0

Updated: 2017-10-03 14:32:53.0

Pushed: 2015-11-25 22:28:38.0

Homepage: null

Size: 60

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

fancy-errors

Node.js developer's Swiss Army knife for those damned errors.

fancy-errors

Features
Install
m install fancy-errors
Basic usage
errors = require('fancy-errors');

imple error
w new errors.Error('Something went %s wrong', 'terribly');

TTPError, throws err.code = 404, err.message = 'Not found'
w new errors.HTTPError(404);

ustom error name
w new errors.NamedError('PaymentGatewayError', 'Error message');
Wrapping
ire('fs').stat('/file/nonexistent', function(err, stats){
(err){
throw new error.Error(err, 'Reading stats failed');


will produce this output:

r: Reading stats failed
Error: ENOENT, stat '/file/nonexistent' (no such file or directory)

k trace:
test.js:36

You can wrap as many errors as you need:

err1 = new errors.Error('Low level error');
err2 = new errors.Error(err1, 'Higher level error');
err3 = new errors.Error(err2, 'Top level error');

ole.log(err3.stack);

this will produce something like this:

r: Top level error
Error: Higher level error
Error: Low level error

Error names

Is there any easier way how to programmatically handle errors then using err.name?

{
(!url){
throw new error.ArgumentError('Invalid URL');

row new errors.HTTPError(500);

tch (err){
(err.name === 'HTTPError'){
res.statusCode = err.code;
res.end(err.message);

else if(err.name === 'ArgumentError') {
res.end(err.message);


Error codes

The .code property can be populated directly from the error's message if the message begins with capitalized word followed by a comma.

err = new errors.Error('INVALID_USERNAME, Username not found');
ole.log(err.code); // outputs `INVALID_USERNAME`
Pre-defined errors
Define your own

You can also define your own errors using .define helper:

rs.define('MyFancyError', 'This is some fancy default message');
w new errors.MyFancyError();

this will output:

ncyError: This is some fancy default message

What the **** is ENOENT?

You should already know what ENOENT means, but still it is nice to have some meaningful error message. When you wrap an error with .errno property, a human-readable string will be appended to the error's message.

Transforms this ugly beast:

r: ENOENT, stat '/file/nonexistent'

into this beauty:

r: ENOENT, stat '/file/nonexistent' (no such file or directory)
.catch

This helper makes it easier to handle error passed into the callback.

This example silently ignores any incoming error:

Func(function(err, data){
(!errors.catch(err)){
// all good, .catch returns `undefined` if there is no error


But if you need to wrap this function and properly call the callback with an error:

tion readFile(callback){
meFunc(function(err, data){
// .catch will call `callback` with an error if present
if(!errors.catch(err, callback)){
  callback(undefined, data);
}
;

.catch does something similar to this:

rr){
(typeof callback === 'function'){
callback(err);

se {
 all good

.catchName

Similator to .catch but accepts the name of the error as the first argument and will catch ONLY errors that match the name.

errors.catchName('FatalError', err)){
 all other errors except FatalError will not be catched

.log

This helper is in it's way similar to .catch, but instead of ignoring the error or passing it to the callback, it simple outputs the error to stderr

Func(function(err, data){
(!errors.log(err)){
// all good, .log returns `undefined` if there is no error


.log does something similar to this:

rr){
nsole.error(err);
se {
 all good

.fatal

Sometimes it is needed to simply throw an error if it occurs and .fatal will hadle this case for you. It will also wrap the original error with your message.

Func(function(err, data){
rors.fatal(err, 'Reading data failed');

 all good here, .fatal will throw only if there is an error

will output:

lError: Reading data failed
Error: ENOENT, stat '/file/nonexistent' (no such file or directory)

.statusCode(err)

This helper function returns a HTTP status code for the given error.

rs.statusCode(new errors.NotFoundError()); // returns 404
.logLevel(err)

Returns a log level ('fatal', 'warning' or 'debug') for pre-defined errors.

rs.logLevel(new errors.NotFoundError()); // returns 'warning'
rs.logLevel(new errors.FatalError()); // returns 'fatal'
rs.logLevel(new errors.Error()); // returns 'debug'
.serialize()

Returns simple object represenatation of the error usually used for logging or for the server response.

rs.serialize(new errors.NotFoundError('File /file/nonexistent not found'));


rror": "File /file/nonexistent not found",
rrorName": "NotFoundError",
rrorCode": undefined

Pretty stack

Error stack is automaticaly prettified. But this feature is available only in development environment (NODE_ENV). In production, you will get standard stack as you know it.

Test
m test
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.