Search in sources :

Example 1 with BadResponseException

use of com.canoo.platform.core.http.BadResponseException in project dolphin-platform by canoo.

the class HttpCallExecutorImpl method execute.

@Override
public CompletableFuture<HttpResponse<R>> execute() {
    final CompletableFuture<HttpResponse<R>> completableFuture = new CompletableFuture<>();
    executor.submit(() -> {
        try {
            final HttpResponse<R> result = provider.get();
            final int statusCode = result.getStatusCode();
            if (statusCode >= 300) {
                final HttpException e = new BadResponseException(result, "Bad Response: " + statusCode);
                if (errorHandler != null) {
                    uiExecutor.execute(() -> errorHandler.accept(e));
                }
                completableFuture.completeExceptionally(e);
            } else {
                if (onDone != null) {
                    uiExecutor.execute(() -> onDone.accept(result));
                }
                completableFuture.complete(result);
            }
        } catch (final HttpException e) {
            if (errorHandler != null) {
                uiExecutor.execute(() -> errorHandler.accept(e));
            }
            completableFuture.completeExceptionally(e);
        }
    });
    return completableFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) BadResponseException(com.canoo.platform.core.http.BadResponseException) HttpResponse(com.canoo.platform.core.http.HttpResponse) HttpException(com.canoo.platform.core.http.HttpException)

Aggregations

BadResponseException (com.canoo.platform.core.http.BadResponseException)1 HttpException (com.canoo.platform.core.http.HttpException)1 HttpResponse (com.canoo.platform.core.http.HttpResponse)1 CompletableFuture (java.util.concurrent.CompletableFuture)1