@Test public void testCallbackErrorHandling() { CompletableFuture<Object> future = new CompletableFuture<>(); IOException exception = new IOException("Request failed miserably"); Callback callback = httpClient.createCallback(createProcessorMock(), future); callback.onFailure(createCallMock(), exception); expectFailure(future, exception); }
<T> Callback createCallback(ResponseProcessor<? extends T> processor, CompletableFuture<T> resultFuture) { return new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) { try { LOG.debug("HTTP response {} - {} {}", response.code(), response.request().method(), response.request().url()); T result = processor.process(new ResponseImpl(response)); resultFuture.complete(result); } catch (Exception e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } finally { response.close(); } } }; }
HttpClientImpl implements HttpClient { <T> Callback createCallback(ResponseProcessor<? extends T> processor, CompletableFuture<T> resultFuture) { return new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) { try { LOG.debug("HTTP response {} - {} {}", response.code(), response.request().method(), response.request().url()); T result = processor.process(new ResponseImpl(response)); resultFuture.complete(result); } catch (Exception e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } finally { response.close(); } } }; } }
HttpClientImpl implements HttpClient { <T> Callback createCallback(ResponseProcessor<? extends T> processor, CompletableFuture<T> resultFuture) { return new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) { try { LOG.debug("HTTP response {} - {} {}", response.code(), response.request().method(), response.request().url()); T result = processor.process(new ResponseImpl(response)); resultFuture.complete(result); } catch (Exception e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } finally { response.close(); } } }; } HttpClientImpl(OkHttpClient client, ObjectMapper mapper); }
HttpClientImpl implements HttpClient { <T> Callback createCallback(ResponseProcessor<? extends T> processor, CompletableFuture<T> resultFuture) { return new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) { try { LOG.debug("HTTP response {} - {} {}", response.code(), response.request().method(), response.request().url()); T result = processor.process(new ResponseImpl(response)); resultFuture.complete(result); } catch (Exception e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } finally { response.close(); } } }; } HttpClientImpl(OkHttpClient client, ObjectMapper mapper); @Override CompletableFuture<T> execute(Consumer<? super RequestBuilder> producer,
                                            ResponseProcessor<? extends T> processor); }
HttpClientImpl implements HttpClient { <T> Callback createCallback(ResponseProcessor<? extends T> processor, CompletableFuture<T> resultFuture) { return new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) { try { LOG.debug("HTTP response {} - {} {}", response.code(), response.request().method(), response.request().url()); T result = processor.process(new ResponseImpl(response)); resultFuture.complete(result); } catch (Exception e) { resultFuture.completeExceptionally(e); LOG.error("Failed HTTP request {} {}", call.request().method(), call.request().url(), e); } finally { response.close(); } } }; } HttpClientImpl(OkHttpClient client, ObjectMapper mapper); @Override CompletableFuture<T> execute(Consumer<? super RequestBuilder> producer,
                                            ResponseProcessor<? extends T> processor);  }