Errors raised in the BodyParser middleware not propagated to interceptor
I have a NestJS application that is using the express' built-in BodyParser middleware to limit the request size. I've created a global interceptor to transform various errors and exceptions, and have also created a global ErrorFilter to cleanup and create responses. // middleware code: nestApp.use('*', json({ limit: '256kb' })) The above code is called in the bootstrap method of the application. I also have an ErrorInteceptor that is registered before the app is intialized // interceptor code export class ErrorInterceptor implements NestInterceptor { intercept(_context: ExecutionContext, next: CallHandler): Observable { return next.handle().pipe( catchError(error => { // ... do error transformations }), ) } } Lastly I also register a global custom ErrorFilter. Whenever an error is throw in the applicative code it will go through both the interceptor and the error filter. However, errors thrown by the built-in middleware are only passed to the error filter i.e. a PayloadTooLarge error won't be passed to the interceptor to transform. It will go directly to the filter. According to the docs, interceptors are called after the middleware and before the filters so I don't understand what's going on. My guess is that since I'm using a 4.x express version something isn't handled properly but I can't upgrade to 5.x. The docs say that: For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them. For example: I want to avoid from adding a custom error handler to the middleware since I want everything transformed by the interceptor. Any help or hints are greatly appreciated!
I have a NestJS
application that is using the express
' built-in BodyParser middleware to limit the request size. I've created a global interceptor to transform various errors and exceptions, and have also created a global ErrorFilter
to cleanup and create responses.
// middleware code:
nestApp.use('*', json({ limit: '256kb' }))
The above code is called in the bootstrap
method of the application.
I also have an ErrorInteceptor
that is registered before the app is intialized
// interceptor code
export class ErrorInterceptor implements NestInterceptor {
intercept(_context: ExecutionContext, next: CallHandler): Observable {
return next.handle().pipe(
catchError(error => {
// ... do error transformations
}),
)
}
}
Lastly I also register a global custom ErrorFilter
.
Whenever an error is throw in the applicative code it will go through both the interceptor and the error filter. However, errors thrown by the built-in middleware are only passed to the error filter i.e. a PayloadTooLarge
error won't be passed to the interceptor to transform. It will go directly to the filter.
According to the docs, interceptors are called after the middleware and before the filters so I don't understand what's going on. My guess is that since I'm using a 4.x express version something isn't handled properly but I can't upgrade to 5.x. The docs say that:
For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them. For example:
I want to avoid from adding a custom error handler to the middleware since I want everything transformed by the interceptor. Any help or hints are greatly appreciated!