Common HTTP errors to be used with express.js or whatever engine you use.
npm install hataCommon HTTP errors to be used with express.js or whatever engine you use.
The following error types are currently supported:
- (400) bad-request: BadRequestError
- (401) unauthorized: UnauthorizedError
- (403) forbidden: ForbiddenError
- (404) not-found: NotFoundError
- (409) conflict: ConflictError
- (422) unprocessable-entity: UnprocessableEntityError
The error object is an instance of Error, with the properties name, message and stack.
``javascript
var NotFoundError = require('hata/not-found');
throw new NotFoundError('Product not found');
`
You can also add more properties to the error object by passing them as the second parameter.
`javascript
var NotFoundError = require('hata/not-found');
var error = new NotFoundError('Product not found', { item: 'product', id: 1, });
// error now has attributes item and id.`
You can also use it in the following way:
`javascript`
var hata = require('hata');
var error = hata(404/, message, obj/);
javascript
// http://expressjs.com/guide/error-handling.html
app.use(function(err, req, res, next) { // jshint ignore:line
return res
.status(err.httpCode || 500)
.header('content-type: application/json')
.send(safeJsonStringify(err));
});
``