Skip to content

Add retry exception handler #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,34 @@ final class DHttpClientRequestWithRetry extends DHttpClientRequest {
@Override
protected <T> HttpResponse<T> performSend(HttpResponse.BodyHandler<T> responseHandler) {
HttpResponse<T> res;
res = super.performSend(responseHandler);
if (res.statusCode() < 300) {
return res;
}
while (retryHandler.isRetry(retryCount++, res)) {
res = super.performSend(responseHandler);
HttpException ex;

do {
try {
res = super.performSend(responseHandler);
ex = null;
} catch (final HttpException e) {
ex = e;
res = null;
}
if (res != null && res.statusCode() < 300) {
return res;
}
retryCount++;
} while (retry(res, ex));

if (res == null && ex != null) {
throw ex;
}

return res;
}

protected boolean retry(HttpResponse<?> res, HttpException ex) {

if (res != null) {
return retryHandler.isRetry(retryCount, res);
}
return retryHandler.isExceptionRetry(retryCount, ex);
}
}
17 changes: 13 additions & 4 deletions client/src/main/java/io/avaje/http/client/HttpException.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,37 @@ public HttpException(int statusCode, Throwable cause) {
}

/**
* Return the response body content as a bean
* Return the response body content as a bean, or else null if body content doesn't exist.
*
* @param cls The type of bean to convert the response to
* @return The response as a bean
*/
public <T> T bean(Class<T> cls) {
if (httpResponse == null) {
return null;
}
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
return context.readBean(cls, body);
}

/**
* Return the response body content as a UTF8 string.
* Return the response body content as a UTF8 string, or else null if body content doesn't exist.
*/
public String bodyAsString() {
if (httpResponse == null) {
return null;
}
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
return new String(body.content(), StandardCharsets.UTF_8);
}

/**
* Return the response body content as raw bytes.
/**
* Return the response body content as raw bytes, or else null if body content doesn't exist.
*/
public byte[] bodyAsBytes() {
if (httpResponse == null) {
return null;
}
final BodyContent body = context.readErrorContent(responseAsBytes, httpResponse);
return body.content();
}
Expand Down
12 changes: 11 additions & 1 deletion client/src/main/java/io/avaje/http/client/RetryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ public interface RetryHandler {
* Return true if the request should be retried.
*
* @param retryCount The number of retry attempts already executed
* @param response The HTTP response
* @param response The HTTP response
* @return True if the request should be retried or false if not
*/
boolean isRetry(int retryCount, HttpResponse<?> response);

/**
* Return true if the request should be retried.
*
* @param retryCount The number of retry attempts already executed
* @param exception The Wrapped Error thrown by the underlying Http Client
* @return True if the request should be retried or false if not
*/
default boolean isExceptionRetry(int retryCount, HttpException exception) {
throw exception;
}
}