npm install cube-errorcube-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:
``js
`
var CustomErrors = require("cube-error");
throw new CustomErrors.NotFound("Hello");
`
The real power, however, comes with matching an error-type:
js
`
somethingThatFails(function(error) {
if(error && error.is(CustomErrors.NotFound)) {
//do something
return;
}
if(error) {
//do else
return;
}
//success
});
NotFound(message, previousError)
$3
We currently support these errors:
- :
HttpError(statusCode, message, previousError)
Used when a resource could not be located.
- :
404
Maps common status codes to correct errors.
maps to NotFound.
HttpError
Otherwise returns a with a statusCode field.
Conflict(message, previousError)
- :
MissingArgument(message, previousError)
Used when a resource modification conflicts with existing internal state.
- :
InvalidArgument(invalidArgumentName, message, previousError)
Can be used when arguments were expected but not passed.
- :
TypeError
Can be used as a custom to indicate unexpected input-type or value
invalidArgument
The error has a -field which stores the value of invalidArgumentName
Unauthorized(message, previousError)
- :
Internal(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.
- :
Custom
Used when an error happens, which can't be recognized as any other error type.
should never be instantiated, but is intended to be inherited from.
Custom
$3
You can build a custom error by inheriting from :
`
js
`
var CustomError = require("cube-error").Custom;
function MyError(message, previousError) {
CustomError.call(this, message, previousError);
//something custom?
}
util.inherits(MyError, CustomError);
is(MyError)
This error will come prepackaged with , stack, message, and previousError fields.
400
TODO
----
- Map HttpError to more errors in specific cases.
Some ideas are:
- to BadRequestError
403
- to ForbiddenError
500
- to InternalServerError
var MyError = function() {}; util.inherits(MyError, CustomError);
- 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. ), and no this.constructor.name` exists.