Implemented telling user when saving to db failed

This commit is contained in:
dankito 2024-09-23 23:30:11 +02:00
parent 5a0ade46b2
commit c6f4b6d250
4 changed files with 19 additions and 9 deletions

View File

@ -13,6 +13,8 @@ object Internationalization {
const val ErrorTransferMoney = "Überweisung konnte nicht ausgeführt werden"
const val SaveToDatabase = "Daten konnten nicht in der Datenbank gespeichert werden"
const val ErrorBiometricAuthentication = "Biometrische Authentifizierung fehlgeschlagen"

View File

@ -11,6 +11,7 @@ fun ApplicationErrorDialog(error: ApplicationError, onDismiss: (() -> Unit)? = n
ErroneousAction.AddAccount -> Internationalization.ErrorAddAccount
ErroneousAction.UpdateAccountTransactions -> Internationalization.ErrorUpdateAccountTransactions
ErroneousAction.TransferMoney -> Internationalization.ErrorTransferMoney
ErroneousAction.SaveToDatabase -> Internationalization.SaveToDatabase
ErroneousAction.BiometricAuthentication -> Internationalization.ErrorBiometricAuthentication
}

View File

@ -7,5 +7,7 @@ enum class ErroneousAction {
TransferMoney,
SaveToDatabase,
BiometricAuthentication
}

View File

@ -129,7 +129,7 @@ class BankingService(
notifyBanksListUpdated()
} catch (e: Throwable) {
// showAndLogError("Could not update bank $bank", e)
showAndLogError(ErroneousAction.SaveToDatabase, "Could not update bank $account", "Bankzugangsdaten konnten nicht aktualisisert werden", e)
}
}
@ -142,6 +142,7 @@ class BankingService(
updateBanksInUi(uiState.banks.value.toMutableList().also { it.remove(bank) })
} catch (e: Throwable) {
log.error(e) { "Could not delete bank ${bank.displayName}" }
showAndLogError(ErroneousAction.SaveToDatabase, "Could not delete bank ${bank.displayName}", "Fehler beim Löschen der Bankzugangsdaten für ${bank.displayName}", e)
}
}
@ -166,14 +167,12 @@ class BankingService(
if (response.type == ResponseType.Success && response.data != null) {
handleSuccessfulGetAccountDataResponse(response.data!!)
} else {
handleUnsuccessfulBankingClientResponse(BankingClientAction.AddAccount, response)
handleUnsuccessfulBankingClientResponse(BankingClientAction.AddAccount, bank.name, response)
}
return response.type == ResponseType.Success
} catch (e: Throwable) {
log.error(e) { "Could not add account for ${bank.name} $loginName" }
uiState.applicationErrorOccurred(ErroneousAction.AddAccount, null, e)
showAndLogError(ErroneousAction.AddAccount, "Could not add account for ${bank.name} $loginName", "Konto für ${bank.name} konnte nicht hinzugefügt werden", e)
return false
}
@ -226,7 +225,7 @@ class BankingService(
if (response.type == ResponseType.Success && response.data != null) {
handleSuccessfulUpdateAccountTransactionsResponse(bank, response.data!!)
} else {
handleUnsuccessfulBankingClientResponse(BankingClientAction.UpdateAccountTransactions, response)
handleUnsuccessfulBankingClientResponse(BankingClientAction.UpdateAccountTransactions, bankAccount?.displayName ?: bank.displayName, response)
}
} catch (e: Throwable) {
log.error(e) { "Could not update account transactions for $bank" }
@ -352,7 +351,7 @@ class BankingService(
))
if (response.error != null) {
handleUnsuccessfulBankingClientResponse(BankingClientAction.TransferMoney, response)
handleUnsuccessfulBankingClientResponse(BankingClientAction.TransferMoney, account.displayName, response)
} else if (response.type == ResponseType.Success) {
uiState.dispatchTransferredMoney(TransferredMoneyEvent(recipientName, amount, currency))
@ -363,8 +362,8 @@ class BankingService(
}
private fun handleUnsuccessfulBankingClientResponse(action: BankingClientAction, response: Response<*>) {
log.error { "$action was not successful: $response" }
private fun handleUnsuccessfulBankingClientResponse(action: BankingClientAction, accountOrBankName: String, response: Response<*>) {
log.error { "$action was not successful for $accountOrBankName: $response" }
response.error?.let { error ->
if (error.type != ErrorType.UserCancelledAction) { // the user knows that she cancelled the action
@ -391,6 +390,12 @@ class BankingService(
}
}
private fun showAndLogError(action: ErroneousAction, logMessage: String, messageToShowToUser: String? = null, exception: Throwable? = null) {
log.error(exception) { logMessage }
uiState.applicationErrorOccurred(action, messageToShowToUser, exception)
}
private suspend fun readTransactionsFromCsv(): List<AccountTransaction> {
val csv = Res.readBytes("files/transactions.csv").decodeToString()