Skip to content

Commit

Permalink
fix: Android reliability (#2250)
Browse files Browse the repository at this point in the history
* feat: for bang bank conversion throw exception so we can see exact error if we want to crash anyway

* feat: show proper error in case of new keyset seed fetch failed
  • Loading branch information
Dmitry-Borodin authored Jan 2, 2024
1 parent 3d84559 commit 7f6858a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.parity.signer.uniffi.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import java.lang.RuntimeException

/**
* Wrapper for uniffi calls into rust. Made for centralized handling errors
Expand Down Expand Up @@ -373,6 +374,22 @@ fun <T> UniffiResult<T>.mapError(): T? {
}
}

/**
* Dangerous cast to non error so we can see a reason in crash logs
*/
@Deprecated("Handle error state")
fun <T> UniffiResult<T>.mapErrorForce(): T {
return when (this) {
is UniffiResult.Error -> {
throw RuntimeException("uniffi interaction exception $error")
}

is UniffiResult.Success -> {
result
}
}
}

fun <T> UniffiResult<T>.toOperationResult(): OperationResult<T, ErrorDisplayed> {
return when (this) {
is UniffiResult.Error -> OperationResult.Err(this.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package io.parity.signer.domain.usecases
import io.parity.signer.domain.NetworkModel
import io.parity.signer.domain.backend.UniffiInteractor
import io.parity.signer.domain.backend.mapError
import io.parity.signer.domain.backend.mapErrorForce
import kotlinx.coroutines.runBlocking


class AllNetworksUseCase(val uniffiInteractor: UniffiInteractor) {
private var allNetworks: List<NetworkModel> = runBlocking { getNetworks() }!!
private var allNetworks: List<NetworkModel> = runBlocking { getNetworks() }

fun updateCache(): Unit {
allNetworks = runBlocking { getNetworks() }!!
allNetworks = runBlocking { getNetworks() }
}

fun getAllNetworks(): List<NetworkModel> = allNetworks
Expand All @@ -20,7 +21,7 @@ class AllNetworksUseCase(val uniffiInteractor: UniffiInteractor) {
fun getDefaultPreselectedNetworks(): List<NetworkModel> = allNetworks
.filter { preselectedkeys.contains(it.title) }

private suspend fun getNetworks(): List<NetworkModel>? {
return uniffiInteractor.getAllNetworks().mapError()
private suspend fun getNetworks(): List<NetworkModel> {
return uniffiInteractor.getAllNetworks().mapErrorForce()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import io.parity.signer.domain.backend.toOperationResult
import io.parity.signer.domain.popUpToTop
import io.parity.signer.screens.error.handleErrorAppState
import io.parity.signer.screens.keysets.create.backupstepscreens.NewKeySetBackupBottomSheet
import io.parity.signer.screens.keysets.create.backupstepscreens.NewKeySetBackupScreen
import io.parity.signer.screens.keysets.create.backupstepscreens.NewKeySetSelectNetworkScreen
Expand All @@ -37,10 +39,7 @@ fun NewKeysetSubgraph(
mutableStateOf("")
}
val seedPhrase = rememberSaveable() {
vm.createNewSeedPhrase() ?: run {
coreNavController.popBackStack()
""
}
vm.createNewSeedPhrase().toOperationResult().handleErrorAppState(coreNavController) ?: ""
}

NavHost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.parity.signer.screens.keysets.create

import androidx.lifecycle.ViewModel
import io.parity.signer.dependencygraph.ServiceLocator
import io.parity.signer.domain.backend.mapError
import io.parity.signer.domain.backend.UniffiResult
import kotlinx.coroutines.runBlocking


Expand All @@ -13,9 +13,9 @@ class NewKeysetNameViewModel : ViewModel() {
val seedNames =
ServiceLocator.seedStorage.lastKnownSeedNames

fun createNewSeedPhrase(): String? {
fun createNewSeedPhrase(): UniffiResult<String> {
return runBlocking {
uniffi.createNewSeedPhrase().mapError()
uniffi.createNewSeedPhrase()
}
}
}

0 comments on commit 7f6858a

Please sign in to comment.