Skip to content

Commit 3237229

Browse files
finke-bakdavisk6
authored andcommitted
Added FeignRequestException, FeignResponseException (#769)
* Add FeignRequestException, FeignResponseException, update and add tests * Remove FeignRequestException, FeignResponseException, add 'content' field to FeignException, update tests. * Fix style
1 parent b8d33fb commit 3237229

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package feign;
1515

16+
import static feign.Util.UTF_8;
1617
import static java.lang.String.format;
1718
import java.io.IOException;
1819

@@ -23,40 +24,58 @@ public class FeignException extends RuntimeException {
2324

2425
private static final long serialVersionUID = 0;
2526
private int status;
27+
private byte[] content;
2628

2729
protected FeignException(String message, Throwable cause) {
2830
super(message, cause);
2931
}
3032

33+
protected FeignException(String message, Throwable cause, byte[] content) {
34+
super(message, cause);
35+
this.content = content;
36+
}
37+
3138
protected FeignException(String message) {
3239
super(message);
3340
}
3441

35-
protected FeignException(int status, String message) {
42+
protected FeignException(int status, String message, byte[] content) {
3643
super(message);
3744
this.status = status;
45+
this.content = content;
3846
}
3947

4048
public int status() {
4149
return this.status;
4250
}
4351

52+
public byte[] content() {
53+
return this.content;
54+
}
55+
56+
public String contentUTF8() {
57+
return new String(content, UTF_8);
58+
}
59+
4460
static FeignException errorReading(Request request, Response ignored, IOException cause) {
4561
return new FeignException(
4662
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
47-
cause);
63+
cause,
64+
request.body());
4865
}
4966

5067
public static FeignException errorStatus(String methodKey, Response response) {
5168
String message = format("status %s reading %s", response.status(), methodKey);
69+
70+
byte[] body = {};
5271
try {
5372
if (response.body() != null) {
54-
String body = Util.toString(response.body().asReader());
55-
message += "; content:\n" + body;
73+
body = Util.toByteArray(response.body().asInputStream());
5674
}
5775
} catch (IOException ignored) { // NOPMD
5876
}
59-
return new FeignException(response.status(), message);
77+
78+
return new FeignException(response.status(), message, body);
6079
}
6180

6281
static FeignException errorExecuting(Request request, IOException cause) {

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,25 @@ public Object decode(Response response, Type type) throws IOException {
511511
api.post();
512512
}
513513

514+
@Test
515+
public void throwsFeignExceptionIncludingBody() {
516+
server.enqueue(new MockResponse().setBody("success!"));
517+
518+
TestInterface api = Feign.builder()
519+
.decoder((response, type) -> {
520+
throw new IOException("timeout");
521+
})
522+
.target(TestInterface.class, "http://localhost:" + server.getPort());
523+
524+
try {
525+
api.body("Request body");
526+
} catch (FeignException e) {
527+
assertThat(e.getMessage())
528+
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
529+
assertThat(e.contentUTF8()).isEqualTo("Request body");
530+
}
531+
}
532+
514533
@Test
515534
public void ensureRetryerClonesItself() {
516535
server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1"));
@@ -776,6 +795,9 @@ void login(
776795
@RequestLine("POST /")
777796
void body(List<String> contents);
778797

798+
@RequestLine("POST /")
799+
String body(String content);
800+
779801
@RequestLine("POST /")
780802
@Headers("Content-Encoding: gzip")
781803
void gzipBody(List<String> contents);

core/src/test/java/feign/client/AbstractClientTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void reasonPhraseIsOptional() throws IOException, InterruptedException {
114114
@Test
115115
public void parsesErrorResponse() throws IOException, InterruptedException {
116116
thrown.expect(FeignException.class);
117-
thrown.expectMessage("status 500 reading TestInterface#get(); content:\n" + "ARGHH");
117+
thrown.expectMessage("status 500 reading TestInterface#get()");
118118

119119
server.enqueue(new MockResponse().setResponseCode(500).setBody("ARGHH"));
120120

@@ -124,6 +124,22 @@ public void parsesErrorResponse() throws IOException, InterruptedException {
124124
api.get();
125125
}
126126

127+
@Test
128+
public void parsesErrorResponseBody() {
129+
String expectedResponseBody = "ARGHH";
130+
131+
server.enqueue(new MockResponse().setResponseCode(500).setBody("ARGHH"));
132+
133+
TestInterface api = newBuilder()
134+
.target(TestInterface.class, "http://localhost:" + server.getPort());
135+
136+
try {
137+
api.get();
138+
} catch (FeignException e) {
139+
assertThat(e.contentUTF8()).isEqualTo(expectedResponseBody);
140+
}
141+
}
142+
127143
@Test
128144
public void safeRebuffering() throws IOException, InterruptedException {
129145
server.enqueue(new MockResponse().setBody("foo"));

core/src/test/java/feign/codec/DefaultErrorDecoderTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ public void throwsFeignException() throws Throwable {
5555

5656
@Test
5757
public void throwsFeignExceptionIncludingBody() throws Throwable {
58-
thrown.expect(FeignException.class);
59-
thrown.expectMessage("status 500 reading Service#foo(); content:\nhello world");
60-
6158
Response response = Response.builder()
6259
.status(500)
6360
.reason("Internal server error")
@@ -66,7 +63,12 @@ public void throwsFeignExceptionIncludingBody() throws Throwable {
6663
.body("hello world", UTF_8)
6764
.build();
6865

69-
throw errorDecoder.decode("Service#foo()", response);
66+
try {
67+
throw errorDecoder.decode("Service#foo()", response);
68+
} catch (FeignException e) {
69+
assertThat(e.getMessage()).isEqualTo("status 500 reading Service#foo()");
70+
assertThat(e.contentUTF8()).isEqualTo("hello world");
71+
}
7072
}
7173

7274
@Test

0 commit comments

Comments
 (0)