Skip to content

Commit 7d4eb18

Browse files
authored
Allow override of maxBodyBytesLength in ErrorDecoder (#2113)
* Allow override of maxBodyBytesLength in ErrorDecoder * Apply linter * Choose constructor approach * Reformat
1 parent 0868e71 commit 7d4eb18

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,26 @@ public interface ErrorDecoder {
8383
*/
8484
public Exception decode(String methodKey, Response response);
8585

86-
public static class Default implements ErrorDecoder {
86+
public class Default implements ErrorDecoder {
8787

8888
private final RetryAfterDecoder retryAfterDecoder = new RetryAfterDecoder();
89+
private Integer maxBodyBytesLength;
90+
private Integer maxBodyCharsLength;
91+
92+
public Default() {
93+
this.maxBodyBytesLength = null;
94+
this.maxBodyCharsLength = null;
95+
}
96+
97+
public Default(Integer maxBodyBytesLength, Integer maxBodyCharsLength) {
98+
this.maxBodyBytesLength = maxBodyBytesLength;
99+
this.maxBodyCharsLength = maxBodyCharsLength;
100+
}
89101

90102
@Override
91103
public Exception decode(String methodKey, Response response) {
92-
FeignException exception = errorStatus(methodKey, response);
104+
FeignException exception = errorStatus(methodKey, response, maxBodyBytesLength,
105+
maxBodyCharsLength);
93106
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER));
94107
if (retryAfter != null) {
95108
return new RetryableException(

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import feign.Request.HttpMethod;
2222
import feign.Response;
2323
import feign.Util;
24+
import java.io.ByteArrayInputStream;
25+
import java.io.InputStream;
2426
import java.util.Collection;
2527
import java.util.Collections;
28+
import java.util.HashMap;
2629
import java.util.LinkedHashMap;
2730
import java.util.Map;
2831
import org.junit.Rule;
@@ -135,4 +138,53 @@ public void retryAfterHeaderThrowsRetryableException() throws Throwable {
135138

136139
throw errorDecoder.decode("Service#foo()", response);
137140
}
141+
142+
@Test
143+
public void lengthOfBodyExceptionTest() {
144+
Response response = bigBodyResponse();
145+
Exception defaultException = errorDecoder.decode("Service#foo()", response);
146+
assertThat(defaultException.getMessage().length()).isLessThan(response.body().length());
147+
148+
ErrorDecoder customizedErrorDecoder = new ErrorDecoder.Default(4000, 2000);
149+
Exception customizedException = customizedErrorDecoder.decode("Service#foo()", response);
150+
assertThat(customizedException.getMessage().length())
151+
.isGreaterThanOrEqualTo(response.body().length());
152+
}
153+
154+
private Response bigBodyResponse() {
155+
String content = "I love a storm in early May\n" +
156+
"When springtime’s boisterous, firstborn thunder\n" +
157+
"Over the sky will gaily wander\n" +
158+
"And growl and roar as though in play.\n" +
159+
"\n" +
160+
"A peal, another — gleeful, cheering…\n" +
161+
"Rain, raindust… On the trees, behold!-\n" +
162+
"The drops hang, each a long pearl earring;\n" +
163+
"Bright sunshine paints the thin threads gold.\n" +
164+
"\n" +
165+
"A stream downhill goes rushing reckless,\n" +
166+
"And in the woods the birds rejoice.\n" +
167+
"Din. Clamour. Noise. All nature echoes\n" +
168+
"The thunder’s youthful, merry voice.\n" +
169+
"\n" +
170+
"You’ll say: ‘Tis laughing, carefree Hebe —\n" +
171+
"She fed her father’s eagle, and\n" +
172+
"The Storm Cup brimming with a seething\n" +
173+
"And bubbling wine dropped from her hand";
174+
175+
InputStream inputStream = new ByteArrayInputStream(content.getBytes(UTF_8));
176+
Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
177+
headers.put("Content-Type", Collections.singleton("text/plain"));
178+
return Response.builder()
179+
.status(400)
180+
.request(Request.create(
181+
Request.HttpMethod.GET,
182+
"/home",
183+
Collections.emptyMap(),
184+
"data".getBytes(Util.UTF_8),
185+
Util.UTF_8,
186+
null))
187+
.body(content, Util.UTF_8)
188+
.build();
189+
}
138190
}

0 commit comments

Comments
 (0)