Skip to content
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
45 changes: 33 additions & 12 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static feign.Util.UTF_8;
Expand All @@ -37,7 +39,7 @@ public class FeignException extends RuntimeException {
"request should not be null";
private static final long serialVersionUID = 0;
private int status;
private byte[] content;
private byte[] responseBody;
private Request request;

protected FeignException(int status, String message, Throwable cause) {
Expand All @@ -46,10 +48,10 @@ protected FeignException(int status, String message, Throwable cause) {
this.request = null;
}

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

Expand All @@ -59,10 +61,10 @@ protected FeignException(int status, String message) {
this.request = null;
}

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

Expand All @@ -73,10 +75,10 @@ protected FeignException(int status, String message, Request request, Throwable
}

protected FeignException(int status, String message, Request request, Throwable cause,
byte[] content) {
byte[] responseBody) {
super(message, cause);
this.status = status;
this.content = content;
this.responseBody = responseBody;
this.request = checkRequestNotNull(request);
}

Expand All @@ -86,10 +88,10 @@ protected FeignException(int status, String message, Request request) {
this.request = checkRequestNotNull(request);
}

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

Expand All @@ -101,8 +103,27 @@ public int status() {
return this.status;
}

/**
* The Response Body, if present.
*
* @return the body of the response.
* @deprecated use {@link #responseBody()} instead.
*/
@Deprecated
public byte[] content() {
return this.content;
return this.responseBody;
}

/**
* The Response body.
*
* @return an Optional wrapping the response body.
*/
public Optional<ByteBuffer> responseBody() {
if (this.responseBody == null) {
return Optional.empty();
}
return Optional.of(ByteBuffer.wrap(this.responseBody));
}

public Request request() {
Expand All @@ -114,8 +135,8 @@ public boolean hasRequest() {
}

public String contentUTF8() {
if (content != null) {
return new String(content, UTF_8);
if (responseBody != null) {
return new String(responseBody, UTF_8);
} else {
return "";
}
Expand Down
41 changes: 41 additions & 0 deletions core/src/test/java/feign/FeignExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,50 @@
package feign;

import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;

public class FeignExceptionTest {

@Test
public void canCreateWithRequestAndResponse() {
Request request = Request.create(Request.HttpMethod.GET,
"/home", Collections.emptyMap(),
"data".getBytes(StandardCharsets.UTF_8),
StandardCharsets.UTF_8,
null);

Response response = Response.builder()
.status(400)
.body("response".getBytes(StandardCharsets.UTF_8))
.request(request)
.build();

FeignException exception =
FeignException.errorReading(request, response, new IOException("socket closed"));
assertThat(exception.responseBody()).isNotEmpty();
assertThat(exception.hasRequest()).isTrue();
assertThat(exception.request()).isNotNull();
}

@Test
public void canCreateWithRequestOnly() {
Request request = Request.create(Request.HttpMethod.GET,
"/home", Collections.emptyMap(),
"data".getBytes(StandardCharsets.UTF_8),
StandardCharsets.UTF_8,
null);

FeignException exception =
FeignException.errorExecuting(request, new IOException("connection timeout"));
assertThat(exception.responseBody()).isEmpty();
assertThat(exception.content()).isNullOrEmpty();
assertThat(exception.hasRequest()).isTrue();
assertThat(exception.request()).isNotNull();
}

@Test(expected = NullPointerException.class)
public void nullRequestShouldThrowNPEwThrowable() {
new Derived(404, "message", null, new Throwable());
Expand Down