1.0.1 • Published 7 years ago

@raggedkarma/errors v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

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:

  • BadRequestError for HTTP 400 Bad Request
  • UnauthorizedError for HTTP 401 Unauthorized
  • ForbiddenError for HTTP 403 Forbidden
  • NotFoundError for HTTP 404 Not Found
  • TooManyRequestsError for HTTP 429 Too Many Requests
  • InternalServerError for 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.

1.0.1

7 years ago

1.0.0

7 years ago