1.1.0 • Published 8 years ago
cube-error v1.1.0
cube-error
This library adds some standardization to errors, and provides an easy way of building new error types.
Installation
npm install cube-error --save
Usage
It is simple to require in the library and throw a standardized error:
var CustomErrors = require("cube-error");
throw new CustomErrors.NotFound("Hello");
The real power, however, comes with matching an error-type:
somethingThatFails(function(error) {
if(error && error.is(CustomErrors.NotFound)) {
//do something
return;
}
if(error) {
//do else
return;
}
//success
});
Supported errors
We currently support these errors:
NotFound(message, previousError)
: Used when a resource could not be located.HttpError(statusCode, message, previousError)
: Maps common status codes to correct errors.404
maps toNotFound
. Otherwise returns aHttpError
with astatusCode
field.Conflict(message, previousError)
: Used when a resource modification conflicts with existing internal state.MissingArgument(message, previousError)
: Can be used when arguments were expected but not passed.InvalidArgument(invalidArgumentName, message, previousError)
: Can be used as a customTypeError
to indicate unexpected input-type or value The error has ainvalidArgument
-field which stores the value ofinvalidArgumentName
Unauthorized(message, previousError)
: Can be used to indicate that authorization has failed. A message can be included to provide a reason as to what will happen as a consequence.Internal(message, previousError)
: Used when an error happens, which can't be recognized as any other error type.
Custom
should never be instantiated, but is intended to be inherited from.
Building custom errors
You can build a custom error by inheriting from Custom
:
var CustomError = require("cube-error").Custom;
function MyError(message, previousError) {
CustomError.call(this, message, previousError);
//something custom?
}
util.inherits(MyError, CustomError);
This error will come prepackaged with is(MyError)
, stack
, message
, and previousError
fields.
TODO
- Map HttpError to more errors in specific cases.
Some ideas are:
400
toBadRequestError
403
toForbiddenError
500
toInternalServerError
- Make HttpError alternate classes always have a statusCode anyway (to reduce breakingness of changes when introducing new mappings).
- Test CustomError extension as a thing in unit tests.
- Handle the case where CustomError is subclasses wrongly (eg.
var MyError = function() {}; util.inherits(MyError, CustomError);
), and nothis.constructor.name
exists.