Search in sources :

Example 1 with ResponseException

use of com.kickstarter.services.ResponseException in project android-oss by kickstarter.

the class ApiErrorOperator method call.

@Override
public Subscriber<? super Response<T>> call(@NonNull final Subscriber<? super T> subscriber) {
    return new Subscriber<retrofit2.Response<T>>() {

        @Override
        public void onCompleted() {
            if (!subscriber.isUnsubscribed()) {
                subscriber.onCompleted();
            }
        }

        @Override
        public void onError(@NonNull final Throwable e) {
            if (!subscriber.isUnsubscribed()) {
                subscriber.onError(e);
            }
        }

        @Override
        public void onNext(@NonNull final retrofit2.Response<T> response) {
            if (subscriber.isUnsubscribed()) {
                return;
            }
            if (!response.isSuccess()) {
                try {
                    final ErrorEnvelope envelope = gson.fromJson(response.errorBody().string(), ErrorEnvelope.class);
                    subscriber.onError(new ApiException(envelope, response));
                } catch (@NonNull final IOException e) {
                    subscriber.onError(new ResponseException(response));
                }
            }
            subscriber.onNext(response.body());
        }
    };
}
Also used : Response(retrofit2.Response) Subscriber(rx.Subscriber) ResponseException(com.kickstarter.services.ResponseException) NonNull(android.support.annotation.NonNull) ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) IOException(java.io.IOException) ApiException(com.kickstarter.services.ApiException)

Aggregations

NonNull (android.support.annotation.NonNull)1 ApiException (com.kickstarter.services.ApiException)1 ResponseException (com.kickstarter.services.ResponseException)1 ErrorEnvelope (com.kickstarter.services.apiresponses.ErrorEnvelope)1 IOException (java.io.IOException)1 Response (retrofit2.Response)1 Subscriber (rx.Subscriber)1