OnErrorResume does not work as expected

I am trying to use WebFlux and I see a behavior that I do not quite understand, I suspect that this is a bug in WebFlux or possibly Reactor, but I need confirmation.

I tried to create a minimally reproducible case consisting of a very simple HandlerFunction that tries to return a 200 response, but throws an exception while creating the body, and then tries to use onErrorResume instead of returning a 404 answer.

The handler looks like this:

public Mono<ServerResponse> reproduce(ServerRequest request){
        return ServerResponse.ok()
        .contentType(APPLICATION_JSON)
        .body(Mono.fromCallable(this::trigger),String.class)
        .onErrorResume(MinimalExampleException.class,e->ServerResponse.notFound().build());
}

I would expect that when I call the associated endpoint, I get a 404 response. Instead, I see a 500 response with log messages indicating Spring, assuming that there was an unhandled exception during request processing.

When I break the point inside onErrorResume, I see that two handlers are registered, the one that I register in the above method, and the one registered by Spring (inside RouterFunctions.toHttpHandler) for the instances ResponseStatusException. Then, during the processing of the request, I call only the second handler (the one registered by Spring), and does not match the exception that was selected, and then proceeds to the root level handler.

Next to how I can tell, Spring is rewriting onErrorResume at the router level, preventing the call that I registered in the handler. Is this expected behavior? Is there a better way to accomplish what I'm trying to do?

+4
source share
1

"catch-all", , .body(...). , , .

3 :

:

return Mono.fromCallable(this::trigger)
           .flatMap(s -> ServerResponse.ok()
                        .contentType(MediaType.TEXT_PLAIN)
                        .syncBody(s))
           .onErrorResume(MinimalExampleException.class,
                        e -> ServerResponse.notFound().build());
  1. ResponseStatusException

onErrorResume , fromCallable() ( ), ResponseStatusException mono:

Mono.fromCallable(this::trigger)
    .onErrorResume(MinimalExampleException.class,
        e->Mono.error(new ResponseStatusException(404, "reason message", e)))

@ExceptionHandler, .

+4

Source: https://habr.com/ru/post/1678202/


All Articles