diff --git a/stripe/src/main/java/com/stripe/android/ApiOperation.kt b/stripe/src/main/java/com/stripe/android/ApiOperation.kt index 6191854712d..8be18a94ba4 100644 --- a/stripe/src/main/java/com/stripe/android/ApiOperation.kt +++ b/stripe/src/main/java/com/stripe/android/ApiOperation.kt @@ -2,8 +2,10 @@ package com.stripe.android import com.stripe.android.exception.APIConnectionException import com.stripe.android.exception.APIException +import com.stripe.android.exception.InvalidRequestException import com.stripe.android.exception.StripeException import java.io.IOException +import java.lang.IllegalArgumentException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -27,6 +29,13 @@ internal abstract class ApiOperation( ResultWrapper.create(APIException(e)) } catch (e: IOException) { ResultWrapper.create(APIConnectionException.create(e)) + } catch (e: IllegalArgumentException) { + ResultWrapper.create( + InvalidRequestException( + message = e.message, + cause = e + ) + ) } withContext(Main) { diff --git a/stripe/src/test/java/com/stripe/android/ApiOperationTest.kt b/stripe/src/test/java/com/stripe/android/ApiOperationTest.kt new file mode 100644 index 00000000000..3c2e001e2b6 --- /dev/null +++ b/stripe/src/test/java/com/stripe/android/ApiOperationTest.kt @@ -0,0 +1,37 @@ +package com.stripe.android + +import com.nhaarman.mockitokotlin2.argWhere +import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.verify +import com.stripe.android.exception.InvalidRequestException +import kotlin.test.Test +import kotlinx.coroutines.MainScope +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class ApiOperationTest { + + private val callback: ApiResultCallback = mock() + + @Test + fun getResult_whenException_shouldInvokeCallbackOnError() { + FakeApiOperation(callback).execute() + verify(callback).onError( + argWhere { + (it as? InvalidRequestException)?.message == "Illegal argument!" + } + ) + } + + internal class FakeApiOperation( + callback: ApiResultCallback + ) : ApiOperation( + workScope = MainScope(), + callback = callback + ) { + override suspend fun getResult(): String? { + throw IllegalArgumentException("Illegal argument!") + } + } +}