Skip to content

Commit e99752b

Browse files
authored
Merge pull request #54 from SentryMan/exception-handling
Add retry exception handler
2 parents b95d48c + 3f9a3ab commit e99752b

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

client/src/main/java/io/avaje/http/client/DHttpClientRequestWithRetry.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,34 @@ final class DHttpClientRequestWithRetry extends DHttpClientRequest {
2222
@Override
2323
protected <T> HttpResponse<T> performSend(HttpResponse.BodyHandler<T> responseHandler) {
2424
HttpResponse<T> res;
25-
res = super.performSend(responseHandler);
26-
if (res.statusCode() < 300) {
27-
return res;
28-
}
29-
while (retryHandler.isRetry(retryCount++, res)) {
30-
res = super.performSend(responseHandler);
25+
HttpException ex;
26+
27+
do {
28+
try {
29+
res = super.performSend(responseHandler);
30+
ex = null;
31+
} catch (final HttpException e) {
32+
ex = e;
33+
res = null;
34+
}
35+
if (res != null && res.statusCode() < 300) {
36+
return res;
37+
}
38+
retryCount++;
39+
} while (retry(res, ex));
40+
41+
if (res == null && ex != null) {
42+
throw ex;
3143
}
44+
3245
return res;
3346
}
3447

48+
protected boolean retry(HttpResponse<?> res, HttpException ex) {
49+
50+
if (res != null) {
51+
return retryHandler.isRetry(retryCount, res);
52+
}
53+
return retryHandler.isExceptionRetry(retryCount, ex);
54+
}
3555
}

client/src/main/java/io/avaje/http/client/HttpException.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,37 @@ public HttpException(int statusCode, Throwable cause) {
9191
}
9292

9393
/**
94-
* Return the response body content as a bean
94+
* Return the response body content as a bean, or else null if body content doesn't exist.
9595
*
9696
* @param cls The type of bean to convert the response to
9797
* @return The response as a bean
9898
*/
9999
public <T> T bean(Class<T> cls) {
100+
if (httpResponse == null) {
101+
return null;
102+
}
100103
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
101104
return context.readBean(cls, body);
102105
}
103106

104107
/**
105-
* Return the response body content as a UTF8 string.
108+
* Return the response body content as a UTF8 string, or else null if body content doesn't exist.
106109
*/
107110
public String bodyAsString() {
111+
if (httpResponse == null) {
112+
return null;
113+
}
108114
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
109115
return new String(body.content(), StandardCharsets.UTF_8);
110116
}
111117

112-
/**
113-
* Return the response body content as raw bytes.
118+
/**
119+
* Return the response body content as raw bytes, or else null if body content doesn't exist.
114120
*/
115121
public byte[] bodyAsBytes() {
122+
if (httpResponse == null) {
123+
return null;
124+
}
116125
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
117126
return body.content();
118127
}

client/src/main/java/io/avaje/http/client/RetryHandler.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ public interface RetryHandler {
1111
* Return true if the request should be retried.
1212
*
1313
* @param retryCount The number of retry attempts already executed
14-
* @param response The HTTP response
14+
* @param response The HTTP response
1515
* @return True if the request should be retried or false if not
1616
*/
1717
boolean isRetry(int retryCount, HttpResponse<?> response);
1818

19+
/**
20+
* Return true if the request should be retried.
21+
*
22+
* @param retryCount The number of retry attempts already executed
23+
* @param exception The Wrapped Error thrown by the underlying Http Client
24+
* @return True if the request should be retried or false if not
25+
*/
26+
default boolean isExceptionRetry(int retryCount, HttpException exception) {
27+
throw exception;
28+
}
1929
}

0 commit comments

Comments
 (0)