1.2.0 • Published 9 years ago
@kwo/http-errors v1.2.0
HTTP Errors
ES6 Errors for HTTP Status Codes
Usage
An example using ExpressJS.
import {HttpError, MethodNotAllowed} from '@kwo/http-errors';
// only allow POST requests
router.use((req, res, next) => {
if (req && req.method && req.method === 'POST')
return next();
next(new MethodNotAllowed());
});
...
// error handler
router.use((err, req, rsp, next) => {
const CT_TEXT = 'text/plain; charset=utf-8';
if (err) {
if (err instanceof HttpError) {
rsp.status(err.status).type(CT_TEXT).send(err.message);
} else {
next(err);
}
} else {
next();
}
});
1.2.0
9 years ago