diff --git a/core/src/main/java/feign/FeignException.java b/core/src/main/java/feign/FeignException.java index b7ea794cae..0779598c61 100644 --- a/core/src/main/java/feign/FeignException.java +++ b/core/src/main/java/feign/FeignException.java @@ -177,13 +177,20 @@ public String contentUTF8() { } static FeignException errorReading(Request request, Response response, IOException cause) { + byte[] body = {}; + try { + if (response.body() != null) { + body = Util.toByteArray(response.body().asInputStream()); + } + } catch (IOException ignored) { // NOPMD + } return new FeignException( response.status(), format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()), request, cause, - request.body(), - request.headers()); + body, + response.headers()); } public static FeignException errorStatus(String methodKey, Response response) { diff --git a/core/src/test/java/feign/AsyncFeignTest.java b/core/src/test/java/feign/AsyncFeignTest.java index 300385be64..a67242b139 100644 --- a/core/src/test/java/feign/AsyncFeignTest.java +++ b/core/src/test/java/feign/AsyncFeignTest.java @@ -621,7 +621,8 @@ void throwsFeignExceptionIncludingBody() throws Throwable { } catch (FeignException e) { assertThat(e.getMessage()) .contains("timeout reading POST http://localhost:" + server.getPort() + "/"); - assertThat(e.contentUTF8()).isEqualTo("Request body"); + // After #2618 the FeignException carries the response body, not the request body. + assertThat(e.contentUTF8()).isEqualTo("success!"); return; } fail(""); diff --git a/core/src/test/java/feign/FeignExceptionTest.java b/core/src/test/java/feign/FeignExceptionTest.java index f86d35fe19..e585d35e7c 100644 --- a/core/src/test/java/feign/FeignExceptionTest.java +++ b/core/src/test/java/feign/FeignExceptionTest.java @@ -43,16 +43,22 @@ void canCreateWithRequestAndResponse() { StandardCharsets.UTF_8, null); + Map> responseHeaders = + Collections.singletonMap("content-type", Collections.singletonList("application/json")); Response response = Response.builder() .status(400) .body("response".getBytes(StandardCharsets.UTF_8)) + .headers(responseHeaders) .request(request) .build(); FeignException exception = FeignException.errorReading(request, response, new IOException("socket closed")); assertThat(exception.responseBody()).isNotEmpty(); + // the exception must carry the response body, not the request body (gh-2618) + assertThat(exception.contentUTF8()).isEqualTo("response"); + assertThat(exception.responseHeaders()).containsKeys("content-type"); assertThat(exception.hasRequest()).isTrue(); assertThat(exception.request()).isNotNull(); } diff --git a/core/src/test/java/feign/FeignTest.java b/core/src/test/java/feign/FeignTest.java index 47a348bfaf..03acf67a51 100755 --- a/core/src/test/java/feign/FeignTest.java +++ b/core/src/test/java/feign/FeignTest.java @@ -607,13 +607,14 @@ void throwsFeignExceptionIncludingBody() { } catch (FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/"); - assertThat(e.contentUTF8()).isEqualTo("Request body"); + // After #2618 the FeignException carries the response body, not the request body. + assertThat(e.contentUTF8()).isEqualTo("success!"); } } @Test void throwsFeignExceptionWithoutBody() { - server.enqueue(new MockResponse().setBody("success!")); + server.enqueue(new MockResponse()); TestInterface api = Feign.builder() diff --git a/core/src/test/java/feign/FeignUnderAsyncTest.java b/core/src/test/java/feign/FeignUnderAsyncTest.java index f18fd5d747..ac150ae370 100644 --- a/core/src/test/java/feign/FeignUnderAsyncTest.java +++ b/core/src/test/java/feign/FeignUnderAsyncTest.java @@ -493,13 +493,14 @@ void throwsFeignExceptionIncludingBody() { } catch (FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/"); - assertThat(e.contentUTF8()).isEqualTo("Request body"); + // After #2618 the FeignException carries the response body, not the request body. + assertThat(e.contentUTF8()).isEqualTo("success!"); } } @Test void throwsFeignExceptionWithoutBody() { - server.enqueue(new MockResponse().setBody("success!")); + server.enqueue(new MockResponse()); TestInterface api = AsyncFeign.builder() diff --git a/hc5/src/test/java/feign/hc5/AsyncApacheHttp5ClientTest.java b/hc5/src/test/java/feign/hc5/AsyncApacheHttp5ClientTest.java index 6d5902d9c0..3b3a4f33cf 100644 --- a/hc5/src/test/java/feign/hc5/AsyncApacheHttp5ClientTest.java +++ b/hc5/src/test/java/feign/hc5/AsyncApacheHttp5ClientTest.java @@ -535,7 +535,8 @@ void throwsFeignExceptionIncludingBody() throws Throwable { } catch (final FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/"); - assertThat(e.contentUTF8()).isEqualTo("Request body"); + // After #2618 the FeignException carries the response body, not the request body. + assertThat(e.contentUTF8()).isEqualTo("success!"); return; } fail(""); diff --git a/java11/src/test/java/feign/http2client/test/Http2ClientAsyncTest.java b/java11/src/test/java/feign/http2client/test/Http2ClientAsyncTest.java index 0d3cd6570b..c5a29c43e9 100644 --- a/java11/src/test/java/feign/http2client/test/Http2ClientAsyncTest.java +++ b/java11/src/test/java/feign/http2client/test/Http2ClientAsyncTest.java @@ -529,7 +529,8 @@ void throwsFeignExceptionIncludingBody() throws Throwable { } catch (final FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/"); - assertThat(e.contentUTF8()).isEqualTo("Request body"); + // After #2618 the FeignException carries the response body, not the request body. + assertThat(e.contentUTF8()).isEqualTo("success!"); return; } fail(""); diff --git a/okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java b/okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java index 79150b86b9..5b2fbe3542 100644 --- a/okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java +++ b/okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java @@ -528,7 +528,8 @@ void throwsFeignExceptionIncludingBody() throws Throwable { } catch (final FeignException e) { assertThat(e.getMessage()) .isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/"); - assertThat(e.contentUTF8()).isEqualTo("Request body"); + // After #2618 the FeignException carries the response body, not the request body. + assertThat(e.contentUTF8()).isEqualTo("success!"); return; } fail("");