Provide a mechanism to handle all the exceptions via @ControllerAdvice including the errors emitted from the WebFilter.

I have attached a simple reproducible project with latest spring boot 3.3.0 + WebFlux.

demo.zip

Comment From: simonbasle

The repro case is indeed quite straightforward. Although I'm not sure about the way you simulate a WebFilter error (by using exchange.getResponse().writeWith(Mono.error(...))), there's also the simpler case of doing return Mono.error(...) commented out in your repro case, which seems more admissible but doesn't work either.

AFAIU filter errors are supposed to be processable by @ControllerAdvices in both webmvc and webflux since 6.0 (see gh-22991), with the introduction of DispatchExceptionHandler and the fact that RequestMappingHandlerAdapter implements that interface 🤔

But here from what I've seen the error reaches an HttpWebHandlerAdapter rather than a RequestMappingHandlerAdapter.

@rstoyanchev I would need your help in investigating this a bit further and determining if it a setup issue or if the repro case is indeed supposed to work as-is and this is an actual bug.

Comment From: kitkars

@simonbasle , Thanks. I tried all these options - assuming at least one option should work.. none of them works! :(

exchange.getResponse().writeWith(Mono.error(...))
Mono.error(...)
throw MyException()

Comment From: bclozel

I think this behavior should be by design.

I believe the intent behind #22991 (aligning WebFlux with #22619) was for @ExceptionHandler methods to be involved for all WebHandler (WebFlux) and HttpRequestHandler (MVC).

On the other hand, I think we can compare WebFilter to Servlet filters as they operate at a lower level. Commit 2878ade does mention the WebFilterChain as a possible source of handled exceptions, but I don't think this is effectively the case. Filters in WebFlux are managed at the HttpHandler level, see:

https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java#L406-L411

I believe HttpHandler is the HTTP adapter layer, where Spring and other Frameworks/libraries can be plugged in the HTTP server. Handling errors at this level involves the WebExceptionHandler contract, which does not know about controllers or controller advices.

The MVC documentation points to other handlers, not Sevlet Filters:

Moreover, as of 5.3, @ExceptionHandler methods in @ControllerAdvice can be used to handle exceptions from any @Controller or any other handler.

Comment From: rstoyanchev

Indeed this is by design. WebFilters are ahead of the DispatcherHandler and outside of higher level, web framework handling through annotated controller methods, and controller advice.

Depending on what you need to do, Boot error handling also could be helpful for errors that happen outside, as well as errors that escape and remain not handled.

Comment From: bclozel

@kitkars you can implement your own WebExceptionHandler, and declare it as a bean in your application to handle such exceptions. Ordering it with @Order(-2) should place it right before the one from Spring Boot. See the error handling section of the Spring Boot docs.

Let us know how it works for you.

Comment From: kitkars

Thanks @bclozel . I am already using workarounds. The controller-advice is one such elegant way to keep centralized error handling for an app. Thanks. We can close this as there are work arounds.

Comment From: bclozel

Thanks for letting us know. I'm glad this works for your app.