Skip to content

Commit 274b2a8

Browse files
FrEaKmAnAdrian Cole
authored andcommitted
Added HTTP status to FeignException
1 parent 02a1e8c commit 274b2a8

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Version 8.11
2+
* Adds HTTP status to FeignException for easier response handling
3+
14
### Version 8.10
25
* Reads class-level @Produces/@Consumes JAX-RS annotations
36
* Supports POST without a body parameter

core/src/main/java/feign/FeignException.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public class FeignException extends RuntimeException {
2626

2727
private static final long serialVersionUID = 0;
28+
private int status;
2829

2930
protected FeignException(String message, Throwable cause) {
3031
super(message, cause);
@@ -34,6 +35,15 @@ protected FeignException(String message) {
3435
super(message);
3536
}
3637

38+
protected FeignException(int status, String message) {
39+
super(message);
40+
this.status = status;
41+
}
42+
43+
public int status() {
44+
return this.status;
45+
}
46+
3747
static FeignException errorReading(Request request, Response ignored, IOException cause) {
3848
return new FeignException(
3949
format("%s reading %s %s", cause.getMessage(), request.method(), request.url()),
@@ -49,7 +59,7 @@ public static FeignException errorStatus(String methodKey, Response response) {
4959
}
5060
} catch (IOException ignored) { // NOPMD
5161
}
52-
return new FeignException(message);
62+
return new FeignException(response.status(), message);
5363
}
5464

5565
static FeignException errorExecuting(Request request, IOException cause) {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static feign.Util.RETRY_AFTER;
3131
import static feign.Util.UTF_8;
32+
import static org.assertj.core.api.Assertions.assertThat;
3233

3334
public class DefaultErrorDecoderTest {
3435

@@ -54,13 +55,21 @@ public void throwsFeignExceptionIncludingBody() throws Throwable {
5455
thrown.expect(FeignException.class);
5556
thrown.expectMessage("status 500 reading Service#foo(); content:\nhello world");
5657

57-
Response
58-
response =
59-
Response.create(500, "Internal server error", headers, "hello world", UTF_8);
58+
Response response = Response.create(500, "Internal server error", headers, "hello world", UTF_8);
6059

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

63+
@Test
64+
public void testFeignExceptionIncludesStatus() throws Throwable {
65+
Response response = Response.create(400, "Bad request", headers, (byte[]) null);
66+
67+
Exception exception = errorDecoder.decode("Service#foo()", response);
68+
69+
assertThat(exception).isInstanceOf(FeignException.class);
70+
assertThat(((FeignException) exception).status()).isEqualTo(400);
71+
}
72+
6473
@Test
6574
public void retryAfterHeaderThrowsRetryableException() throws Throwable {
6675
thrown.expect(FeignException.class);

0 commit comments

Comments
 (0)