/** * <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(); } }
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 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(); } }
@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"); }
@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(); } }
@Override public void onResponse(@Nonnull 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(); } }
public ApolloHttpException(@Nullable Response rawResponse) { super(formatMessage(rawResponse)); this.code = rawResponse != null ? rawResponse.code() : 0; this.message = rawResponse != null ? rawResponse.message() : ""; this.rawResponse = rawResponse; }
@Override public void accept(Throwable throwable) throws Exception { errorRef.set(throwable); errorResponse.set(((ApolloHttpException) throwable).rawResponse().body().string()); } })
@Override public void onResponse(@Nonnull 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(); } }
public ApolloHttpException(@Nullable Response rawResponse) { super(formatMessage(rawResponse)); this.code = rawResponse != null ? rawResponse.code() : 0; this.message = rawResponse != null ? rawResponse.message() : ""; this.rawResponse = rawResponse; }
/** * 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(@Nonnull ApolloHttpException e) { onFailure(e); Response response = e.rawResponse(); if (response != null) { response.close(); } }
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(@Nonnull ApolloHttpException e) { onFailure(e); Response response = e.rawResponse(); if (response != null) { response.close(); } }
throw new ApolloHttpException(httpResponse);
/** * <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(@Nonnull ApolloHttpException e) { onFailure(e); okhttp3.Response response = e.rawResponse(); if (response != null) { response.close(); } }
/** * <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(@Nonnull ApolloHttpException e) { onFailure(e); okhttp3.Response response = e.rawResponse(); if (response != null) { response.close(); } }