@vavra7/errors
v1.1.1
Published
Errors handling for Express application containing a set of predefined errors.
Downloads
2
Readme
Errors
Errors handling for Express application containing a set of predefined errors.
Preconditions
Express application
Installation
npm install @vavra7/errors
Set up
Apply errorHandlerMiddleware
as very last middleware in Express app.
import express from 'express';
import { errorHandlerMiddleware } from '@vavra7/errors';
const app = express();
app.use(errorHandlerMiddleware);
Usage
- In controllers catch and pass all errors in Express
next()
function. - You can use predefined errors that can be exported from package.
Example
import express from 'express';
import { errorHandlerMiddleware, NotAuthenticatedError } from '@vavra7/errors';
const app = express();
app.get('/posts/:id', (req, res, next) => {
try {
if (!req.context?.user) {
throw new NotAuthenticatedError();
}
res.send('some post')
} catch (err) {
next(err)
}
})
app.use(errorHandlerMiddleware);