Skip to content

[http-client] Exception Mapper/Async Retry (2) #275

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 4 commits into from
Aug 24, 2023
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
14 changes: 12 additions & 2 deletions http-client/src/main/java/io/avaje/http/client/DBaseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.function.Function;

import static java.util.Objects.requireNonNull;

Expand All @@ -27,6 +28,7 @@ abstract class DBaseBuilder {
Duration requestTimeout = Duration.ofSeconds(20);
BodyAdapter bodyAdapter;
RetryHandler retryHandler;
Function<HttpException, RuntimeException> errorHandler;
AuthTokenProvider authTokenProvider;

CookieHandler cookieHandler = new CookieManager();
Expand Down Expand Up @@ -167,7 +169,15 @@ DHttpClientContext buildClient() {
if (bodyAdapter == null) {
bodyAdapter = defaultBodyAdapter();
}
return new DHttpClientContext(client, baseUrl, requestTimeout, bodyAdapter, retryHandler, buildListener(), authTokenProvider, buildIntercept());
return new DHttpClientContext(
client,
baseUrl,
requestTimeout,
bodyAdapter,
retryHandler,
errorHandler,
buildListener(),
authTokenProvider,
buildIntercept());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.function.Function;

final class DHttpClientBuilder extends DBaseBuilder implements HttpClient.Builder, HttpClient.Builder.State {

Expand Down Expand Up @@ -52,6 +53,12 @@ public HttpClient.Builder retryHandler(RetryHandler retryHandler) {
return this;
}

@Override
public HttpClient.Builder globalErrorMapper(Function<HttpException, RuntimeException> handler) {
this.errorHandler = handler;
return this;
}

@Override
public HttpClient.Builder requestLogging(boolean requestLogging) {
this.requestLogging = requestLogging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;

final class DHttpClientContext implements HttpClient, SpiHttpClient {

Expand All @@ -39,21 +40,31 @@ final class DHttpClientContext implements HttpClient, SpiHttpClient {
private final LongAdder metricResBytes = new LongAdder();
private final LongAdder metricResMicros = new LongAdder();
private final LongAccumulator metricResMaxMicros = new LongAccumulator(Math::max, 0);

DHttpClientContext(java.net.http.HttpClient httpClient, String baseUrl, Duration requestTimeout, BodyAdapter bodyAdapter, RetryHandler retryHandler, RequestListener requestListener, AuthTokenProvider authTokenProvider, RequestIntercept intercept) {
private final Function<HttpException, RuntimeException> errorHandler;

DHttpClientContext(
java.net.http.HttpClient httpClient,
String baseUrl,
Duration requestTimeout,
BodyAdapter bodyAdapter,
RetryHandler retryHandler,
Function<HttpException, RuntimeException> errorHandler,
RequestListener requestListener,
AuthTokenProvider authTokenProvider,
RequestIntercept intercept) {
this.httpClient = httpClient;
this.baseUrl = baseUrl;
this.requestTimeout = requestTimeout;
this.bodyAdapter = bodyAdapter;
this.retryHandler = retryHandler;
this.errorHandler = errorHandler;
this.requestListener = requestListener;
this.authTokenProvider = authTokenProvider;
this.withAuthToken = authTokenProvider != null;
this.requestIntercept = intercept;
}

@Override
@SuppressWarnings("unchecked")
public <T> T create(Class<T> clientInterface) {
if (!clientInterface.isInterface()) {
throw new IllegalArgumentException("API declarations must be interfaces.");
Expand Down Expand Up @@ -109,6 +120,10 @@ public UrlBuilder url() {
return new UrlBuilder(baseUrl);
}

public Function<HttpException, RuntimeException> errorMapper() {
return errorHandler;
}

@Override
public java.net.http.HttpClient httpClient() {
return httpClient;
Expand Down Expand Up @@ -184,19 +199,6 @@ public long avgMicros() {
}
}

@Override
public void checkResponse(HttpResponse<?> response) {
if (response.statusCode() >= 300) {
throw new HttpException(response, this);
}
}

void checkMaybeThrow(HttpResponse<byte[]> response) {
if (response.statusCode() >= 300) {
throw new HttpException(this, response);
}
}

@SuppressWarnings("unchecked")
public BodyContent readErrorContent(boolean responseAsBytes, HttpResponse<?> httpResponse) {
if (responseAsBytes) {
Expand Down Expand Up @@ -259,16 +261,28 @@ String firstHeader(HttpHeaders headers, String... names) {
<T> HttpResponse<T> send(HttpRequest.Builder requestBuilder, HttpResponse.BodyHandler<T> bodyHandler) {
try {
return httpClient.send(requestBuilder.build(), bodyHandler);
} catch (final IOException e) {
throw new HttpException(499, e);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new HttpException(499, e);
} catch (final Exception e) {
throw new HttpException(499, e);
}
}

<T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest.Builder requestBuilder, HttpResponse.BodyHandler<T> bodyHandler) {
return httpClient.sendAsync(requestBuilder.build(), bodyHandler);
<T> CompletableFuture<HttpResponse<T>> sendAsync(
HttpRequest.Builder requestBuilder, HttpResponse.BodyHandler<T> bodyHandler) {
return httpClient
.sendAsync(requestBuilder.build(), bodyHandler)
.handle(
(r, e) -> {
if (e != null) {
if (e.getCause() instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new HttpException(499, e.getCause());
}
return r;
});
}

<T> BodyContent write(T bean, Class<?> type, String contentType) {
Expand Down

This file was deleted.

Loading