Search in sources :

Example 6 with ApiException

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

the class ApiExceptionFactory method apiError.

@NonNull
public static ApiException apiError(@NonNull final ErrorEnvelope errorEnvelope) {
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(errorEnvelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(errorEnvelope.httpCode(), body);
    return new ApiException(errorEnvelope, response);
}
Also used : Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(androidx.annotation.NonNull)

Example 7 with ApiException

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

the class DiscoveryViewModelTest method testShowSnackBar_whenIntentFromDeepLinkSuccessResponse_showErrorMessage.

@Test
public void testShowSnackBar_whenIntentFromDeepLinkSuccessResponse_showErrorMessage() {
    final String url = "https://*.kickstarter.com/profile/verify_email";
    final Intent intentWithUrl = new Intent().setData(Uri.parse(url));
    final ErrorEnvelope errorEnvelope = ErrorEnvelope.builder().httpCode(403).errorMessages(Collections.singletonList("expired")).build();
    final ApiException apiException = ApiExceptionFactory.apiError(errorEnvelope);
    final MockApiClient mockApiClient = new MockApiClient() {

        @NonNull
        @Override
        public Observable<EmailVerificationEnvelope> verifyEmail(@NonNull final String token) {
            return Observable.error(apiException);
        }
    };
    final Environment mockedClientEnvironment = environment().toBuilder().apiClient(mockApiClient).build();
    this.vm = new DiscoveryViewModel.ViewModel(mockedClientEnvironment);
    this.vm.getOutputs().showSuccessMessage().subscribe(this.showSuccessMessage);
    this.vm.getOutputs().showErrorMessage().subscribe(this.showErrorMessage);
    this.vm.intent(intentWithUrl);
    this.showSuccessMessage.assertNoValues();
    this.showErrorMessage.assertValue("expired");
}
Also used : EmailVerificationEnvelope(com.kickstarter.services.apiresponses.EmailVerificationEnvelope) MockApiClient(com.kickstarter.mock.services.MockApiClient) NonNull(androidx.annotation.NonNull) ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Environment(com.kickstarter.libs.Environment) Intent(android.content.Intent) ApiException(com.kickstarter.services.ApiException) Test(org.junit.Test)

Example 8 with ApiException

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

the class ApiExceptionFactory method invalidLoginException.

@NonNull
public static ApiException invalidLoginException() {
    final ErrorEnvelope envelope = ErrorEnvelope.builder().errorMessages(Collections.singletonList("Invalid login.")).httpCode(401).ksrCode(ErrorEnvelope.INVALID_XAUTH_LOGIN).build();
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(envelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(envelope.httpCode(), body);
    return new ApiException(envelope, response);
}
Also used : ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(android.support.annotation.NonNull)

Example 9 with ApiException

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

the class ApiErrorOperator method call.

@Override
public Subscriber<? super Response<T>> call(@NonNull final Subscriber<? super T> subscriber) {
    final Gson gson = this.gson;
    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.isSuccessful()) {
                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));
                }
            } else {
                subscriber.onNext(response.body());
                subscriber.onCompleted();
            }
        }
    };
}
Also used : Response(retrofit2.Response) Subscriber(rx.Subscriber) ResponseException(com.kickstarter.services.ResponseException) NonNull(androidx.annotation.NonNull) Gson(com.google.gson.Gson) ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) IOException(java.io.IOException) ApiException(com.kickstarter.services.ApiException)

Example 10 with ApiException

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

the class ApiExceptionFactory method tfaRequired.

@NonNull
public static ApiException tfaRequired() {
    final ErrorEnvelope envelope = ErrorEnvelope.builder().ksrCode(ErrorEnvelope.TFA_REQUIRED).httpCode(403).errorMessages(Collections.singletonList("Two-factor authentication required.")).build();
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(envelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(envelope.httpCode(), body);
    return new ApiException(envelope, response);
}
Also used : ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(androidx.annotation.NonNull)

Aggregations

ApiException (com.kickstarter.services.ApiException)12 ErrorEnvelope (com.kickstarter.services.apiresponses.ErrorEnvelope)10 ResponseBody (okhttp3.ResponseBody)10 Observable (rx.Observable)10 Gson (com.google.gson.Gson)9 NonNull (androidx.annotation.NonNull)7 NonNull (android.support.annotation.NonNull)5 Intent (android.content.Intent)1 Environment (com.kickstarter.libs.Environment)1 MockApiClient (com.kickstarter.mock.services.MockApiClient)1 ResponseException (com.kickstarter.services.ResponseException)1 EmailVerificationEnvelope (com.kickstarter.services.apiresponses.EmailVerificationEnvelope)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 Response (retrofit2.Response)1 Subscriber (rx.Subscriber)1