Search in sources :

Example 1 with ApolloNetworkException

use of com.apollographql.apollo.exception.ApolloNetworkException in project apollo-android by apollographql.

the class ApolloServerInterceptor method interceptAsync.

@Override
public void interceptAsync(@Nonnull final InterceptorRequest request, @Nonnull final ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull final CallBack callBack) {
    if (disposed)
        return;
    dispatcher.execute(new Runnable() {

        @Override
        public void run() {
            callBack.onFetch(FetchSourceType.NETWORK);
            try {
                httpCall = httpCall(request.operation);
            } catch (IOException e) {
                logger.e(e, "Failed to prepare http call for operation %s", request.operation.name().name());
                callBack.onFailure(new ApolloNetworkException("Failed to prepare http call", e));
                return;
            }
            httpCall.enqueue(new Callback() {

                @Override
                public void onFailure(@Nonnull Call call, @Nonnull IOException e) {
                    if (disposed)
                        return;
                    logger.e(e, "Failed to execute http call for operation %s", request.operation.name().name());
                    callBack.onFailure(new ApolloNetworkException("Failed to execute http call", e));
                }

                @Override
                public void onResponse(@Nonnull Call call, @Nonnull Response response) throws IOException {
                    if (disposed)
                        return;
                    callBack.onResponse(new ApolloInterceptor.InterceptorResponse(response));
                    callBack.onCompleted();
                }
            });
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Nonnull(javax.annotation.Nonnull) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) IOException(java.io.IOException) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException)

Example 2 with ApolloNetworkException

use of com.apollographql.apollo.exception.ApolloNetworkException in project apollo-android by apollographql.

the class ApolloCallbackTest method onNetworkError.

@Test
public void onNetworkError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
    apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {

        @Override
        public void onResponse(@Nonnull Response response) {
            countDownLatch.countDown();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            countDownLatch.countDown();
        }

        @Override
        public void onNetworkError(@Nonnull ApolloNetworkException e) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
    assertThat(exceptionRef.get()).isInstanceOf(ApolloNetworkException.class);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApolloException(com.apollographql.apollo.exception.ApolloException) Nonnull(javax.annotation.Nonnull) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException) Test(org.junit.Test)

Example 3 with ApolloNetworkException

use of com.apollographql.apollo.exception.ApolloNetworkException in project apollo-android by apollographql.

the class ApolloPrefetchCallbackTest method onNetworkError.

@Test
public void onNetworkError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    apolloClient.prefetch(EMPTY_QUERY).enqueue(ApolloPrefetchCallback.wrap(new ApolloPrefetch.Callback() {

        @Override
        public void onSuccess() {
            fail("Expected onNetworkError");
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            fail("Expected onNetworkError");
        }

        @Override
        public void onNetworkError(@Nonnull ApolloNetworkException e) {
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(2, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Handler(android.os.Handler) CountDownLatch(java.util.concurrent.CountDownLatch) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException) Test(org.junit.Test)

Aggregations

ApolloNetworkException (com.apollographql.apollo.exception.ApolloNetworkException)3 Nonnull (javax.annotation.Nonnull)3 Handler (android.os.Handler)2 ApolloException (com.apollographql.apollo.exception.ApolloException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 Response (com.apollographql.apollo.api.Response)1 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)1 IOException (java.io.IOException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Response (okhttp3.Response)1 MockResponse (okhttp3.mockwebserver.MockResponse)1