I'm trying to retrieve data from a REST service using Spring's WebClient and trying to throw the 4XX and 5XX in a Error
instead of instead of a RuntimeException
.
return webclient.
.get()
.uri(myURI)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatus::isError, handleError()
.bodyToMono(String.class)
.block();
private static Function<ClientResponse, Mono<? extends Throwable>> handleError() {
return response -> response.bodyToMono(String.class).map(CustomError::new);
}
I'm trying to get this Error
in a test but received a ReactiveException
instead.
org.opentest4j.AssertionFailedError: Unexpected exception type thrown,
Expected :class com.example.CustomError
Actual :class reactor.core.Exceptions$ReactiveException
When I switch the extesion of CustomError
from Error
to RuntimeException
the test pass.
Since the onStatus expcept a Function<ClientResponse, Mono<? extends Throwable>>
I expected to be able to throw an Error in this call.
I'm working on a test library and a initial request is strictly necessary and without a successfully request the test must fail. A Error
will be prevent anyone to catch any problem related with the REST request in their own tests.