From 2aec175ccc5a5c3cbcd152697956ee5da90e5214 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 5 Jul 2019 10:48:49 +0100 Subject: [PATCH] Drain body after exception from applying onStatus Closes gh-23230 --- .../reactive/function/client/DefaultWebClient.java | 14 ++++++++++---- .../client/WebClientDataBufferAllocatingTests.java | 11 ++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index ebf2b041d5..629b16e600 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -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 exMono = handler.apply(response, request); - exMono = exMono.flatMap(ex -> drainBody(response, ex)); - exMono = exMono.onErrorResume(ex -> drainBody(response, ex)); + Mono 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); } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java index d750f93f16..b1a05f5c28 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java @@ -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> exceptionFunction) {