Simple Express HTTP error response middleware.
- Simple API error responses.
This package is available on NPM:
$ yarn add allanchau-http-error
A simple example:
// App.
const app = require('express');
const httpError = require('allanchau-http-error');
app.use(httpError());
app.get('/stringexample', (req, res, next) => {
return res.error('Oops something went wrong.');
});
// Returns
// {
// "code": "400",
// "message": "Oops something went wrong.",
// }
app.get('/errorobjectexample', (req, res, next) => {
return res.error(new Error('Oops something went wrong.'));
});
// Returns the same thing
// {
// "code": "400",
// "message": "Oops something went wrong.",
// }
app.get('/customerrorexample', (req, res, next) => {
return res.error({
code: 'ERRBADREQUEST',
message: 'Oops something went wrong.',
});
});
// Returns
// {
// "code": "ERRBADREQUEST",
// "message": "Oops something went wrong.",
// }