fix(medusa): Error handling middleware should be applied when no config is exported (#5634)
**What** - Fixes an issue where the default errorHandler middleware was not applied if the user did not have a `middlewares.ts` file. - Updates the type of `MiddlewaresConfig` so `middlewares` is now optional, as people might want to only set a bodyParser config for a matcher and not provide any additional middleware.
This commit is contained in:
committed by
GitHub
parent
7226f5b699
commit
52800710a2
5
.changeset/smooth-baboons-repeat.md
Normal file
5
.changeset/smooth-baboons-repeat.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa): Ensure that errorHandler middleware is applied if no middleware config exists
|
||||
@@ -653,12 +653,7 @@ export class RoutesLoader {
|
||||
*/
|
||||
applyErrorHandlerMiddleware(): void {
|
||||
const middlewareDescriptor = this.globalMiddlewaresDescriptor
|
||||
|
||||
if (!middlewareDescriptor) {
|
||||
return
|
||||
}
|
||||
|
||||
const errorHandlerFn = middlewareDescriptor.config?.errorHandler
|
||||
const errorHandlerFn = middlewareDescriptor?.config?.errorHandler
|
||||
|
||||
/**
|
||||
* If the user has opted out of the error handler then return
|
||||
@@ -740,6 +735,10 @@ export class RoutesLoader {
|
||||
*/
|
||||
|
||||
for (const route of routes) {
|
||||
if (!route.middlewares || !route.middlewares.length) {
|
||||
continue
|
||||
}
|
||||
|
||||
const methods = (
|
||||
Array.isArray(route.method) ? route.method : [route.method]
|
||||
).filter(Boolean) as MiddlewareVerb[]
|
||||
|
||||
@@ -65,7 +65,7 @@ export type MiddlewareRoute = {
|
||||
method?: MiddlewareVerb | MiddlewareVerb[]
|
||||
matcher: string | RegExp
|
||||
bodyParser?: ParserConfig
|
||||
middlewares: MiddlewareFunction[]
|
||||
middlewares?: MiddlewareFunction[]
|
||||
}
|
||||
|
||||
export type MiddlewaresConfig = {
|
||||
|
||||
Reference in New Issue
Block a user