Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/test/java/feign/AsyncFeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/java/feign/FeignExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ void canCreateWithRequestAndResponse() {
StandardCharsets.UTF_8,
null);

Map<String, Collection<String>> 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();
}
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/java/feign/FeignUnderAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion hc5/src/test/java/feign/hc5/AsyncApacheHttp5ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
3 changes: 2 additions & 1 deletion okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down