1.0.1 • Published 9 years ago
@raggedkarma/errors v1.0.1
An error set for easy HTTP error responses.
This is an extension of super-error. Every error defined in this package behaves as presented in that package.
Use the following errors to wrap additional HTTP response information to an error:
BadRequestErrorfor HTTP 400 Bad RequestUnauthorizedErrorfor HTTP 401 UnauthorizedForbiddenErrorfor HTTP 403 ForbiddenNotFoundErrorfor HTTP 404 Not FoundTooManyRequestsErrorfor HTTP 429 Too Many RequestsInternalServerErrorfor HTTP 500 Internal Server Error
All of these errors accept two arguments: message, and details.
Error details are expected to be an array of objects, each detailing an error. This is to allow for bulk response, such as in the case of validation errors.
Example
const errors = require("@raggedkarma/errors");
...
.catch(e => {
var err new errors.BadRequestError("Validation Errors", [{"message": "Name is required", "field": "name"}, ...]).causedBy(e);
});
...
// Express
res.status(err.code);
if (!err.message) return res.send();
res.json({
"errors": err.details
});The package also presents a knows static function, so you can quickly verify if an error response has been previously wrapped by an error defined by this package.
Testing
Use npm install && npm test to run all tests, and npm coverage to generate a full coverage report.