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);
}
}
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");
}
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);
}
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();
}
Aggregations