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

Added FeignRequestException, FeignResponseException #769

Merged
Merged
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
29 changes: 24 additions & 5 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package feign;

import static feign.Util.UTF_8;
import static java.lang.String.format;
import java.io.IOException;

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

private static final long serialVersionUID = 0;
private int status;
private byte[] content;

protected FeignException(String message, Throwable cause) {
super(message, cause);
}

protected FeignException(String message, Throwable cause, byte[] content) {
super(message, cause);
this.content = content;
}

protected FeignException(String message) {
super(message);
}

protected FeignException(int status, String message) {
protected FeignException(int status, String message, byte[] content) {
super(message);
this.status = status;
this.content = content;
}

public int status() {
return this.status;
}

public byte[] content() {
return this.content;
}

public String contentUTF8() {
return new String(content, UTF_8);
}

static FeignException errorReading(Request request, Response ignored, IOException cause) {
return new FeignException(
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
cause);
cause,
request.body());
}

public static FeignException errorStatus(String methodKey, Response response) {
String message = format("status %s reading %s", response.status(), methodKey);

byte[] body = {};
try {
if (response.body() != null) {
String body = Util.toString(response.body().asReader());
message += "; content:\n" + body;
body = Util.toByteArray(response.body().asInputStream());
}
} catch (IOException ignored) { // NOPMD
}
return new FeignException(response.status(), message);

return new FeignException(response.status(), message, body);
}

static FeignException errorExecuting(Request request, 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 @@ -511,6 +511,25 @@ public Object decode(Response response, Type type) throws IOException {
api.post();
}

@Test
public void throwsFeignExceptionIncludingBody() {
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.body("Request body");
} catch (FeignException e) {
assertThat(e.getMessage())
.isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("Request body");
}
}

@Test
public void ensureRetryerClonesItself() {
server.enqueue(new MockResponse().setResponseCode(503).setBody("foo 1"));
Expand Down Expand Up @@ -776,6 +795,9 @@ void login(
@RequestLine("POST /")
void body(List<String> contents);

@RequestLine("POST /")
String body(String content);

@RequestLine("POST /")
@Headers("Content-Encoding: gzip")
void gzipBody(List<String> contents);
Expand Down
18 changes: 17 additions & 1 deletion core/src/test/java/feign/client/AbstractClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void reasonPhraseIsOptional() throws IOException, InterruptedException {
@Test
public void parsesErrorResponse() throws IOException, InterruptedException {
thrown.expect(FeignException.class);
thrown.expectMessage("status 500 reading TestInterface#get(); content:\n" + "ARGHH");
thrown.expectMessage("status 500 reading TestInterface#get()");

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

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

@Test
public void parsesErrorResponseBody() {
String expectedResponseBody = "ARGHH";

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

TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());

try {
api.get();
} catch (FeignException e) {
assertThat(e.contentUTF8()).isEqualTo(expectedResponseBody);
}
}

@Test
public void safeRebuffering() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("foo"));
Expand Down
10 changes: 6 additions & 4 deletions core/src/test/java/feign/codec/DefaultErrorDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public void throwsFeignException() throws Throwable {

@Test
public void throwsFeignExceptionIncludingBody() throws Throwable {
thrown.expect(FeignException.class);
thrown.expectMessage("status 500 reading Service#foo(); content:\nhello world");

Response response = Response.builder()
.status(500)
.reason("Internal server error")
Expand All @@ -66,7 +63,12 @@ public void throwsFeignExceptionIncludingBody() throws Throwable {
.body("hello world", UTF_8)
.build();

throw errorDecoder.decode("Service#foo()", response);
try {
throw errorDecoder.decode("Service#foo()", response);
} catch (FeignException e) {
assertThat(e.getMessage()).isEqualTo("status 500 reading Service#foo()");
assertThat(e.contentUTF8()).isEqualTo("hello world");
}
}

@Test
Expand Down