Skip to content

Commit

Permalink
Allow override of maxBodyBytesLength in ErrorDecoder (#2113)
Browse files Browse the repository at this point in the history
* Allow override of maxBodyBytesLength in ErrorDecoder

* Apply linter

* Choose constructor approach

* Reformat
  • Loading branch information
ddeath authored Jul 5, 2023
1 parent 0868e71 commit 7d4eb18
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
17 changes: 15 additions & 2 deletions core/src/main/java/feign/codec/ErrorDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,26 @@ public interface ErrorDecoder {
*/
public Exception decode(String methodKey, Response response);

public static class Default implements ErrorDecoder {
public class Default implements ErrorDecoder {

private final RetryAfterDecoder retryAfterDecoder = new RetryAfterDecoder();
private Integer maxBodyBytesLength;
private Integer maxBodyCharsLength;

public Default() {
this.maxBodyBytesLength = null;
this.maxBodyCharsLength = null;
}

public Default(Integer maxBodyBytesLength, Integer maxBodyCharsLength) {
this.maxBodyBytesLength = maxBodyBytesLength;
this.maxBodyCharsLength = maxBodyCharsLength;
}

@Override
public Exception decode(String methodKey, Response response) {
FeignException exception = errorStatus(methodKey, response);
FeignException exception = errorStatus(methodKey, response, maxBodyBytesLength,
maxBodyCharsLength);
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER));
if (retryAfter != null) {
return new RetryableException(
Expand Down
52 changes: 52 additions & 0 deletions core/src/test/java/feign/codec/DefaultErrorDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import feign.Request.HttpMethod;
import feign.Response;
import feign.Util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Rule;
Expand Down Expand Up @@ -135,4 +138,53 @@ public void retryAfterHeaderThrowsRetryableException() throws Throwable {

throw errorDecoder.decode("Service#foo()", response);
}

@Test
public void lengthOfBodyExceptionTest() {
Response response = bigBodyResponse();
Exception defaultException = errorDecoder.decode("Service#foo()", response);
assertThat(defaultException.getMessage().length()).isLessThan(response.body().length());

ErrorDecoder customizedErrorDecoder = new ErrorDecoder.Default(4000, 2000);
Exception customizedException = customizedErrorDecoder.decode("Service#foo()", response);
assertThat(customizedException.getMessage().length())
.isGreaterThanOrEqualTo(response.body().length());
}

private Response bigBodyResponse() {
String content = "I love a storm in early May\n" +
"When springtime’s boisterous, firstborn thunder\n" +
"Over the sky will gaily wander\n" +
"And growl and roar as though in play.\n" +
"\n" +
"A peal, another — gleeful, cheering…\n" +
"Rain, raindust… On the trees, behold!-\n" +
"The drops hang, each a long pearl earring;\n" +
"Bright sunshine paints the thin threads gold.\n" +
"\n" +
"A stream downhill goes rushing reckless,\n" +
"And in the woods the birds rejoice.\n" +
"Din. Clamour. Noise. All nature echoes\n" +
"The thunder’s youthful, merry voice.\n" +
"\n" +
"You’ll say: ‘Tis laughing, carefree Hebe —\n" +
"She fed her father’s eagle, and\n" +
"The Storm Cup brimming with a seething\n" +
"And bubbling wine dropped from her hand";

InputStream inputStream = new ByteArrayInputStream(content.getBytes(UTF_8));
Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
headers.put("Content-Type", Collections.singleton("text/plain"));
return Response.builder()
.status(400)
.request(Request.create(
Request.HttpMethod.GET,
"/home",
Collections.emptyMap(),
"data".getBytes(Util.UTF_8),
Util.UTF_8,
null))
.body(content, Util.UTF_8)
.build();
}
}

0 comments on commit 7d4eb18

Please sign in to comment.