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 5d12416 commit b03e4e8
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 37 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 @@ -26,17 +26,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 @@ -57,8 +60,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 @@ -119,6 +123,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 @@ -30,18 +30,18 @@ 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,
public RetryableException(int status, String message, HttpMethod httpMethod, Throwable cause,
Date retryAfter) {
super(message, cause);
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 @@ -177,7 +177,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 @@ -28,15 +28,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"));
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/feign/codec/EncodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
package feign.codec;

import feign.FeignException;
import static feign.Util.checkNotNull;
import feign.FeignException;

/**
* Similar to {@code javax.websocket.EncodeException}, raised when a problem occurs encoding a
Expand All @@ -29,14 +29,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"));
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/feign/codec/ErrorDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* <p/>
* Ex:
*
*
* <pre>
* class IllegalArgumentExceptionOn404Decoder implements ErrorDecoder {
*
Expand Down Expand Up @@ -94,6 +94,7 @@ public Exception decode(String methodKey, Response response) {
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER));
if (retryAfter != null) {
return new RetryableException(
response.status(),
exception.getMessage(),
response.request().httpMethod(),
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 @@ -30,6 +30,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));
}
}
8 changes: 4 additions & 4 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ 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 @@ -540,7 +540,7 @@ public void ensureRetryerClonesItself() throws Exception {
.errorDecoder(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 All @@ -564,7 +564,7 @@ public void throwsOriginalExceptionAfterFailedRetries() throws Exception {
.errorDecoder(new ErrorDecoder() {
@Override
public Exception decode(String methodKey, Response response) {
return new RetryableException("play it again sam!", HttpMethod.POST,
return new RetryableException(response.status(), "play it again sam!", HttpMethod.POST,
new TestInterfaceException(message), null);
}
}).target(TestInterface.class, "http://localhost:" + server.getPort());
Expand All @@ -587,7 +587,7 @@ public void throwsRetryableExceptionIfNoUnderlyingCause() throws Exception {
.errorDecoder(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 @@ -61,14 +61,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 @@ -77,7 +77,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 @@ -43,7 +43,7 @@
* <p>
* <p>
* Example: <br>
*
*
* <pre>
* <code>
* Feign.builder()
Expand Down Expand Up @@ -152,7 +152,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
6 changes: 3 additions & 3 deletions jaxb/src/main/java/feign/jaxb/JAXBDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
* <p>
* Basic example with with Feign.Builder:
* </p>
*
*
* <pre>
* JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
* .withMarshallerJAXBEncoding(&quot;UTF-8&quot;)
* .withMarshallerSchemaLocation(&quot;http://apihost http://apihost/schema.xsd&quot;)
* .build();
*
*
* api = Feign.builder()
* .decoder(new JAXBDecoder(jaxbFactory))
* .target(MyApi.class, &quot;http://api&quot;);
Expand Down Expand Up @@ -92,7 +92,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
4 changes: 2 additions & 2 deletions sax/src/main/java/feign/sax/SAXDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Decodes responses using SAX, which is supported both in normal JVM environments, as well Android.
* <br>
* <h4>Basic example with with Feign.Builder</h4> <br>
*
*
* <pre>
* api = Feign.builder()
* .decoder(SAXDecoder.builder()
Expand Down Expand Up @@ -87,7 +87,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
8 changes: 4 additions & 4 deletions soap/src/main/java/feign/soap/SOAPDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
* <pre>
*
* public interface MyApi {
*
*
* &#64;RequestLine("POST /getObject")
* &#64;Headers({
* "SOAPAction: getObject",
* "Content-Type: text/xml"
* })
* MyJaxbObjectResponse getObject(MyJaxbObjectRequest request);
*
*
* }
*
* ...
Expand All @@ -73,7 +73,7 @@
* log.info(faultException.getFault().getFaultString());
* }
* </pre>
*
*
* </p>
*
* @see SOAPErrorDecoder
Expand Down Expand Up @@ -123,7 +123,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 b03e4e8

Please sign in to comment.