Skip to content

Commit

Permalink
Handle missing INTERNET permissions when making network requests. (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonycosentini authored Aug 13, 2019
1 parent 5255332 commit 69c934b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal open class Dispatcher(
onError(e.toPurchasesError())
} catch (e: IOException) {
onError(e.toPurchasesError())
} catch (e: SecurityException) {
// This can happen if a user disables the INTERNET permission.
onError(e.toPurchasesError())
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions purchases/src/main/kotlin/com/revenuecat/purchases/errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum class PurchasesErrorCode(val description: String) {
InvalidAppUserIdError("The app user id is not valid."),
OperationAlreadyInProgressError("The operation is already in progress."),
UnknownBackendError("There was an unknown backend error."),
InsufficientPermissionsError("App does not have sufficient permissions to make purchases.")
}

internal enum class BackendErrorCode(val value: Int) {
Expand Down Expand Up @@ -67,6 +68,8 @@ internal enum class BackendErrorCode(val value: Int) {
internal fun Exception.toPurchasesError(): PurchasesError {
if (this is JSONException || this is IOException) {
return PurchasesError(PurchasesErrorCode.NetworkError, localizedMessage)
} else if (this is SecurityException) {
return PurchasesError(PurchasesErrorCode.InsufficientPermissionsError, localizedMessage)
}
return PurchasesError(PurchasesErrorCode.UnknownError, localizedMessage)
}
Expand Down
20 changes: 20 additions & 0 deletions purchases/src/test/java/com/revenuecat/purchases/DispatcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import java.util.concurrent.ExecutorService
import java.util.concurrent.atomic.AtomicReference

@RunWith(AndroidJUnit4::class)
@Config(manifest = Config.NONE)
Expand Down Expand Up @@ -99,4 +100,23 @@ class DispatcherTest {
executorService.shutdownNow()
}
}

@Test
fun securityExceptionsAreCorrectlyConvertedToPurchaseErrors() {
val errorHolder = AtomicReference<PurchasesError>()

val call = object : Dispatcher.AsyncCall() {
override fun call(): HTTPClient.Result {
throw SecurityException("missing permissoins")
}

override fun onError(error: PurchasesError) {
errorHolder.set(error)
}
}

call.run()

assertThat(errorHolder.get().code).isEqualTo(PurchasesErrorCode.InsufficientPermissionsError)
}
}

0 comments on commit 69c934b

Please sign in to comment.