Skip to content

Commit 6b8ed38

Browse files
robachmannkdavisk6
authored andcommitted
Fixes NullPointerException when accessing a FeignException's content (#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.
1 parent 089a59f commit 6b8ed38

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

core/src/main/java/feign/FeignException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public byte[] content() {
5757
}
5858

5959
public String contentUTF8() {
60-
return new String(content, UTF_8);
60+
if (content != null) {
61+
return new String(content, UTF_8);
62+
} else {
63+
return "";
64+
}
6165
}
6266

6367
static FeignException errorReading(Request request, Response response, IOException cause) {

core/src/test/java/feign/FeignTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,25 @@ public void throwsFeignExceptionIncludingBody() {
526526
}
527527
}
528528

529+
@Test
530+
public void throwsFeignExceptionWithoutBody() {
531+
server.enqueue(new MockResponse().setBody("success!"));
532+
533+
TestInterface api = Feign.builder()
534+
.decoder((response, type) -> {
535+
throw new IOException("timeout");
536+
})
537+
.target(TestInterface.class, "http://localhost:" + server.getPort());
538+
539+
try {
540+
api.noContent();
541+
} catch (FeignException e) {
542+
assertThat(e.getMessage())
543+
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
544+
assertThat(e.contentUTF8()).isEqualTo("");
545+
}
546+
}
547+
529548
@Test
530549
public void ensureRetryerClonesItself() throws Exception {
531550
server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1"));
@@ -882,6 +901,9 @@ void login(
882901
@RequestLine("POST /")
883902
String body(String content);
884903

904+
@RequestLine("POST /")
905+
String noContent();
906+
885907
@RequestLine("POST /")
886908
@Headers("Content-Encoding: gzip")
887909
void gzipBody(List<String> contents);

0 commit comments

Comments
 (0)