1.0.0 • Published 2 years ago
@infotition/express-error-handler v1.0.0
Installation
With npm:
npm install @infotition/express-error-handler
or with yarn:
yarn add @infotition/express-error-handler
Getting started
Convert and catch errors as express middleware:
import { errorConverter, errorHandler, ApiError } from '@infotition/express-error-handler';
import express, { Request, Response,NextFunction } from 'express';
const app = express();
// ... other middlewares
// convert error to ApiError, if needed
app.use((err: ApiError, _: Request, __: Response, next: NextFunction) =>
errorConverter(err, next),
);
// handle error
app.use((err: ApiError, _: Request, res: Response, __: NextFunction) =>
errorHandler(err, logger, res),
);
Catch the actual thrown api errors in the routes:
import { Router } from 'express';
import { catchAsync } from '@infotition/express-error-handler';
export const router = Router();
const testController = catchAsync(async (_, res) => {
res.status(200).send("works");
});
router.route('/test').get(testController);
Docs
Read full docs on GitHub.
1.0.0
2 years ago