Implemented showing application errors

This commit is contained in:
dankito 2024-08-26 19:08:28 +02:00
parent 1d48e617df
commit 98c510f29a
10 changed files with 78 additions and 12 deletions

View File

@ -3,15 +3,23 @@ package net.codinux.banking.ui.composables
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import net.codinux.banking.ui.dialogs.ApplicationErrorDialog
import net.codinux.banking.ui.dialogs.BankingClientErrorDialog
import net.codinux.banking.ui.state.UiState
@Composable
fun StateHandler(uiState: UiState) {
val applicationError by uiState.applicationErrorOccurred.collectAsState()
val bankingClientError by uiState.bankingClientErrorOccurred.collectAsState()
bankingClientError?.let {
BankingClientErrorDialog(it) {
applicationError?.let { error ->
ApplicationErrorDialog(error) {
uiState.applicationErrorOccurred.value = null
}
}
bankingClientError?.let { error ->
BankingClientErrorDialog(error) {
uiState.bankingClientErrorOccurred.value = null
}
}

View File

@ -0,0 +1,9 @@
package net.codinux.banking.ui.config
object Internationalization {
const val ErrorAddAccount = "Konto konnte nicht hinzugefügt werden"
const val ErrorUpdateAccountTransactions = "Umsätze konnten nicht aktualisiert werden"
}

View File

@ -0,0 +1,18 @@
package net.codinux.banking.ui.dialogs
import androidx.compose.runtime.Composable
import net.codinux.banking.ui.config.Internationalization
import net.codinux.banking.ui.model.error.ApplicationError
import net.codinux.banking.ui.model.error.ErroneousAction
@Composable
fun ApplicationErrorDialog(error: ApplicationError, onDismiss: (() -> Unit)? = null) {
val title = when (error.erroneousAction) {
ErroneousAction.AddAccount -> Internationalization.ErrorAddAccount
ErroneousAction.UpdateAccountTransactions -> Internationalization.ErrorUpdateAccountTransactions
}
// add exception stacktrace?
ErrorDialog(error.errorMessage, title, onDismiss = onDismiss)
}

View File

@ -1,15 +1,16 @@
package net.codinux.banking.ui.dialogs
import androidx.compose.runtime.Composable
import net.codinux.banking.ui.model.BankingClientAction
import net.codinux.banking.ui.model.BankingClientError
import net.codinux.banking.ui.config.Internationalization
import net.codinux.banking.ui.model.error.BankingClientAction
import net.codinux.banking.ui.model.error.BankingClientError
import net.codinux.banking.ui.model.Config
@Composable
fun BankingClientErrorDialog(error: BankingClientError, onDismiss: (() -> Unit)? = null) {
val title = when (error.erroneousAction) {
BankingClientAction.AddAccount -> "Konto konnte nicht hinzugefügt werden"
BankingClientAction.UpdateAccountTransactions -> "Umsätze konnten nicht aktualisiert werden"
BankingClientAction.AddAccount -> Internationalization.ErrorAddAccount
BankingClientAction.UpdateAccountTransactions -> Internationalization.ErrorUpdateAccountTransactions
}
val text = if (error.error.internalError != null) {

View File

@ -0,0 +1,9 @@
package net.codinux.banking.ui.model.error
data class ApplicationError(
val erroneousAction: ErroneousAction,
val errorMessage: String,
val exception: Throwable? = null
) {
override fun toString() = "$erroneousAction $errorMessage"
}

View File

@ -1,4 +1,4 @@
package net.codinux.banking.ui.model
package net.codinux.banking.ui.model.error
enum class BankingClientAction {
AddAccount,

View File

@ -1,4 +1,4 @@
package net.codinux.banking.ui.model
package net.codinux.banking.ui.model.error
import net.codinux.banking.client.model.response.Error

View File

@ -0,0 +1,6 @@
package net.codinux.banking.ui.model.error
enum class ErroneousAction {
AddAccount,
UpdateAccountTransactions
}

View File

@ -12,8 +12,9 @@ import net.codinux.banking.client.model.response.ResponseType
import net.codinux.banking.fints.config.FinTsClientConfiguration
import net.codinux.banking.fints.config.FinTsClientOptions
import net.codinux.banking.ui.model.BankInfo
import net.codinux.banking.ui.model.BankingClientAction
import net.codinux.banking.ui.model.BankingClientError
import net.codinux.banking.ui.model.error.BankingClientAction
import net.codinux.banking.ui.model.error.BankingClientError
import net.codinux.banking.ui.model.error.ErroneousAction
import net.codinux.banking.ui.state.UiState
import net.codinux.csv.reader.CsvReader
import net.codinux.log.Log
@ -64,7 +65,7 @@ class BankingService(
} catch (e: Throwable) {
log.error(e) { "Could not add account for ${bank.name} $loginName" }
// TODO: handle internal error
uiState.applicationErrorOccurred(ErroneousAction.AddAccount, e)
return false
}

View File

@ -3,12 +3,26 @@ package net.codinux.banking.ui.state
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import net.codinux.banking.client.model.AccountTransaction
import net.codinux.banking.ui.model.BankingClientError
import net.codinux.banking.ui.model.error.ApplicationError
import net.codinux.banking.ui.model.error.BankingClientError
import net.codinux.banking.ui.model.error.ErroneousAction
class UiState : ViewModel() {
val transactions = MutableStateFlow<List<AccountTransaction>>(emptyList())
val applicationErrorOccurred = MutableStateFlow<ApplicationError?>(null)
val bankingClientErrorOccurred = MutableStateFlow<BankingClientError?>(null)
fun applicationErrorOccurred(erroneousAction: ErroneousAction, exception: Throwable, errorMessage: String? = null) {
val message = errorMessage
?: exception.message ?: exception::class.simpleName // TODO: find a better way to get error message from exception
if (message != null) {
applicationErrorOccurred.value = ApplicationError(erroneousAction, message, exception)
}
}
}