Affects: 5.3.6
webmvc.fn onError
doesn't work with CompletableFuture
.
Error handler predicates are never called.
Take a look at the code below.
error1 scenario shows the problem: application should return an error response with "error" body, returns Whitelabel Error Page instead.
error2 scenario works fine.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;
import java.util.concurrent.CompletableFuture;
import static org.springframework.web.servlet.function.RouterFunctions.route;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RouterFunction<ServerResponse> routes() {
return route()
.GET("error1", request -> {
CompletableFuture<String> result = CompletableFuture.supplyAsync(
() -> {
throwRuntimeException();
return "test";
}
);
return ServerResponse.ok().body(result);
})
.GET("error2", request -> {
throwRuntimeException();
CompletableFuture<String> result = CompletableFuture.supplyAsync(
() -> "test"
);
return ServerResponse.ok().body(result);
})
.onError(RuntimeException.class, (throwable, request) ->
ServerResponse
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("error")
)
.build();
}
private void throwRuntimeException() {
throw new RuntimeException();
}
}