Skip to content

Commit

Permalink
Catch IllegalArgumentException in ApiOperation (#2441)
Browse files Browse the repository at this point in the history
Fixes #2440
  • Loading branch information
mshafrir-stripe authored Apr 29, 2020
1 parent fed08a6 commit b9c0e1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions stripe/src/main/java/com/stripe/android/ApiOperation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +29,13 @@ internal abstract class ApiOperation<ResultType>(
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) {
Expand Down
37 changes: 37 additions & 0 deletions stripe/src/test/java/com/stripe/android/ApiOperationTest.kt
Original file line number Diff line number Diff line change
@@ -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<String> = mock()

@Test
fun getResult_whenException_shouldInvokeCallbackOnError() {
FakeApiOperation(callback).execute()
verify(callback).onError(
argWhere {
(it as? InvalidRequestException)?.message == "Illegal argument!"
}
)
}

internal class FakeApiOperation(
callback: ApiResultCallback<String>
) : ApiOperation<String>(
workScope = MainScope(),
callback = callback
) {
override suspend fun getResult(): String? {
throw IllegalArgumentException("Illegal argument!")
}
}
}

0 comments on commit b9c0e1e

Please sign in to comment.