mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-05 19:08:23 +00:00
Refactor error handling and simplify intent processing
- Introduce `FailedMigrationException` to handle empty or missing private keys in `DataStoreAccess`. - Update `DataStoreAccess.getEncryptedKey` to throw `FailedMigrationException` instead of `CancellationException`. - Simplify `IntentUtils.getIntentData` by removing the global try-catch block and allowing specific exceptions to propagate. - Update decryption logic in `IntentUtils` to rethrow `FailedMigrationException` while continuing to log other exceptions.
This commit is contained in:
@@ -8,7 +8,6 @@ import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStoreFile
|
||||
import java.util.concurrent.CancellationException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
@@ -40,8 +39,11 @@ object DataStoreAccess {
|
||||
suspend fun getEncryptedKey(context: Context, npub: String, key: Preferences.Key<String>): String {
|
||||
val prefs = getDataStore(context, npub).data
|
||||
.first()
|
||||
if (prefs.asMap().keys.isEmpty()) {
|
||||
throw FailedMigrationException("Datastore is empty")
|
||||
}
|
||||
|
||||
val encrypted = prefs[key] ?: throw CancellationException("Private key not found in Datastore")
|
||||
val encrypted = prefs[key] ?: throw FailedMigrationException("Private key not found in Datastore")
|
||||
return SecureCryptoHelper.decrypt(encrypted)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import androidx.core.net.toUri
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import com.greenart7c3.nostrsigner.Amber
|
||||
import com.greenart7c3.nostrsigner.BuildConfig
|
||||
import com.greenart7c3.nostrsigner.FailedMigrationException
|
||||
import com.greenart7c3.nostrsigner.LocalPreferences
|
||||
import com.greenart7c3.nostrsigner.R
|
||||
import com.greenart7c3.nostrsigner.database.ApplicationEntity
|
||||
@@ -367,6 +368,7 @@ object IntentUtils {
|
||||
pubKey,
|
||||
) ?: "Could not decrypt the message"
|
||||
} catch (e: Exception) {
|
||||
if (e is FailedMigrationException) throw e
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
val database = Amber.instance.getLogDatabase(account.npub)
|
||||
database.dao().insertLog(
|
||||
@@ -566,56 +568,41 @@ object IntentUtils {
|
||||
route: String?,
|
||||
currentLoggedInAccount: Account,
|
||||
): IntentData? {
|
||||
try {
|
||||
if (intent.data == null) {
|
||||
if (intent.data == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (intent.extras?.getString("id") == null) {
|
||||
intent.putExtra("id", UUID.randomUUID().toString().substring(0, 6))
|
||||
}
|
||||
|
||||
val id = intent.extras?.getString("id") ?: ""
|
||||
val mainActivity = Amber.instance.getMainActivity()
|
||||
mainActivity?.let {
|
||||
if (mainActivity.mainViewModel.intents.value.any { it.id == id }) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
if (intent.extras?.getString("id") == null) {
|
||||
intent.putExtra("id", UUID.randomUUID().toString().substring(0, 6))
|
||||
var localAccount = currentLoggedInAccount
|
||||
if (intent.getStringExtra("current_user") != null) {
|
||||
var npub = intent.getStringExtra("current_user")
|
||||
if (npub != null) {
|
||||
npub = parsePubKey(npub)
|
||||
}
|
||||
|
||||
val id = intent.extras?.getString("id") ?: ""
|
||||
val mainActivity = Amber.instance.getMainActivity()
|
||||
mainActivity?.let {
|
||||
if (mainActivity.mainViewModel.intents.value.any { it.id == id }) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
var localAccount = currentLoggedInAccount
|
||||
if (intent.getStringExtra("current_user") != null) {
|
||||
var npub = intent.getStringExtra("current_user")
|
||||
if (npub != null) {
|
||||
npub = parsePubKey(npub)
|
||||
}
|
||||
LocalPreferences.loadFromEncryptedStorageSync(context, npub)?.let {
|
||||
localAccount = it
|
||||
}
|
||||
}
|
||||
|
||||
if (intent.dataString?.startsWith("nostrconnect:") == true) {
|
||||
NostrConnectUtils.getIntentFromNostrConnect(intent, localAccount)
|
||||
} else if (intent.extras?.getString(Browser.EXTRA_APPLICATION_ID) == null) {
|
||||
return getIntentDataFromIntent(context, intent, packageName, route, localAccount)
|
||||
} else {
|
||||
return getIntentDataWithoutExtras(context, intent.data?.toString() ?: "", intent, packageName, route, localAccount)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
LocalPreferences.allSavedAccounts(Amber.instance).forEach {
|
||||
Amber.instance.getLogDatabase(it.npub).dao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = "IntentUtils",
|
||||
type = "Error",
|
||||
message = e.stackTraceToString(),
|
||||
time = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
}
|
||||
LocalPreferences.loadFromEncryptedStorageSync(context, npub)?.let {
|
||||
localAccount = it
|
||||
}
|
||||
}
|
||||
|
||||
if (intent.dataString?.startsWith("nostrconnect:") == true) {
|
||||
NostrConnectUtils.getIntentFromNostrConnect(intent, localAccount)
|
||||
} else if (intent.extras?.getString(Browser.EXTRA_APPLICATION_ID) == null) {
|
||||
return getIntentDataFromIntent(context, intent, packageName, route, localAccount)
|
||||
} else {
|
||||
return getIntentDataWithoutExtras(context, intent.data?.toString() ?: "", intent, packageName, route, localAccount)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user