Revert errorhandler order in RouterFunctionBuilder

Prior to this commit, error handlers in the WebMvc.fn and WebFlux.fn
router function builders had to be registered in an unintuitive, reverse
order, due to the filter chain composition model used.
This commit reverses the error handler order, so that more specific
error handlers can come before generic ones.

Closes gh-25541
This commit is contained in:
Arjen Poutsma
2020-09-15 15:43:25 +02:00
parent 200b33b26a
commit 392895e256
4 changed files with 82 additions and 37 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.web.reactive.function.server;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
@@ -210,4 +211,26 @@ public class RouterFunctionBuilderTests {
.verifyComplete();
}
@Test
public void multipleOnErrors() {
RouterFunction<ServerResponse> route = RouterFunctions.route()
.GET("/error", request -> Mono.error(new IOException()))
.onError(IOException.class, (t, r) -> ServerResponse.status(200).build())
.onError(Exception.class, (t, r) -> ServerResponse.status(201).build())
.build();
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/error").build();
ServerRequest serverRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<HttpStatus> responseStatus = route.route(serverRequest)
.flatMap(handlerFunction -> handlerFunction.handle(serverRequest))
.map(ServerResponse::statusCode);
StepVerifier.create(responseStatus)
.assertNext(status -> assertThat(status).isEqualTo(HttpStatus.OK))
.verifyComplete();
}
}