Browse Source

Allow override of maxBodyBytesLength in ErrorDecoder (#2113)

* Allow override of maxBodyBytesLength in ErrorDecoder

* Apply linter

* Choose constructor approach

* Reformat
pull/2121/head
Dušan Plavák 1 year ago committed by GitHub
parent
commit
7d4eb18cf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      core/src/main/java/feign/codec/ErrorDecoder.java
  2. 52
      core/src/test/java/feign/codec/DefaultErrorDecoderTest.java

17
core/src/main/java/feign/codec/ErrorDecoder.java

@ -83,13 +83,26 @@ public interface ErrorDecoder { @@ -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(

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

@ -21,8 +21,11 @@ import feign.Request; @@ -21,8 +21,11 @@ import feign.Request;
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;
@ -135,4 +138,53 @@ public class DefaultErrorDecoderTest { @@ -135,4 +138,53 @@ public class DefaultErrorDecoderTest {
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();
}
}

Loading…
Cancel
Save