Skip to content

Commit bdb4613

Browse files
velokdavisk6
authored andcommitted
Make status mandatory (OpenFeign#880)
1 parent 29935b2 commit bdb4613

13 files changed

Lines changed: 44 additions & 37 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ public class FeignException extends RuntimeException {
2626
private int status;
2727
private byte[] content;
2828

29-
protected FeignException(String message, Throwable cause) {
29+
protected FeignException(int status, String message, Throwable cause) {
3030
super(message, cause);
31+
this.status = status;
3132
}
3233

33-
protected FeignException(String message, Throwable cause, byte[] content) {
34+
protected FeignException(int status, String message, Throwable cause, byte[] content) {
3435
super(message, cause);
36+
this.status = status;
3537
this.content = content;
3638
}
3739

38-
protected FeignException(String message) {
40+
protected FeignException(int status, String message) {
3941
super(message);
42+
this.status = status;
4043
}
4144

4245
protected FeignException(int status, String message, byte[] content) {
@@ -57,8 +60,9 @@ public String contentUTF8() {
5760
return new String(content, UTF_8);
5861
}
5962

60-
static FeignException errorReading(Request request, Response ignored, IOException cause) {
63+
static FeignException errorReading(Request request, Response response, IOException cause) {
6164
return new FeignException(
65+
response.status(),
6266
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
6367
cause,
6468
request.body());
@@ -119,6 +123,7 @@ private static FeignException errorStatus(int status, String message, byte[] bod
119123

120124
static FeignException errorExecuting(Request request, IOException cause) {
121125
return new RetryableException(
126+
-1,
122127
format("%s executing %s %s", cause.getMessage(), request.httpMethod(), request.url()),
123128
request.httpMethod(),
124129
cause,

core/src/main/java/feign/RetryableException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public class RetryableException extends FeignException {
3030
/**
3131
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
3232
*/
33-
public RetryableException(String message, HttpMethod httpMethod, Throwable cause,
33+
public RetryableException(int status, String message, HttpMethod httpMethod, Throwable cause,
3434
Date retryAfter) {
35-
super(message, cause);
35+
super(status, message, cause);
3636
this.httpMethod = httpMethod;
3737
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
3838
}
3939

4040
/**
4141
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
4242
*/
43-
public RetryableException(String message, HttpMethod httpMethod, Date retryAfter) {
44-
super(message);
43+
public RetryableException(int status, String message, HttpMethod httpMethod, Date retryAfter) {
44+
super(status, message);
4545
this.httpMethod = httpMethod;
4646
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
4747
}

core/src/main/java/feign/SynchronousMethodHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Object decode(Response response) throws Throwable {
177177
} catch (FeignException e) {
178178
throw e;
179179
} catch (RuntimeException e) {
180-
throw new DecodeException(e.getMessage(), e);
180+
throw new DecodeException(response.status(), e.getMessage(), e);
181181
}
182182
}
183183

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public class DecodeException extends FeignException {
2828
/**
2929
* @param message the reason for the failure.
3030
*/
31-
public DecodeException(String message) {
32-
super(checkNotNull(message, "message"));
31+
public DecodeException(int status, String message) {
32+
super(status, checkNotNull(message, "message"));
3333
}
3434

3535
/**
3636
* @param message possibly null reason for the failure.
3737
* @param cause the cause of the error.
3838
*/
39-
public DecodeException(String message, Throwable cause) {
40-
super(message, checkNotNull(cause, "cause"));
39+
public DecodeException(int status, String message, Throwable cause) {
40+
super(status, message, checkNotNull(cause, "cause"));
4141
}
4242
}

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

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

16-
import feign.FeignException;
1716
import static feign.Util.checkNotNull;
17+
import feign.FeignException;
1818

1919
/**
2020
* Similar to {@code javax.websocket.EncodeException}, raised when a problem occurs encoding a
@@ -29,14 +29,14 @@ public class EncodeException extends FeignException {
2929
* @param message the reason for the failure.
3030
*/
3131
public EncodeException(String message) {
32-
super(checkNotNull(message, "message"));
32+
super(-1, checkNotNull(message, "message"));
3333
}
3434

3535
/**
3636
* @param message possibly null reason for the failure.
3737
* @param cause the cause of the error.
3838
*/
3939
public EncodeException(String message, Throwable cause) {
40-
super(message, checkNotNull(cause, "cause"));
40+
super(-1, message, checkNotNull(cause, "cause"));
4141
}
4242
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* <p/>
3737
* Ex:
38-
*
38+
*
3939
* <pre>
4040
* class IllegalArgumentExceptionOn404Decoder implements ErrorDecoder {
4141
*
@@ -94,6 +94,7 @@ public Exception decode(String methodKey, Response response) {
9494
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER));
9595
if (retryAfter != null) {
9696
return new RetryableException(
97+
response.status(),
9798
exception.getMessage(),
9899
response.request().httpMethod(),
99100
exception,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public Object decode(Response response, Type type) throws IOException {
3030
if (String.class.equals(type)) {
3131
return Util.toString(body.asReader());
3232
}
33-
throw new DecodeException(format("%s is not a type supported by this decoder.", type));
33+
throw new DecodeException(response.status(),
34+
format("%s is not a type supported by this decoder.", type));
3435
}
3536
}

core/src/test/java/feign/FeignTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public void retryableExceptionInDecoder() throws Exception {
480480
public Object decode(Response response, Type type) throws IOException {
481481
String string = super.decode(response, type).toString();
482482
if ("retry!".equals(string)) {
483-
throw new RetryableException(string, HttpMethod.POST, null);
483+
throw new RetryableException(response.status(), string, HttpMethod.POST, null);
484484
}
485485
return string;
486486
}
@@ -540,7 +540,7 @@ public void ensureRetryerClonesItself() throws Exception {
540540
.errorDecoder(new ErrorDecoder() {
541541
@Override
542542
public Exception decode(String methodKey, Response response) {
543-
return new RetryableException("play it again sam!", HttpMethod.POST, null);
543+
return new RetryableException(response.status(), "play it again sam!", HttpMethod.POST, null);
544544
}
545545
}).target(TestInterface.class, "http://localhost:" + server.getPort());
546546

@@ -564,7 +564,7 @@ public void throwsOriginalExceptionAfterFailedRetries() throws Exception {
564564
.errorDecoder(new ErrorDecoder() {
565565
@Override
566566
public Exception decode(String methodKey, Response response) {
567-
return new RetryableException("play it again sam!", HttpMethod.POST,
567+
return new RetryableException(response.status(), "play it again sam!", HttpMethod.POST,
568568
new TestInterfaceException(message), null);
569569
}
570570
}).target(TestInterface.class, "http://localhost:" + server.getPort());
@@ -587,7 +587,7 @@ public void throwsRetryableExceptionIfNoUnderlyingCause() throws Exception {
587587
.errorDecoder(new ErrorDecoder() {
588588
@Override
589589
public Exception decode(String methodKey, Response response) {
590-
return new RetryableException(message, HttpMethod.POST, null);
590+
return new RetryableException(response.status(), message, HttpMethod.POST, null);
591591
}
592592
}).target(TestInterface.class, "http://localhost:" + server.getPort());
593593

core/src/test/java/feign/RetryerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class RetryerTest {
2828

2929
@Test
3030
public void only5TriesAllowedAndExponentialBackoff() throws Exception {
31-
RetryableException e = new RetryableException(null, null, null);
31+
RetryableException e = new RetryableException(-1, null, null, null);
3232
Default retryer = new Retryer.Default();
3333
assertEquals(1, retryer.attempt);
3434
assertEquals(0, retryer.sleptForMillis);
@@ -61,14 +61,14 @@ protected long currentTimeMillis() {
6161
}
6262
};
6363

64-
retryer.continueOrPropagate(new RetryableException(null, null, new Date(5000)));
64+
retryer.continueOrPropagate(new RetryableException(-1, null, null, new Date(5000)));
6565
assertEquals(2, retryer.attempt);
6666
assertEquals(1000, retryer.sleptForMillis);
6767
}
6868

6969
@Test(expected = RetryableException.class)
7070
public void neverRetryAlwaysPropagates() {
71-
Retryer.NEVER_RETRY.continueOrPropagate(new RetryableException(null, null, new Date(5000)));
71+
Retryer.NEVER_RETRY.continueOrPropagate(new RetryableException(-1, null, null, new Date(5000)));
7272
}
7373

7474
@Test
@@ -77,7 +77,7 @@ public void defaultRetryerFailsOnInterruptedException() {
7777

7878
Thread.currentThread().interrupt();
7979
RetryableException expected =
80-
new RetryableException(null, null, new Date(System.currentTimeMillis() + 5000));
80+
new RetryableException(-1, null, null, new Date(System.currentTimeMillis() + 5000));
8181
try {
8282
retryer.continueOrPropagate(expected);
8383
Thread.interrupted(); // reset interrupted flag in case it wasn't

jackson/src/main/java/feign/jackson/JacksonIteratorDecoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* <p>
4444
* <p>
4545
* Example: <br>
46-
*
46+
*
4747
* <pre>
4848
* <code>
4949
* Feign.builder()
@@ -152,7 +152,7 @@ public boolean hasNext() {
152152
current = objectReader.readValue(parser);
153153
} catch (IOException e) {
154154
// Input Stream closed automatically by parser
155-
throw new DecodeException(e.getMessage(), e);
155+
throw new DecodeException(response.status(), e.getMessage(), e);
156156
}
157157
return current != null;
158158
}

0 commit comments

Comments
 (0)