fancy-errors
Node.js developer's Swiss Army knife for those damned errors.
Features
- Named errors & codes (beauties like
ValidationError
,FileNotFoundError
orHTTPError
) - Error wrapping that helps you catch 'em all!
- Utility functions for catching and logging errors
- Translates
.errno
like 'ENOENT' into more meaningful message - Pretty and colorful error stacks
- sprintf support (via util.format)
- Pre-defined common errors
- and your own custom errors
Install
$ npm install fancy-errors
Basic usage
var errors = ; // Simple errorthrow 'Something went %s wrong' 'terribly'; // HTTPError, throws err.code = 404, err.message = 'Not found'throw 404; // Custom error namethrow 'PaymentGatewayError' 'Error message';
Wrapping
;
will produce this output:
Error: Reading stats failed ← Error: ENOENT, stat '/file/nonexistent' Stack trace: ↳ test.js:36 ...
You can wrap as many errors as you need:
var err1 = 'Low level error';var err2 = err1 'Higher level error';var err3 = err2 'Top level error'; console;
this will produce something like this:
Error: 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
?
try if!url throw 'Invalid URL'; throw 500; catch err iferrname === 'HTTPError' resstatusCode = errcode; res; else iferrname === 'ArgumentError' res;
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.
var err = 'INVALID_USERNAME, Username not found';console; // outputs `INVALID_USERNAME`
Pre-defined errors
AuthenticationError(message)
ArgumentError(message)
ConflictError(message)
ConnectionError(message)
DirectoryNotFoundError(message)
FatalError(message)
FileLoadError(message)
FileNotFoundError(message)
ForbiddenError(message)
HTTPError(statusCode, statusMessage)
IOError(message)
InvalidParameterError(parameterName, message)
MissingParameterError(parameterName, message)
NotAcceptableError(message)
NotAllowedError(message)
NotFoundError(message)
PreconditionError(message)
RangeError(message)
RateLimiterError(message)
ReferenceError(message)
ResourceBusyError(message)
TimeoutError(message)
TooLargeError(message)
TypeError(message)
UnsupportedError(message)
URIError(message)
URITooLongError(message)
ValidationError(message)
Define your own
You can also define your own errors using .define
helper:
errors;throw ;
this will output:
MyFancyError: 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:
Error: ENOENT, stat '/file/nonexistent'
into this beauty:
Error: ENOENT, stat '/file/nonexistent'
.catch
This helper makes it easier to handle error passed into the callback.
This example silently ignores any incoming error:
;
But if you need to wrap this function and properly call the callback with an error:
{ ;}
.catch does something similar to this:
iferr iftypeof callback === 'function' ; else // 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.
if!errors // 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
;
.log does something similar to this:
iferr console; else // 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.
;
will output:
FatalError: Reading data failed ← Error: ENOENT, stat '/file/nonexistent' ...
.statusCode(err)
This helper function returns a HTTP status code for the given error.
errors; // returns 404
.logLevel(err)
Returns a log level ('fatal', 'warning' or 'debug') for pre-defined errors.
errors; // returns 'warning'errors; // returns 'fatal'errors; // returns 'debug'
.serialize()
Returns simple object represenatation of the error usually used for logging or for the server response.
errors; "error": "File /file/nonexistent not found" "errorName": "NotFoundError" "errorCode": 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
$ npm test
License
MIT