Skip to content

Commit

Permalink
try-catch getSubscriptions for OOMs (#4435)
Browse files Browse the repository at this point in the history
  • Loading branch information
aitorvs authored Apr 16, 2024
1 parent 70c8ece commit 5548962
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ enum class BillingError {
ITEM_ALREADY_OWNED,
ITEM_NOT_OWNED,
NETWORK_ERROR,
UNKNOWN_ERROR,
UNKNOWN_ERROR, // for when billing returns something we don't understand
BILLING_CRASH_ERROR, // This is our own error
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.android.billingclient.api.queryProductDetails
import com.android.billingclient.api.queryPurchaseHistory
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.subscriptions.impl.billing.BillingError.BILLING_CRASH_ERROR
import com.duckduckgo.subscriptions.impl.billing.BillingInitResult.Failure
import com.duckduckgo.subscriptions.impl.billing.BillingInitResult.Success
import com.squareup.anvil.annotations.ContributesBinding
Expand All @@ -44,6 +45,7 @@ import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.withContext
import logcat.LogPriority.WARN
import logcat.asLog
import logcat.logcat

@ContributesBinding(AppScope::class)
Expand Down Expand Up @@ -97,25 +99,30 @@ class RealBillingClientAdapter @Inject constructor(
}

override suspend fun getSubscriptions(productIds: List<String>): SubscriptionsResult {
val client = billingClient
if (client == null || !client.isReady) return SubscriptionsResult.Failure()

val queryParams = QueryProductDetailsParams.newBuilder()
.setProductList(
productIds.map { productId ->
Product.newBuilder()
.setProductId(productId)
.setProductType(ProductType.SUBS)
.build()
},
)
.build()

val (billingResult, productDetails) = client.queryProductDetails(queryParams)

return when (billingResult.responseCode) {
BillingResponseCode.OK -> SubscriptionsResult.Success(productDetails.orEmpty())
else -> SubscriptionsResult.Failure(billingResult.responseCode.toBillingError(), billingResult.debugMessage)
try {
val client = billingClient
if (client == null || !client.isReady) return SubscriptionsResult.Failure()

val queryParams = QueryProductDetailsParams.newBuilder()
.setProductList(
productIds.map { productId ->
Product.newBuilder()
.setProductId(productId)
.setProductType(ProductType.SUBS)
.build()
},
)
.build()

val (billingResult, productDetails) = client.queryProductDetails(queryParams)

return when (billingResult.responseCode) {
BillingResponseCode.OK -> SubscriptionsResult.Success(productDetails.orEmpty())
else -> SubscriptionsResult.Failure(billingResult.responseCode.toBillingError(), billingResult.debugMessage)
}
} catch (t: Throwable) {
logcat { "Error getting subscriptions: ${t.asLog()}" }
return SubscriptionsResult.Failure(billingError = BILLING_CRASH_ERROR)
}
}

Expand Down

0 comments on commit 5548962

Please sign in to comment.