Search in sources :

Example 1 with ApolloHttpException

use of com.apollographql.apollo.exception.ApolloHttpException 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 2 with ApolloHttpException

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

the class ApolloExceptionTest method httpException.

@Test
public void httpException() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(401).setBody("Unauthorized request!"));
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final AtomicReference<String> errorResponse = new AtomicReference<>();
    Rx2Apollo.from(apolloClient.query(emptyQuery)).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            errorRef.set(throwable);
            errorResponse.set(((ApolloHttpException) throwable).rawResponse().body().string());
        }
    }).test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS).assertError(ApolloHttpException.class);
    ApolloHttpException e = (ApolloHttpException) errorRef.get();
    assertThat(e.code()).isEqualTo(401);
    assertThat(e.message()).isEqualTo("Client Error");
    assertThat(errorResponse.get()).isEqualTo("Unauthorized request!");
    assertThat(e.getMessage()).isEqualTo("HTTP 401 Client Error");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Consumer(io.reactivex.functions.Consumer) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with ApolloHttpException

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

the class ApolloCallbackTest method onHttpError.

@Test
public void onHttpError() 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(401).setBody("Unauthorized request!"));
    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) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }

        @Override
        public void onHttpError(@Nonnull ApolloHttpException e) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
    assertThat(exceptionRef.get()).isInstanceOf(ApolloHttpException.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) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ApolloHttpException

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

the class ApolloPrefetchCallbackTest method onHttpError.

@Test
public void onHttpError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    server.enqueue(new MockResponse().setResponseCode(401).setBody("Unauthorized request!"));
    apolloClient.prefetch(EMPTY_QUERY).enqueue(ApolloPrefetchCallback.wrap(new ApolloPrefetch.Callback() {

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

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

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

Aggregations

ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)4 ApolloException (com.apollographql.apollo.exception.ApolloException)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.junit.Test)3 Handler (android.os.Handler)2 Response (com.apollographql.apollo.api.Response)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nonnull (javax.annotation.Nonnull)2 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)1 OperationResponseParser (com.apollographql.apollo.response.OperationResponseParser)1 Consumer (io.reactivex.functions.Consumer)1