Implemented showing application errors
This commit is contained in:
parent
1d48e617df
commit
98c510f29a
|
@ -3,15 +3,23 @@ package net.codinux.banking.ui.composables
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import net.codinux.banking.ui.dialogs.ApplicationErrorDialog
|
||||||
import net.codinux.banking.ui.dialogs.BankingClientErrorDialog
|
import net.codinux.banking.ui.dialogs.BankingClientErrorDialog
|
||||||
import net.codinux.banking.ui.state.UiState
|
import net.codinux.banking.ui.state.UiState
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun StateHandler(uiState: UiState) {
|
fun StateHandler(uiState: UiState) {
|
||||||
|
val applicationError by uiState.applicationErrorOccurred.collectAsState()
|
||||||
val bankingClientError by uiState.bankingClientErrorOccurred.collectAsState()
|
val bankingClientError by uiState.bankingClientErrorOccurred.collectAsState()
|
||||||
|
|
||||||
bankingClientError?.let {
|
applicationError?.let { error ->
|
||||||
BankingClientErrorDialog(it) {
|
ApplicationErrorDialog(error) {
|
||||||
|
uiState.applicationErrorOccurred.value = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bankingClientError?.let { error ->
|
||||||
|
BankingClientErrorDialog(error) {
|
||||||
uiState.bankingClientErrorOccurred.value = null
|
uiState.bankingClientErrorOccurred.value = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
|
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -1,15 +1,16 @@
|
||||||
package net.codinux.banking.ui.dialogs
|
package net.codinux.banking.ui.dialogs
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import net.codinux.banking.ui.model.BankingClientAction
|
import net.codinux.banking.ui.config.Internationalization
|
||||||
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.Config
|
import net.codinux.banking.ui.model.Config
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun BankingClientErrorDialog(error: BankingClientError, onDismiss: (() -> Unit)? = null) {
|
fun BankingClientErrorDialog(error: BankingClientError, onDismiss: (() -> Unit)? = null) {
|
||||||
val title = when (error.erroneousAction) {
|
val title = when (error.erroneousAction) {
|
||||||
BankingClientAction.AddAccount -> "Konto konnte nicht hinzugefügt werden"
|
BankingClientAction.AddAccount -> Internationalization.ErrorAddAccount
|
||||||
BankingClientAction.UpdateAccountTransactions -> "Umsätze konnten nicht aktualisiert werden"
|
BankingClientAction.UpdateAccountTransactions -> Internationalization.ErrorUpdateAccountTransactions
|
||||||
}
|
}
|
||||||
|
|
||||||
val text = if (error.error.internalError != null) {
|
val text = if (error.error.internalError != null) {
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package net.codinux.banking.ui.model
|
package net.codinux.banking.ui.model.error
|
||||||
|
|
||||||
enum class BankingClientAction {
|
enum class BankingClientAction {
|
||||||
AddAccount,
|
AddAccount,
|
|
@ -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
|
import net.codinux.banking.client.model.response.Error
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package net.codinux.banking.ui.model.error
|
||||||
|
|
||||||
|
enum class ErroneousAction {
|
||||||
|
AddAccount,
|
||||||
|
UpdateAccountTransactions
|
||||||
|
}
|
|
@ -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.FinTsClientConfiguration
|
||||||
import net.codinux.banking.fints.config.FinTsClientOptions
|
import net.codinux.banking.fints.config.FinTsClientOptions
|
||||||
import net.codinux.banking.ui.model.BankInfo
|
import net.codinux.banking.ui.model.BankInfo
|
||||||
import net.codinux.banking.ui.model.BankingClientAction
|
import net.codinux.banking.ui.model.error.BankingClientAction
|
||||||
import net.codinux.banking.ui.model.BankingClientError
|
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.banking.ui.state.UiState
|
||||||
import net.codinux.csv.reader.CsvReader
|
import net.codinux.csv.reader.CsvReader
|
||||||
import net.codinux.log.Log
|
import net.codinux.log.Log
|
||||||
|
@ -64,7 +65,7 @@ class BankingService(
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
log.error(e) { "Could not add account for ${bank.name} $loginName" }
|
log.error(e) { "Could not add account for ${bank.name} $loginName" }
|
||||||
|
|
||||||
// TODO: handle internal error
|
uiState.applicationErrorOccurred(ErroneousAction.AddAccount, e)
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,26 @@ package net.codinux.banking.ui.state
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import net.codinux.banking.client.model.AccountTransaction
|
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() {
|
class UiState : ViewModel() {
|
||||||
|
|
||||||
val transactions = MutableStateFlow<List<AccountTransaction>>(emptyList())
|
val transactions = MutableStateFlow<List<AccountTransaction>>(emptyList())
|
||||||
|
|
||||||
|
val applicationErrorOccurred = MutableStateFlow<ApplicationError?>(null)
|
||||||
|
|
||||||
val bankingClientErrorOccurred = MutableStateFlow<BankingClientError?>(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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue