diff --git a/.changeset/tricky-plants-sell.md b/.changeset/tricky-plants-sell.md new file mode 100644 index 0000000000..abaafde176 --- /dev/null +++ b/.changeset/tricky-plants-sell.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"@medusajs/framework": patch +--- + +fix: fallback to console with req.scope is undefined diff --git a/packages/core/framework/src/http/middlewares/error-handler.ts b/packages/core/framework/src/http/middlewares/error-handler.ts index 478dad206c..822d0c1582 100644 --- a/packages/core/framework/src/http/middlewares/error-handler.ts +++ b/packages/core/framework/src/http/middlewares/error-handler.ts @@ -20,14 +20,20 @@ export function errorHandler() { res: Response, _: 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) - logger.error(err) const errorType = err.type || err.name - const errObj = { code: err.code, type: err.type, diff --git a/packages/medusa/src/loaders/index.ts b/packages/medusa/src/loaders/index.ts index 3ca4368ce9..9bccfcdfb9 100644 --- a/packages/medusa/src/loaders/index.ts +++ b/packages/medusa/src/loaders/index.ts @@ -89,10 +89,10 @@ async function loadEntrypoints( 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) => { req.scope = container.createScope() as MedusaContainer req.requestId = (req.headers["x-request-id"] as string) ?? v4() @@ -108,6 +108,10 @@ async function loadEntrypoints( next() }) + const { shutdown } = await expressLoader({ + app: expressApp, + }) + await adminLoader({ app: expressApp, configModule, rootDirectory, plugins }) await apiLoader({ container,