public ApolloHttpException(@Nullable okhttp3.Response rawResponse) { super(formatMessage(rawResponse)); this.code = rawResponse != null ? rawResponse.code() : 0; this.message = rawResponse != null ? rawResponse.message() : ""; this.rawResponse = rawResponse; }
@Override public void run() { onTransportFailure(new ApolloNetworkException("Subscription server is not responding")); } });
/** * Execute store operation * <p> * <b>NOTE: this is a sync operation, proceed with a caution as it may include SQLite instruction<b/> * </p> * * @throws {@link ApolloException} in case of any errors */ public final T execute() throws ApolloException { checkIfExecuted(); try { return perform(); } catch (Exception e) { throw new ApolloException("Failed to perform store operation", e); } }
@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); } }
/** * Gets called when an http request error takes place. This is the case when the returned http status code doesn't * lie in the range 200 (inclusive) and 300 (exclusive). */ public void onHttpError(@NotNull ApolloHttpException e) { onFailure(e); Response response = e.rawResponse(); if (response != null) { response.close(); } }
private synchronized void activate(Optional<ApolloPrefetch.Callback> callback) throws ApolloCanceledException { switch (state.get()) { case IDLE: originalCallback.set(callback.orNull()); tracker.registerPrefetchCall(this); break; case CANCELED: throw new ApolloCanceledException("Call is cancelled."); case TERMINATED: case ACTIVE: throw new IllegalStateException("Already Executed"); default: throw new IllegalStateException("Unknown state"); } state.set(ACTIVE); }
@Override public void onResponse(@NotNull ApolloInterceptor.InterceptorResponse response) { Response httpResponse = response.httpResponse.get(); try { Optional<Callback> callback = terminate(); if (!callback.isPresent()) { logger.d("onResponse for prefetch operation: %s. No callback present.", operation().name().name()); return; } if (httpResponse.isSuccessful()) { callback.get().onSuccess(); } else { callback.get().onHttpError(new ApolloHttpException(httpResponse)); } } finally { httpResponse.close(); } }
@Override public boolean test(Throwable throwable) throws Exception { ApolloNetworkException e = (ApolloNetworkException) throwable; assertThat(e.getMessage()).isEqualTo("Failed to execute http call"); assertThat(e.getCause().getClass()).isEqualTo(SocketTimeoutException.class); return true; } });
@Override public boolean test(Throwable throwable) throws Exception { ApolloParseException e = (ApolloParseException) throwable; assertThat(e.getMessage()).isEqualTo("Failed to parse http response"); assertThat(e.getCause().getClass()).isEqualTo(JsonEncodingException.class); return true; } });
/** * Schedules operation to be executed in dispatcher * * @param callback to be notified about operation result */ public void enqueue(@Nullable final Callback<T> callback) { checkIfExecuted(); this.callback.set(callback); dispatcher.execute(new Runnable() { @Override public void run() { T result; try { result = perform(); } catch (Exception e) { notifyFailure(new ApolloException("Failed to perform store operation", e)); return; } notifySuccess(result); } }); }
@Override public void onNetworkError(@NotNull Throwable t) { Callback<T> callback = this.originalCallback; if (callback != null) { callback.onFailure(new ApolloNetworkException("Subscription failed", t)); } terminate(); }
/** * <p>Gets called when an http request error takes place. This is the case when the returned http status code * doesn't lie in the range 200 (inclusive) and 300 (exclusive).</p> * * <b>NOTE:</b> by overriding this callback you must call {@link okhttp3.Response#close()} on {@link * ApolloHttpException#rawResponse} to close the network connection. */ public void onHttpError(@NotNull ApolloHttpException e) { onFailure(e); okhttp3.Response response = e.rawResponse(); if (response != null) { response.close(); } }
private synchronized void activate(Optional<ApolloCall.Callback<T>> callback) throws ApolloCanceledException { switch (state.get()) { case IDLE: originalCallback.set(callback.orNull()); tracker.registerQueryWatcher(this); break; case CANCELED: throw new ApolloCanceledException("Call is cancelled."); case TERMINATED: case ACTIVE: throw new IllegalStateException("Already Executed"); default: throw new IllegalStateException("Unknown state"); } state.set(ACTIVE); }
@Override public boolean test(Throwable throwable) throws Exception { ApolloNetworkException e = (ApolloNetworkException) throwable; assertThat(e.getMessage()).isEqualTo("Failed to execute http call"); assertThat(e.getCause().getClass()).isEqualTo(SocketTimeoutException.class); return true; } });
@Override public void run() { T result; try { result = perform(); } catch (Exception e) { notifyFailure(new ApolloException("Failed to perform store operation", e)); return; } notifySuccess(result); } });
@Override public void run() { callBack.onFetch(FetchSourceType.NETWORK); try { httpCall = httpCall(request.operation, request.cacheHeaders, request.sendQueryDocument); } 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(@NotNull Call call, @NotNull 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(@NotNull Call call, @NotNull Response response) throws IOException { if (disposed) return; callBack.onResponse(new ApolloInterceptor.InterceptorResponse(response)); callBack.onCompleted(); } }); } });
@Override public void accept(Throwable throwable) throws Exception { errorRef.set(throwable); errorResponse.set(((ApolloHttpException) throwable).rawResponse().body().string()); } })
@SuppressWarnings("ResultOfMethodCallIgnored") private synchronized void activate(Optional<Callback<T>> callback) { switch (state.get()) { case IDLE: originalCallback.set(callback.orNull()); tracker.registerCall(this); callback.apply(new Action<Callback<T>>() { @Override public void apply(@NotNull Callback<T> callback) { callback.onStatusEvent(StatusEvent.SCHEDULED); } }); break; case CANCELED: throw new ApolloCanceledException("Call is cancelled."); case TERMINATED: case ACTIVE: throw new IllegalStateException("Already Executed"); default: throw new IllegalStateException("Unknown state"); } state.set(ACTIVE); }
InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException { ResponseNormalizer<Record> responseNormalizer = apolloStore.cacheResponseNormalizer(); //noinspection unchecked ApolloStoreOperation<Response> apolloStoreOperation = apolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders); Response cachedResponse = apolloStoreOperation.execute(); if (cachedResponse.data() != null) { logger.d("Cache HIT for operation %s", request.operation); return new InterceptorResponse(null, cachedResponse, responseNormalizer.records()); } logger.d("Cache MISS for operation %s", request.operation); throw new ApolloException(String.format("Cache miss for operation %s", request.operation)); }
@Override public void execute(@NotNull Callback<T> callback) throws ApolloCanceledException { checkNotNull(callback, "callback == null"); synchronized (this) { switch (state.get()) { case IDLE: { state.set(ACTIVE); subscriptionCallback = new SubscriptionManagerCallback<>(callback, this); subscriptionManager.subscribe(subscription, subscriptionCallback); break; } case CANCELED: throw new ApolloCanceledException("Call is cancelled."); case TERMINATED: case ACTIVE: throw new IllegalStateException("Already Executed"); default: throw new IllegalStateException("Unknown state"); } } }