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 IOException in ApiOperation #1722

Merged
merged 1 commit into from
Oct 17, 2019
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
4 changes: 4 additions & 0 deletions stripe/src/main/java/com/stripe/android/ApiOperation.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.stripe.android

import android.os.AsyncTask
import com.stripe.android.exception.APIConnectionException
import com.stripe.android.exception.StripeException
import java.io.IOException
import org.json.JSONException

internal abstract class ApiOperation<ResultType>(
Expand All @@ -17,6 +19,8 @@ internal abstract class ApiOperation<ResultType>(
ResultWrapper.create(e)
} catch (e: JSONException) {
ResultWrapper.create(e)
} catch (e: IOException) {
ResultWrapper.create(APIConnectionException.create(e))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class StripeApiRequestExecutor internal constructor(
return stripeResponse
} catch (e: IOException) {
logger.error("Exception while making Stripe API request", e)
throw APIConnectionException.create(request.baseUrl, e)
throw APIConnectionException.create(e, request.baseUrl)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class StripeFireAndForgetRequestExecutor(
// required to trigger the request
return it.responseCode
} catch (e: IOException) {
throw APIConnectionException.create(request.baseUrl, e)
throw APIConnectionException.create(e, request.baseUrl)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.stripe.android.exception

import java.io.IOException

/**
* An [Exception] that represents a failure to connect to Stripe's API.
*/
Expand All @@ -11,9 +13,13 @@ class APIConnectionException(
private const val STATUS_CODE = 0

@JvmStatic
fun create(url: String, e: Exception): APIConnectionException {
fun create(e: IOException, url: String? = null): APIConnectionException {
val displayUrl = listOfNotNull(
"Stripe",
"($url)".takeUnless { url.isNullOrBlank() }
).joinToString(" ")
return APIConnectionException(
"IOException during API request to Stripe ($url): ${e.message}. " +
"IOException during API request to $displayUrl: ${e.message}. " +
"Please check your internet connection and try again. " +
"If this problem persists, you should check Stripe's " +
"service status at https://twitter.com/stripestatus, " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
package com.stripe.android.exception

import java.io.IOException
import kotlin.test.Test
import kotlin.test.assertEquals

class APIConnectionExceptionTest {

@Test
fun testCreate() {
fun testCreateWithUrl() {
val ex = APIConnectionException.create(
"https://api.stripe.com/v1/payment_methods",
IllegalArgumentException("Invalid id")
IOException("Could not connect"),
"https://api.stripe.com/v1/payment_methods"
)
assertEquals(
"IOException during API request to Stripe " +
"(https://api.stripe.com/v1/payment_methods): Invalid id. " +
"Please check your internet connection and try again. " +
"If this problem persists, you should check Stripe's service " +
"status at https://twitter.com/stripestatus, " +
"or let us know at [email protected].",
"(https://api.stripe.com/v1/payment_methods): Could not connect. " +
"Please check your internet connection and try again. " +
"If this problem persists, you should check Stripe's service " +
"status at https://twitter.com/stripestatus, " +
"or let us know at [email protected].",
ex.message
)
}

@Test
fun testCreateWithoutUrl() {
val ex = APIConnectionException.create(
IOException("Could not connect")
)
assertEquals(
"IOException during API request to Stripe: Could not connect. " +
"Please check your internet connection and try again. " +
"If this problem persists, you should check Stripe's service " +
"status at https://twitter.com/stripestatus, " +
"or let us know at [email protected].",
ex.message
)
}
Expand Down