I wrote a try catch wrapper for all my routes in Express JS but it doesn’t seem to work as intended.
catchErrors.ts
import { NextFunction, Request, Response } from "express"
export const catchErrors = (originalFunction: any) => {
return function (req: Request, res: Response, next: NextFunction) {
try {
return originalFunction(req, res, next)
} catch (e) {
next(e)
}
}
}
Then I wrap any route using the wrapper:
router.get('/', catchErrors(this.users))
Got any idea why?