From ec3cdb1c394e24e1e0f6852eb54d3722e216dc18 Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 19 Sep 2020 03:15:56 +0200 Subject: [PATCH] Added convenience constructor for when an error occurred --- .../utils/multiplatform/CommonExtensions.kt | 4 +-- .../net/dankito/banking/fints/FinTsClient.kt | 2 +- .../responses/GetTransactionsResponse.kt | 6 ++++- .../dankito/banking/hbci4jBankingClient.kt | 27 +++++++++---------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/CommonExtensions.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/CommonExtensions.kt index 5c976b45..6453e456 100644 --- a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/CommonExtensions.kt +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/CommonExtensions.kt @@ -8,8 +8,8 @@ val Char.isUpperCase: Boolean get() = isLowerCase == false -fun Throwable?.getInnerExceptionMessage(maxDepth: Int = 3): String? { - return this?.getInnerException(maxDepth)?.message +fun Throwable.getInnerExceptionMessage(maxDepth: Int = 3): String { + return this.getInnerException(maxDepth).message ?: "" } fun Throwable.getInnerException(maxDepth: Int = 3): Throwable { diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt index abaafa6c..d750f1c0 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt @@ -733,7 +733,7 @@ open class FinTsClient( log.error(webResponse.error) { "Request to $bank (${bank.finTs3ServerAddress}) failed" } } - return Response(false, errorMessage = webResponse.error.getInnerExceptionMessage()) + return Response(false, errorMessage = webResponse.error?.getInnerExceptionMessage()) } protected open fun decodeBase64Response(responseBody: String): String { diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/responses/GetTransactionsResponse.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/responses/GetTransactionsResponse.kt index a907157a..6fbfee0b 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/responses/GetTransactionsResponse.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/responses/GetTransactionsResponse.kt @@ -1,15 +1,19 @@ package net.dankito.banking.ui.model.responses import net.dankito.banking.ui.model.RetrievedAccountData +import net.dankito.banking.ui.model.TypedBankAccount open class GetTransactionsResponse( errorToShowToUser: String?, - open val retrievedData: List = listOf(), + open val retrievedData: List, userCancelledAction: Boolean = false, open val tanRequiredButWeWereToldToAbortIfSo: Boolean = false ) : BankingClientResponse(true /* any value */, errorToShowToUser, userCancelledAction) { + constructor(account: TypedBankAccount, errorToShowToUser: String) : this(errorToShowToUser, listOf(RetrievedAccountData(account, false, null, listOf(), listOf()))) + + override val isSuccessful: Boolean get() = errorToShowToUser == null && retrievedData.isNotEmpty() && retrievedData.none { it.successfullyRetrievedData == false } diff --git a/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt b/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt index 90f9390d..d49931a2 100644 --- a/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt +++ b/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt @@ -85,7 +85,7 @@ open class hbci4jBankingClient( } } - return AddAccountResponse(connection.error.getInnerExceptionMessage(), customer) + return AddAccountResponse(connection.error?.getInnerExceptionMessage() ?: "Could not connect", customer) } protected open fun tryToRetrieveAccountTransactionsForAddedAccounts(customer: TypedCustomer): AddAccountResponse { @@ -142,18 +142,17 @@ open class hbci4jBankingClient( } } - protected open fun getTransactions(bankAccount: TypedBankAccount, parameter: GetTransactionsParameter): GetTransactionsResponse { + protected open fun getTransactions(account: TypedBankAccount, parameter: GetTransactionsParameter): GetTransactionsResponse { val connection = connect() - val retrievingDataFailed = listOf(RetrievedAccountData(bankAccount, false, null, listOf(), listOf())) connection.handle?.let { handle -> try { - val (nullableBalanceJob, accountTransactionsJob, status) = executeJobsForGetAccountingEntries(handle, bankAccount, parameter) + val (nullableBalanceJob, accountTransactionsJob, status) = executeJobsForGetAccountingEntries(handle, account, parameter) // Pruefen, ob die Kommunikation mit der Bank grundsaetzlich geklappt hat if (!status.isOK) { log.error("Could not connect to bank ${credentials.bankCode} $status: ${status.errorString}") - return GetTransactionsResponse("Could not connect to bank ${credentials.bankCode}: $status", retrievingDataFailed) + return GetTransactionsResponse(account,"Could not connect to bank ${credentials.bankCode}: $status") } // Auswertung des Saldo-Abrufs. @@ -161,8 +160,8 @@ open class hbci4jBankingClient( if (parameter.alsoRetrieveBalance && nullableBalanceJob != null) { val balanceResult = nullableBalanceJob.jobResult as GVRSaldoReq if(balanceResult.isOK == false) { - log.error("Could not fetch balance of bank account $bankAccount: $balanceResult", balanceResult.getJobStatus().exceptions) - return GetTransactionsResponse("Could not fetch balance of bank account $bankAccount: $balanceResult", retrievingDataFailed) + log.error("Could not fetch balance of bank account $account: $balanceResult", balanceResult.getJobStatus().exceptions) + return GetTransactionsResponse(account,"Could not fetch balance of bank account $account: $balanceResult") } balance = balanceResult.entries[0].ready.value.bigDecimalValue.toBigDecimal() @@ -175,16 +174,16 @@ open class hbci4jBankingClient( // Pruefen, ob der Abruf der Umsaetze geklappt hat if (result.isOK == false) { - log.error("Could not get fetch account transactions of bank account $bankAccount: $result", result.getJobStatus().exceptions) - return GetTransactionsResponse("Could not fetch account transactions of bank account $bankAccount: $result", retrievingDataFailed) + log.error("Could not get fetch account transactions of bank account $account: $result", result.getJobStatus().exceptions) + return GetTransactionsResponse(account,"Could not fetch account transactions of bank account $account: $result") } - return GetTransactionsResponse(null, listOf(RetrievedAccountData(bankAccount, true, balance.toBigDecimal(), - accountTransactionMapper.mapAccountTransactions(bankAccount, result), listOf()))) + return GetTransactionsResponse(null, listOf(RetrievedAccountData(account, true, balance.toBigDecimal(), + accountTransactionMapper.mapAccountTransactions(account, result), listOf()))) } catch(e: Exception) { log.error("Could not get accounting details for bank ${credentials.bankCode}", e) - return GetTransactionsResponse(e.getInnerExceptionMessage(), retrievingDataFailed) + return GetTransactionsResponse(account, e.getInnerExceptionMessage()) } finally { closeConnection(connection) @@ -193,7 +192,7 @@ open class hbci4jBankingClient( closeConnection(connection) - return GetTransactionsResponse(connection.error.getInnerExceptionMessage(), retrievingDataFailed) + return GetTransactionsResponse(account, connection.error?.getInnerExceptionMessage() ?: "Could not connect") } protected open fun executeJobsForGetAccountingEntries(handle: HBCIHandler, bankAccount: TypedBankAccount, parameter: GetTransactionsParameter): Triple { @@ -252,7 +251,7 @@ open class hbci4jBankingClient( } } - return BankingClientResponse(false, connection.error.getInnerExceptionMessage() ?: "Could not connect") + return BankingClientResponse(false, connection.error?.getInnerExceptionMessage() ?: "Could not connect") } protected open fun createTransferCashJob(handle: HBCIHandler, data: TransferMoneyData) {