Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FeignException::responseBody() returns request body instead of response body #2619

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
23 changes: 14 additions & 9 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ static FeignException errorReading(Request request, Response response, IOExcepti
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
request,
cause,
request.body(),
request.headers());
readResponseBody(response),
response.headers());
}

public static FeignException errorStatus(String methodKey, Response response) {
Expand All @@ -193,13 +193,7 @@ public static FeignException errorStatus(String methodKey, Response response) {
public static FeignException errorStatus(
String methodKey, Response response, Integer maxBodyBytesLength, Integer maxBodyCharsLength) {

byte[] body = {};
try {
if (response.body() != null) {
body = Util.toByteArray(response.body().asInputStream());
}
} catch (IOException ignored) { // NOPMD
}
byte[] body = readResponseBody(response);

String message =
new FeignExceptionMessageBuilder()
Expand All @@ -213,6 +207,17 @@ public static FeignException errorStatus(
return errorStatus(response.status(), message, response.request(), body, response.headers());
}

private static byte[] readResponseBody(Response response) {
byte[] body = {};
try {
if (response.body() != null) {
body = Util.toByteArray(response.body().asInputStream());
}
} catch (IOException ignored) { // NOPMD
}
return body;
}

private static FeignException errorStatus(
int status,
String message,
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/feign/AsyncFeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void throwsFeignExceptionIncludingBody() throws Throwable {
} catch (FeignException e) {
assertThat(e.getMessage())
.contains("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("Request body");
assertThat(e.contentUTF8()).isEqualTo("success!");
return;
}
fail("");
Expand Down
1 change: 1 addition & 0 deletions core/src/test/java/feign/FeignExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void canCreateWithRequestAndResponse() {
FeignException exception =
FeignException.errorReading(request, response, new IOException("socket closed"));
assertThat(exception.responseBody()).isNotEmpty();
assertThat(exception.responseBody().get().array()).asString().isEqualTo("response");
assertThat(exception.hasRequest()).isTrue();
assertThat(exception.request()).isNotNull();
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ void throwsFeignExceptionIncludingBody() {
} catch (FeignException e) {
assertThat(e.getMessage())
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("Request body");
assertThat(e.contentUTF8()).isEqualTo("success!");
}
}

Expand All @@ -624,7 +624,7 @@ void throwsFeignExceptionWithoutBody() {
} catch (FeignException e) {
assertThat(e.getMessage())
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("");
assertThat(e.contentUTF8()).isEqualTo("success!");
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/feign/FeignUnderAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ void throwsFeignExceptionIncludingBody() {
} catch (FeignException e) {
assertThat(e.getMessage())
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("Request body");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, backwards compatibility is my main concern here.

I think we need to deprecate contentUTF8, keep response body as result of contentUTF8 and introduce resquestBody and responseBody or something with that meaning.

Breaking compatibility like this only on major releases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is also the same bug, because contentUTF8() method contains following code:

public String contentUTF8() {
    if (responseBody != null) {
      return new String(responseBody, UTF_8);
    } else {
      return "";
    }
  }

Also content() method is deprecated and method responseBody() already exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example in this case contentUTF8() returns response body hello world

assertThat(e.contentUTF8()).isEqualTo("success!");
}
}

Expand All @@ -511,7 +511,7 @@ void throwsFeignExceptionWithoutBody() {
} catch (FeignException e) {
assertThat(e.getMessage())
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("");
assertThat(e.contentUTF8()).isEqualTo("success!");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ 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");
assertThat(e.contentUTF8()).isEqualTo("success!");
return;
}
fail("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ 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");
assertThat(e.contentUTF8()).isEqualTo("success!");
return;
}
fail("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ 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");
assertThat(e.contentUTF8()).isEqualTo("success!");
return;
}
fail("");
Expand Down