https://github.com/markusand/errors-express
Central error handling for Express
https://github.com/markusand/errors-express
Last synced: 11 months ago
JSON representation
Central error handling for Express
- Host: GitHub
- URL: https://github.com/markusand/errors-express
- Owner: markusand
- Created: 2022-01-13T22:31:10.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-31T18:57:47.000Z (about 2 years ago)
- Last Synced: 2025-02-27T05:47:46.562Z (over 1 year ago)
- Language: TypeScript
- Size: 380 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Errors for Express
Easy error handling for Express APIs.
[](https://npmjs.org/package/errors-express)
[](https://npmjs.org/package/errors-express)
[](https://npmjs.org/package/errors-express)
## Installation
Install together with express
```bash
npm i express errors-express
```
## Usage
Import the error handler middleware and place it at the end of the middleware stack. Any error (operational or not) thrown by a controller is handled and sent to the client.
Use the handler with a callback to perform extra actions such as logging. Callback receives the error and the request object.
Use guards to return a custom error response after not found or not allowed methods requests.
```js
import express from 'express';
import errorHandler, { Errors, Guards } from 'errors-express';
const app = express();
app.get('/protected', async (req, res) => {
throw Errors.Unauthorized('You must first sign in');
});
app.all('*', Guards.NotFound());
app.use(errorHandler((error, req) => {
console.log(`[${req.method} ${req.url}] ${error.message}`);
}));
app.listen(process.env.PORT || 3000);
```
## Errors
The base error class used by the package is HttpError. Send optional error details, useful for providing context to error handling in frontend.
`HttpError(statusCode, message, details?)`
Details can be a string or an object that contains an error code, message and a free context object
Default errors are provided by the package, just include optional message and details.
```javascript
import { HttpError, Errors } from 'errors-express';
throw new HttpError(400, 'Invalid request', 'MISSING_PSWD');
throw Errors.NotFound();
throw Errors.Forbidden('You cannot do this');
throw Errors.TooManyRequests('You reached the maximum limit or requests', {
code: 'REQUEST_LIMIT_REACHED';
message: 'You reached the maximum limit or requests';
ctx: {
maxRequests: 5,
retryIn: '1min',
};
});
```
| Error | statusCode | Default message |
| --- | --- | --- |
| **BadRequest** | 400 | The request syntax is invalid |
| **Unauthorized** | 401 | The authentication credentials are invalid |
| **Forbidden** | 403 | You are not allowed to use this resource |
| **NotFound** | 404 | This resource does not exist |
| **MethodNotAllowed** | 405 | This method is not allowed for this resource |
| **Conflict** | 409 | There is a conflict with the current state of the resource |
| **Unprocessable** | 422 | The request is unprocessable |
| **TooManyRequests** | 429 | The maximum number of requests has been exceeded |
| **InternalServer** | 500 | An internal server error occurred |
## Guards
Guards automatically return an error if none of the previous handlers are called.
```javascript
import { Guards } from 'errors-express';
app.get('/resource', ResourceController);
app.use(Guards.MethodNotAllowed());
app.use(Guards.NotFound()),
```
Only **MethodNotAllowed** and **NotFound** are available.