add catchAsync to handle errors, remove all try/catch blocks from contorllers
This commit is contained in:
1 parent
5120390923
commit
bd13e9d9c8
15 files changed
+1243
-1535
No files matched your search
@@ -0,0 +1,14 @@
|
||||
import type { NextFunction, Request, RequestHandler, Response } from "express";
|
||||
type AsyncRequestHandler = (req: Request, res: Response) => Promise<unknown>;
|
||||
|
||||
/**
|
||||
* Wraps an async route handler so that any error it throws or any promise it
|
||||
* rejects is passed to Express's error middleware (`handleErrors`). Every async controller method must be wrapped.
|
||||
*/
|
||||
export const catchAsync = (fn: AsyncRequestHandler): RequestHandler => {
|
||||
const wrappedHandler = (req: Request, res: Response, next: NextFunction) => {
|
||||
fn(req, res).catch(next);
|
||||
};
|
||||
|
||||
return wrappedHandler;
|
||||
};
|
||||
Reference in new issue
Block a user