https://github.com/internap/base-http-errors
https://github.com/internap/base-http-errors
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/internap/base-http-errors
- Owner: internap
- Created: 2017-03-30T14:56:23.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-20T17:24:49.000Z (about 9 years ago)
- Last Synced: 2025-08-27T16:36:47.447Z (10 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 8
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Base HTTP Errors
A set of basic HTTP errors that can be used in Express.js
## Usage
```javascript
const {
NotFound,
BadRequest
} = require('base-http-errors');
app.use((req, res, next) => {
if (routeDoesntExist()) {
next(new NotFound());
}
if (somethingWrongWithRequest()) {
next(new BadRequest('A Custom Message'));
}
});
// error handler
app.use((err, req, res, next) => {
res.status(err.status);
res.json({
message: err.message,
type: err.constructor.name,
});
});
```
## Available Errors
| Error Class | Default Message | Status Code |
| ----------------- | ------------------ | ------------ |
| BaseError | Base Error | 500 |
| TooManyRequests | Too Many Requests | 429 |
| NotFound | Not Found | 404 |
| Unauthorized | Unauthorized | 401 |
| Forbidden | Forbidden | 403 |
| BadRequest | Bad Request | 400 |
| InvalidInput | Invalid Input | 400 |
## Custom Errors
You may subclass any of the above listed errors to create your own error.
E.g.
```javascript
class CustomError extends BaseError {
constructor(message) {
super(message || 'Custom Error');
this.status = 400;
}
}
```