Search in sources :

Example 1 with ApolloParseException

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

the class ApolloInterceptorTest method asyncApplicationInterceptorThrowsApolloException.

@Test
public void asyncApplicationInterceptorThrowsApolloException() throws Exception {
    final String message = "ApolloException";
    EpisodeHeroNameQuery query = createHeroNameQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
            ApolloException apolloException = new ApolloParseException(message);
            callBack.onFailure(apolloException);
        }

        @Override
        public void dispose() {
        }
    };
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertError(new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable throwable) throws Exception {
            return message.equals(throwable.getMessage()) && throwable instanceof ApolloParseException;
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 2 with ApolloParseException

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

the class ApolloParseInterceptor method parse.

@SuppressWarnings("unchecked")
InterceptorResponse parse(Operation operation, okhttp3.Response httpResponse) throws ApolloHttpException, ApolloParseException {
    String cacheKey = httpResponse.request().header(HttpCache.CACHE_KEY_HEADER);
    if (httpResponse.isSuccessful()) {
        try {
            OperationResponseParser parser = new OperationResponseParser(operation, responseFieldMapper, scalarTypeAdapters, normalizer);
            Response parsedResponse = parser.parse(httpResponse.body().source()).toBuilder().fromCache(httpResponse.cacheResponse() != null).build();
            if (parsedResponse.hasErrors() && httpCache != null) {
                httpCache.removeQuietly(cacheKey);
            }
            return new InterceptorResponse(httpResponse, parsedResponse, normalizer.records());
        } catch (Exception rethrown) {
            logger.e(rethrown, "Failed to parse network response for operation: %s", operation);
            closeQuietly(httpResponse);
            if (httpCache != null) {
                httpCache.removeQuietly(cacheKey);
            }
            throw new ApolloParseException("Failed to parse http response", rethrown);
        }
    } else {
        logger.e("Failed to parse network response: %s", httpResponse);
        throw new ApolloHttpException(httpResponse);
    }
}
Also used : Response(com.apollographql.apollo.api.Response) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) OperationResponseParser(com.apollographql.apollo.response.OperationResponseParser) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ApolloException(com.apollographql.apollo.exception.ApolloException)

Example 3 with ApolloParseException

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

the class ApolloCallbackTest method onParseError.

@Test
public void onParseError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
    server.enqueue(new MockResponse().setResponseCode(200).setBody("nonsense"));
    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 onParseError(@Nonnull ApolloParseException e) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
    assertThat(exceptionRef.get()).isInstanceOf(ApolloParseException.class);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) ApolloException(com.apollographql.apollo.exception.ApolloException) Nonnull(javax.annotation.Nonnull) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ApolloException (com.apollographql.apollo.exception.ApolloException)3 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)3 Response (com.apollographql.apollo.api.Response)2 Nonnull (javax.annotation.Nonnull)2 Test (org.junit.Test)2 Handler (android.os.Handler)1 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)1 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)1 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)1 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)1 OperationResponseParser (com.apollographql.apollo.response.OperationResponseParser)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MockResponse (okhttp3.mockwebserver.MockResponse)1