Skip to content

Commit c4f3f4e

Browse files
committed
More useful message for NO_DEFAULT exception out of the box.
1 parent 1cd3315 commit c4f3f4e

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/main/java/feign/error/ErrorHandling.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package feign.error;
1515

16+
import feign.Response;
17+
1618
import java.lang.annotation.ElementType;
1719
import java.lang.annotation.Inherited;
1820
import java.lang.annotation.Retention;
@@ -28,5 +30,18 @@
2830
Class<? extends Exception> defaultException() default NO_DEFAULT.class;
2931

3032
final class NO_DEFAULT extends Exception {
33+
34+
/**
35+
* For backward compatibility.
36+
*/
37+
@Deprecated
38+
public NO_DEFAULT() {
39+
}
40+
41+
@FeignExceptionConstructor
42+
public NO_DEFAULT(@ResponseBody Response response) {
43+
super("Endpoint responded with " + response.status() + ", reason: " + response.reason());
44+
}
45+
3146
}
3247
}

src/test/java/feign/error/AnnotationErrorDecoderExceptionConstructorsTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public static Iterable<Object[]> data() {
124124

125125
@Test
126126
public void test() throws Exception {
127-
128127
AnnotationErrorDecoder decoder = AnnotationErrorDecoder
129128
.builderFor(TestClientInterfaceWithDifferentExceptionConstructors.class)
130129
.withResponseBodyDecoder(new OptionalDecoder(new Decoder.Default()))
@@ -133,13 +132,26 @@ public void test() throws Exception {
133132
Exception genericException = decoder.decode(feignConfigKey("method1Test"),
134133
testResponse(errorCode, NON_NULL_BODY, NON_NULL_HEADERS));
135134

136-
assertThat(genericException.getClass()).isEqualTo(expectedExceptionClass);
135+
assertThat(genericException).isInstanceOf(expectedExceptionClass);
137136

138137
ParametersException exception = (ParametersException) genericException;
139138
assertThat(exception.body()).isEqualTo(expectedBody);
140139
assertThat(exception.headers()).isEqualTo(expectedHeaders);
140+
}
141+
142+
@Test
143+
public void testIfExceptionIsNotInTheList() throws Exception {
144+
AnnotationErrorDecoder decoder = AnnotationErrorDecoder
145+
.builderFor(TestClientInterfaceWithDifferentExceptionConstructors.class)
146+
.withResponseBodyDecoder(new OptionalDecoder(new Decoder.Default()))
147+
.build();
141148

149+
Exception genericException = decoder.decode(feignConfigKey("method1Test"),
150+
testResponse(-1, NON_NULL_BODY, NON_NULL_HEADERS));
142151

152+
assertThat(genericException)
153+
.isInstanceOf(ErrorHandling.NO_DEFAULT.class)
154+
.hasMessage("Endpoint responded with -1, reason: null");
143155
}
144156

145157
interface TestClientInterfaceWithDifferentExceptionConstructors {

0 commit comments

Comments
 (0)