fix: initiate request container before other express middleware (#12761)

This commit is contained in:
Harminder Virk authored and GitHub committed 2025-06-19 07:23:15 +00:00
1 parent ffa611b546
commit 94b62c6724
3 files changed
+23 -7

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/framework": patch
---
fix: fallback to console with req.scope is undefined
@@ -20,14 +20,20 @@ export function errorHandler() {
res: Response, res: Response,
_: NextFunction _: NextFunction
) { ) {
const logger = req.scope.resolve(ContainerRegistrationKeys.LOGGER) const logger = req.scope
? req.scope.resolve(ContainerRegistrationKeys.LOGGER)
: console
if (!req.scope) {
logger.error(
"req.scope is missing unexpectedly. It should be defined in all the cases"
)
}
err = formatException(err) err = formatException(err)
logger.error(err) logger.error(err)
const errorType = err.type || err.name const errorType = err.type || err.name
const errObj = { const errObj = {
code: err.code, code: err.code,
type: err.type, type: err.type,
+8 -4
View File
@@ -89,10 +89,10 @@ async function loadEntrypoints(
return async () => {} return async () => {}
} }
const { shutdown } = await expressLoader({ /**
app: expressApp, * The scope and the ip address must be fetched before we execute any other
}) * middleware
*/
expressApp.use((req: Request, res: Response, next: NextFunction) => { expressApp.use((req: Request, res: Response, next: NextFunction) => {
req.scope = container.createScope() as MedusaContainer req.scope = container.createScope() as MedusaContainer
req.requestId = (req.headers["x-request-id"] as string) ?? v4() req.requestId = (req.headers["x-request-id"] as string) ?? v4()
@@ -108,6 +108,10 @@ async function loadEntrypoints(
next() next()
}) })
const { shutdown } = await expressLoader({
app: expressApp,
})
await adminLoader({ app: expressApp, configModule, rootDirectory, plugins }) await adminLoader({ app: expressApp, configModule, rootDirectory, plugins })
await apiLoader({ await apiLoader({
container, container,