Added convenience constructor for when an error occurred

This commit is contained in:
dankito 2020-09-19 03:15:56 +02:00
parent 2d4499514a
commit ec3cdb1c39
4 changed files with 21 additions and 18 deletions

View File

@ -8,8 +8,8 @@ val Char.isUpperCase: Boolean
get() = isLowerCase == false get() = isLowerCase == false
fun Throwable?.getInnerExceptionMessage(maxDepth: Int = 3): String? { fun Throwable.getInnerExceptionMessage(maxDepth: Int = 3): String {
return this?.getInnerException(maxDepth)?.message return this.getInnerException(maxDepth).message ?: ""
} }
fun Throwable.getInnerException(maxDepth: Int = 3): Throwable { fun Throwable.getInnerException(maxDepth: Int = 3): Throwable {

View File

@ -733,7 +733,7 @@ open class FinTsClient(
log.error(webResponse.error) { "Request to $bank (${bank.finTs3ServerAddress}) failed" } 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 { protected open fun decodeBase64Response(responseBody: String): String {

View File

@ -1,15 +1,19 @@
package net.dankito.banking.ui.model.responses package net.dankito.banking.ui.model.responses
import net.dankito.banking.ui.model.RetrievedAccountData import net.dankito.banking.ui.model.RetrievedAccountData
import net.dankito.banking.ui.model.TypedBankAccount
open class GetTransactionsResponse( open class GetTransactionsResponse(
errorToShowToUser: String?, errorToShowToUser: String?,
open val retrievedData: List<RetrievedAccountData> = listOf(), open val retrievedData: List<RetrievedAccountData>,
userCancelledAction: Boolean = false, userCancelledAction: Boolean = false,
open val tanRequiredButWeWereToldToAbortIfSo: Boolean = false open val tanRequiredButWeWereToldToAbortIfSo: Boolean = false
) : BankingClientResponse(true /* any value */, errorToShowToUser, userCancelledAction) { ) : BankingClientResponse(true /* any value */, errorToShowToUser, userCancelledAction) {
constructor(account: TypedBankAccount, errorToShowToUser: String) : this(errorToShowToUser, listOf(RetrievedAccountData(account, false, null, listOf(), listOf())))
override val isSuccessful: Boolean override val isSuccessful: Boolean
get() = errorToShowToUser == null && retrievedData.isNotEmpty() && retrievedData.none { it.successfullyRetrievedData == false } get() = errorToShowToUser == null && retrievedData.isNotEmpty() && retrievedData.none { it.successfullyRetrievedData == false }

View File

@ -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 { 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 connection = connect()
val retrievingDataFailed = listOf(RetrievedAccountData(bankAccount, false, null, listOf(), listOf()))
connection.handle?.let { handle -> connection.handle?.let { handle ->
try { 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 // Pruefen, ob die Kommunikation mit der Bank grundsaetzlich geklappt hat
if (!status.isOK) { if (!status.isOK) {
log.error("Could not connect to bank ${credentials.bankCode} $status: ${status.errorString}") 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. // Auswertung des Saldo-Abrufs.
@ -161,8 +160,8 @@ open class hbci4jBankingClient(
if (parameter.alsoRetrieveBalance && nullableBalanceJob != null) { if (parameter.alsoRetrieveBalance && nullableBalanceJob != null) {
val balanceResult = nullableBalanceJob.jobResult as GVRSaldoReq val balanceResult = nullableBalanceJob.jobResult as GVRSaldoReq
if(balanceResult.isOK == false) { if(balanceResult.isOK == false) {
log.error("Could not fetch balance of bank account $bankAccount: $balanceResult", balanceResult.getJobStatus().exceptions) log.error("Could not fetch balance of bank account $account: $balanceResult", balanceResult.getJobStatus().exceptions)
return GetTransactionsResponse("Could not fetch balance of bank account $bankAccount: $balanceResult", retrievingDataFailed) return GetTransactionsResponse(account,"Could not fetch balance of bank account $account: $balanceResult")
} }
balance = balanceResult.entries[0].ready.value.bigDecimalValue.toBigDecimal() balance = balanceResult.entries[0].ready.value.bigDecimalValue.toBigDecimal()
@ -175,16 +174,16 @@ open class hbci4jBankingClient(
// Pruefen, ob der Abruf der Umsaetze geklappt hat // Pruefen, ob der Abruf der Umsaetze geklappt hat
if (result.isOK == false) { if (result.isOK == false) {
log.error("Could not get fetch account transactions of bank account $bankAccount: $result", result.getJobStatus().exceptions) log.error("Could not get fetch account transactions of bank account $account: $result", result.getJobStatus().exceptions)
return GetTransactionsResponse("Could not fetch account transactions of bank account $bankAccount: $result", retrievingDataFailed) return GetTransactionsResponse(account,"Could not fetch account transactions of bank account $account: $result")
} }
return GetTransactionsResponse(null, listOf(RetrievedAccountData(bankAccount, true, balance.toBigDecimal(), return GetTransactionsResponse(null, listOf(RetrievedAccountData(account, true, balance.toBigDecimal(),
accountTransactionMapper.mapAccountTransactions(bankAccount, result), listOf()))) accountTransactionMapper.mapAccountTransactions(account, result), listOf())))
} }
catch(e: Exception) { catch(e: Exception) {
log.error("Could not get accounting details for bank ${credentials.bankCode}", e) log.error("Could not get accounting details for bank ${credentials.bankCode}", e)
return GetTransactionsResponse(e.getInnerExceptionMessage(), retrievingDataFailed) return GetTransactionsResponse(account, e.getInnerExceptionMessage())
} }
finally { finally {
closeConnection(connection) closeConnection(connection)
@ -193,7 +192,7 @@ open class hbci4jBankingClient(
closeConnection(connection) 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<HBCIJob?, HBCIJob, HBCIExecStatus> { protected open fun executeJobsForGetAccountingEntries(handle: HBCIHandler, bankAccount: TypedBankAccount, parameter: GetTransactionsParameter): Triple<HBCIJob?, HBCIJob, HBCIExecStatus> {
@ -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) { protected open fun createTransferCashJob(handle: HBCIHandler, data: TransferMoneyData) {