Skip to content

Commit

Permalink
Fixes NullPointerException when accessing a FeignException's content (#…
Browse files Browse the repository at this point in the history
…914)

Fixes NullPointerException when accessing a FeignException's content

Fixes #912 

If the content of a FeignException is null, `contentUTF8()` now returns an empty string rather than throwing a NullPointerException.
  • Loading branch information
robachmann authored and kdavisk6 committed Mar 8, 2019
1 parent 78ca283 commit 73593dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public byte[] content() {
}

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

static FeignException errorReading(Request request, Response response, IOException cause) {
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,25 @@ public void throwsFeignExceptionIncludingBody() {
}
}

@Test
public void throwsFeignExceptionWithoutBody() {
server.enqueue(new MockResponse().setBody("success!"));

TestInterface api = Feign.builder()
.decoder((response, type) -> {
throw new IOException("timeout");
})
.target(TestInterface.class, "http://localhost:" + server.getPort());

try {
api.noContent();
} catch (FeignException e) {
assertThat(e.getMessage())
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("");
}
}

@Test
public void ensureRetryerClonesItself() throws Exception {
server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1"));
Expand Down Expand Up @@ -882,6 +901,9 @@ void login(
@RequestLine("POST /")
String body(String content);

@RequestLine("POST /")
String noContent();

@RequestLine("POST /")
@Headers("Content-Encoding: gzip")
void gzipBody(List<String> contents);
Expand Down

0 comments on commit 73593dc

Please sign in to comment.