Skip to content

Commit

Permalink
Make status mandatory (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
velo authored and kdavisk6 committed Feb 1, 2019
1 parent 163740c commit 148aaee
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 28 deletions.
13 changes: 9 additions & 4 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ public class FeignException extends RuntimeException {
private int status;
private byte[] content;

protected FeignException(String message, Throwable cause) {
protected FeignException(int status, String message, Throwable cause) {
super(message, cause);
this.status = status;
}

protected FeignException(String message, Throwable cause, byte[] content) {
protected FeignException(int status, String message, Throwable cause, byte[] content) {
super(message, cause);
this.status = status;
this.content = content;
}

protected FeignException(String message) {
protected FeignException(int status, String message) {
super(message);
this.status = status;
}

protected FeignException(int status, String message, byte[] content) {
Expand All @@ -56,8 +59,9 @@ public String contentUTF8() {
return new String(content, UTF_8);
}

static FeignException errorReading(Request request, Response ignored, IOException cause) {
static FeignException errorReading(Request request, Response response, IOException cause) {
return new FeignException(
response.status(),
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
cause,
request.body());
Expand Down Expand Up @@ -118,6 +122,7 @@ private static FeignException errorStatus(int status, String message, byte[] bod

static FeignException errorExecuting(Request request, IOException cause) {
return new RetryableException(
-1,
format("%s executing %s %s", cause.getMessage(), request.httpMethod(), request.url()),
request.httpMethod(),
cause,
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/feign/RetryableException.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public class RetryableException extends FeignException {
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
*/
public RetryableException(
String message, HttpMethod httpMethod, Throwable cause, Date retryAfter) {
super(message, cause);
int status, String message, HttpMethod httpMethod, Throwable cause, Date retryAfter) {
super(status, message, cause);
this.httpMethod = httpMethod;
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
}

/**
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
*/
public RetryableException(String message, HttpMethod httpMethod, Date retryAfter) {
super(message);
public RetryableException(int status, String message, HttpMethod httpMethod, Date retryAfter) {
super(status, message);
this.httpMethod = httpMethod;
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/feign/SynchronousMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Object decode(Response response) throws Throwable {
} catch (FeignException e) {
throw e;
} catch (RuntimeException e) {
throw new DecodeException(e.getMessage(), e);
throw new DecodeException(response.status(), e.getMessage(), e);
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/feign/codec/DecodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class DecodeException extends FeignException {
/**
* @param message the reason for the failure.
*/
public DecodeException(String message) {
super(checkNotNull(message, "message"));
public DecodeException(int status, String message) {
super(status, checkNotNull(message, "message"));
}

/**
* @param message possibly null reason for the failure.
* @param cause the cause of the error.
*/
public DecodeException(String message, Throwable cause) {
super(message, checkNotNull(cause, "cause"));
public DecodeException(int status, String message, Throwable cause) {
super(status, message, checkNotNull(cause, "cause"));
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/feign/codec/EncodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public class EncodeException extends FeignException {
* @param message the reason for the failure.
*/
public EncodeException(String message) {
super(checkNotNull(message, "message"));
super(-1, checkNotNull(message, "message"));
}

/**
* @param message possibly null reason for the failure.
* @param cause the cause of the error.
*/
public EncodeException(String message, Throwable cause) {
super(message, checkNotNull(cause, "cause"));
super(-1, message, checkNotNull(cause, "cause"));
}
}
6 changes: 5 additions & 1 deletion core/src/main/java/feign/codec/ErrorDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public Exception decode(String methodKey, Response response) {
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER));
if (retryAfter != null) {
return new RetryableException(
exception.getMessage(), response.request().httpMethod(), exception, retryAfter);
response.status(),
exception.getMessage(),
response.request().httpMethod(),
exception,
retryAfter);
}
return exception;
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/feign/codec/StringDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public Object decode(Response response, Type type) throws IOException {
if (String.class.equals(type)) {
return Util.toString(body.asReader());
}
throw new DecodeException(format("%s is not a type supported by this decoder.", type));
throw new DecodeException(
response.status(), format("%s is not a type supported by this decoder.", type));
}
}
10 changes: 7 additions & 3 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ public void retryableExceptionInDecoder() throws Exception {
public Object decode(Response response, Type type) throws IOException {
String string = super.decode(response, type).toString();
if ("retry!".equals(string)) {
throw new RetryableException(string, HttpMethod.POST, null);
throw new RetryableException(
response.status(), string, HttpMethod.POST, null);
}
return string;
}
Expand Down Expand Up @@ -545,7 +546,8 @@ public void ensureRetryerClonesItself() throws Exception {
new ErrorDecoder() {
@Override
public Exception decode(String methodKey, Response response) {
return new RetryableException("play it again sam!", HttpMethod.POST, null);
return new RetryableException(
response.status(), "play it again sam!", HttpMethod.POST, null);
}
})
.target(TestInterface.class, "http://localhost:" + server.getPort());
Expand Down Expand Up @@ -573,6 +575,7 @@ public void throwsOriginalExceptionAfterFailedRetries() throws Exception {
@Override
public Exception decode(String methodKey, Response response) {
return new RetryableException(
response.status(),
"play it again sam!",
HttpMethod.POST,
new TestInterfaceException(message),
Expand Down Expand Up @@ -601,7 +604,8 @@ public void throwsRetryableExceptionIfNoUnderlyingCause() throws Exception {
new ErrorDecoder() {
@Override
public Exception decode(String methodKey, Response response) {
return new RetryableException(message, HttpMethod.POST, null);
return new RetryableException(
response.status(), message, HttpMethod.POST, null);
}
})
.target(TestInterface.class, "http://localhost:" + server.getPort());
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/feign/RetryerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RetryerTest {

@Test
public void only5TriesAllowedAndExponentialBackoff() throws Exception {
RetryableException e = new RetryableException(null, null, null);
RetryableException e = new RetryableException(-1, null, null, null);
Default retryer = new Retryer.Default();
assertEquals(1, retryer.attempt);
assertEquals(0, retryer.sleptForMillis);
Expand Down Expand Up @@ -62,14 +62,14 @@ protected long currentTimeMillis() {
}
};

retryer.continueOrPropagate(new RetryableException(null, null, new Date(5000)));
retryer.continueOrPropagate(new RetryableException(-1, null, null, new Date(5000)));
assertEquals(2, retryer.attempt);
assertEquals(1000, retryer.sleptForMillis);
}

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

@Test
Expand All @@ -78,7 +78,7 @@ public void defaultRetryerFailsOnInterruptedException() {

Thread.currentThread().interrupt();
RetryableException expected =
new RetryableException(null, null, new Date(System.currentTimeMillis() + 5000));
new RetryableException(-1, null, null, new Date(System.currentTimeMillis() + 5000));
try {
retryer.continueOrPropagate(expected);
Thread.interrupted(); // reset interrupted flag in case it wasn't
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public boolean hasNext() {
current = objectReader.readValue(parser);
} catch (IOException e) {
// Input Stream closed automatically by parser
throw new DecodeException(e.getMessage(), e);
throw new DecodeException(response.status(), e.getMessage(), e);
}
return current != null;
}
Expand Down
2 changes: 1 addition & 1 deletion jaxb/src/main/java/feign/jaxb/JAXBDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Object decode(Response response, Type type) throws IOException {
saxParserFactory.newSAXParser().getXMLReader(),
new InputSource(response.body().asInputStream())));
} catch (JAXBException | ParserConfigurationException | SAXException e) {
throw new DecodeException(e.toString(), e);
throw new DecodeException(response.status(), e.toString(), e);
} finally {
if (response.body() != null) {
response.body().close();
Expand Down
2 changes: 1 addition & 1 deletion sax/src/main/java/feign/sax/SAXDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc
}
return handler.result();
} catch (SAXException e) {
throw new DecodeException(e.getMessage(), e);
throw new DecodeException(response.status(), e.getMessage(), e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion soap/src/main/java/feign/soap/SOAPDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Object decode(Response response, Type type) throws IOException {
.unmarshal(message.getSOAPBody().extractContentAsDocument());
}
} catch (SOAPException | JAXBException e) {
throw new DecodeException(e.toString(), e);
throw new DecodeException(response.status(), e.toString(), e);
} finally {
if (response.body() != null) {
response.body().close();
Expand Down

0 comments on commit 148aaee

Please sign in to comment.