fix(medusa): Update wrapHandler so it also passes errors in non-async route handlers to the errorHandler middleware (#5597)
* fix: Align @types/react versions across UI packages * update codeowners * fix: Update wrapHandler so it also catches errors for non-async route handlers * rm chaining * align wrapHandler in utils and add deprecation note --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
c08240df3f
commit
2947f57db1
@@ -3,7 +3,7 @@ import { NextFunction, Request, RequestHandler, Response } from "express"
|
||||
type handler = (req: Request, res: Response) => Promise<void>
|
||||
|
||||
export const wrapHandler = (fn: handler): RequestHandler => {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
const req_ = req as Request & { errors?: Error[] }
|
||||
if (req_?.errors?.length) {
|
||||
return res.status(400).json({
|
||||
@@ -13,7 +13,11 @@ export const wrapHandler = (fn: handler): RequestHandler => {
|
||||
})
|
||||
}
|
||||
|
||||
return fn(req, res).catch(next)
|
||||
try {
|
||||
return await fn(req, res)
|
||||
} catch (err) {
|
||||
next(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user