Drain body after exception from applying onStatus

Closes gh-23230
This commit is contained in:
Rossen Stoyanchev 2019-07-05 10:48:49 +01:00
parent 2088a3d57b
commit 2aec175ccc
2 changed files with 20 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -455,9 +455,15 @@ class DefaultWebClient implements WebClient {
for (StatusHandler handler : this.statusHandlers) {
if (handler.test(response.statusCode())) {
HttpRequest request = this.requestSupplier.get();
Mono<? extends Throwable> exMono = handler.apply(response, request);
Mono<? extends Throwable> exMono;
try {
exMono = handler.apply(response, request);
exMono = exMono.flatMap(ex -> drainBody(response, ex));
exMono = exMono.onErrorResume(ex -> drainBody(response, ex));
}
catch (Throwable ex2) {
exMono = drainBody(response, ex2);
}
return errorFunction.apply(exMono);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -147,6 +147,15 @@ public class WebClientDataBufferAllocatingTests extends AbstractDataBufferAlloca
testOnStatus(ex, response -> response.bodyToMono(Void.class).then(Mono.error(ex)));
}
@Test // gh-23230
public void onStatusWithImmediateErrorAndBodyNotConsumed() {
RuntimeException ex = new RuntimeException("response error");
testOnStatus(ex, response -> {
throw ex;
});
}
private void testOnStatus(Throwable expected,
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {