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