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:
Kasper Fabricius KristensenandOli Juhl authored and GitHub committed 2023-11-15 19:41:10 +01:00
1 parent c08240df3f
commit 2947f57db1
4 files changed
+21 -6

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Update wrapHandler so that it is also able to catch errors for sync handlers
@@ -2,8 +2,11 @@ import { NextFunction, Request, RequestHandler, Response } from "express"
type handler = (req: Request, res: Response) => Promise<void> type handler = (req: Request, res: Response) => Promise<void>
/**
* @deprecated use `import { wrapHandler } from "@medusajs/utils"`
*/
export default (fn: handler): RequestHandler => { export default (fn: handler): RequestHandler => {
return (req: Request, res: Response, next: NextFunction) => { return async (req: Request, res: Response, next: NextFunction) => {
if (req?.errors?.length) { if (req?.errors?.length) {
return res.status(400).json({ return res.status(400).json({
errors: req.errors, errors: req.errors,
@@ -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)
}
} }
} }
@@ -1,4 +1,4 @@
import { promiseAll } from "@medusajs/utils" import { promiseAll, wrapHandler } from "@medusajs/utils"
import cors from "cors" import cors from "cors"
import { Router, json, text, urlencoded, type Express } from "express" import { Router, json, text, urlencoded, type Express } from "express"
import { readdir } from "fs/promises" import { readdir } from "fs/promises"
@@ -9,7 +9,6 @@ import {
authenticateCustomer, authenticateCustomer,
errorHandler, errorHandler,
requireCustomerAuthentication, requireCustomerAuthentication,
wrapHandler,
} from "../../../api/middlewares" } from "../../../api/middlewares"
import { ConfigModule } from "../../../types/global" import { ConfigModule } from "../../../types/global"
import logger from "../../logger" import logger from "../../logger"
+6 -2
View File
@@ -3,7 +3,7 @@ import { NextFunction, Request, RequestHandler, Response } from "express"
type handler = (req: Request, res: Response) => Promise<void> type handler = (req: Request, res: Response) => Promise<void>
export const wrapHandler = (fn: handler): RequestHandler => { 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[] } const req_ = req as Request & { errors?: Error[] }
if (req_?.errors?.length) { if (req_?.errors?.length) {
return res.status(400).json({ 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)
}
} }
} }