Skip to content

Commit eb77215

Browse files
authored
Merge pull request #26 from SimY4/master
More useful message for NO_DEFAULT exception out of the box.
2 parents 1cd3315 + 4bf1b55 commit eb77215

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

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

16+
import feign.Response;
1617
import java.lang.annotation.ElementType;
1718
import java.lang.annotation.Inherited;
1819
import java.lang.annotation.Retention;
@@ -28,5 +29,9 @@
2829
Class<? extends Exception> defaultException() default NO_DEFAULT.class;
2930

3031
final class NO_DEFAULT extends Exception {
32+
@FeignExceptionConstructor
33+
public NO_DEFAULT(@ResponseBody Response response) {
34+
super("Endpoint responded with " + response.status() + ", reason: " + response.reason());
35+
}
3136
}
3237
}

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+
}
141141

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();
148+
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)