https://github.com/allanchau/node-http-error
Simple Express HTTP error handler.
https://github.com/allanchau/node-http-error
error express http node
Last synced: 2 months ago
JSON representation
Simple Express HTTP error handler.
- Host: GitHub
- URL: https://github.com/allanchau/node-http-error
- Owner: allanchau
- License: mit
- Created: 2017-03-15T08:53:45.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T23:21:51.000Z (over 3 years ago)
- Last Synced: 2025-10-30T12:25:35.596Z (8 months ago)
- Topics: error, express, http, node
- Language: JavaScript
- Size: 719 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# node-http-error
[](https://www.npmjs.com/package/allanchau-http-error)



Simple Express HTTP error response middleware.
## Features
- Simple API error responses.
## Installation
This package is available on [NPM](https://www.npmjs.com/package/allanchau-http-error):
```shell
$ yarn add allanchau-http-error
```
## Usage
A simple example:
```javascript
// 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.",
// }
```
You can also provide some defaults for the route.
```js
app.use(
httpError({
defaultCode: "ERRNOTAUTHORIZED",
defaultMessage: "The user is not authorized.",
defaultStatusCode: 401,
})
);
app.get("/stringexample", (req, res, next) => {
return res.error();
});
// Returns
// {
// "code": "ERRNOTAUTHORIZED",
// "message": "The user is not authorized.",
// }
```