Skip to content

Commit

Permalink
fix(medusa): Update wrapHandler so it also passes errors in non-asy…
Browse files Browse the repository at this point in the history
…nc 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 <[email protected]>
  • Loading branch information
kasperkristensen and olivermrbl authored Nov 15, 2023
1 parent c08240d commit 2947f57
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-books-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): Update wrapHandler so that it is also able to catch errors for sync handlers
11 changes: 9 additions & 2 deletions packages/medusa/src/api/middlewares/await-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { NextFunction, Request, RequestHandler, Response } from "express"

type handler = (req: Request, res: Response) => Promise<void>

/**
* @deprecated use `import { wrapHandler } from "@medusajs/utils"`
*/
export default (fn: handler): RequestHandler => {
return (req: Request, res: Response, next: NextFunction) => {
return async (req: Request, res: Response, next: NextFunction) => {
if (req?.errors?.length) {
return res.status(400).json({
errors: req.errors,
Expand All @@ -12,7 +15,11 @@ export default (fn: handler): RequestHandler => {
})
}

return fn(req, res).catch(next)
try {
return await fn(req, res)
} catch (err) {
next(err)
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/medusa/src/loaders/helpers/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promiseAll } from "@medusajs/utils"
import { promiseAll, wrapHandler } from "@medusajs/utils"
import cors from "cors"
import { Router, json, text, urlencoded, type Express } from "express"
import { readdir } from "fs/promises"
Expand All @@ -9,7 +9,6 @@ import {
authenticateCustomer,
errorHandler,
requireCustomerAuthentication,
wrapHandler,
} from "../../../api/middlewares"
import { ConfigModule } from "../../../types/global"
import logger from "../../logger"
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/src/common/wrap-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit 2947f57

Please sign in to comment.