Express-Torch icon indicating copy to clipboard operation
Express-Torch copied to clipboard

Feature request: wrapper for routes

Open DJWassink opened this issue 6 years ago • 1 comments

Express doesn't really handle async/await that nicely, so either we have to add a try/catch inside an async router function or the error won't be handled by the error fallback, so a lot of times people tend to wrap a handler in an async error catcher, for example:

export const asyncWrapper = (handler: (req: Request, res: Response, next?: NextFunction) => Promise<any>) => (req: Request, res: Response, next?: NextFunction) => {
    Promise.resolve(handler(req, res, next)).catch(next);
}

Since we already got a nice route group method, maybe apart from the middleware we can also support a wrapper functionality?

DJWassink avatar Mar 14 '18 12:03 DJWassink

Can you submit a PR with a failing test showing the expecting behavior?

JBlaak avatar Mar 14 '18 12:03 JBlaak