Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch IllegalArgumentException in ApiOperation #2441

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!")
}
}
}