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 26c09cd4..f7a5435f 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt @@ -25,6 +25,7 @@ import net.dankito.banking.fints.transactions.IAccountTransactionsParser import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser import net.dankito.banking.fints.util.IBase64Service import net.dankito.banking.fints.util.PureKotlinBase64Service +import net.dankito.utils.multiplatform.log.Logger import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.banking.fints.webclient.IWebClient import net.dankito.banking.fints.webclient.KtorWebClient @@ -58,6 +59,14 @@ open class FinTsClient( open val messageLogWithoutSensitiveData: List get() = messageLogCollector.messageLogWithoutSensitiveData + protected open val messageLogAppender: IMessageLogAppender = object : IMessageLogAppender { + + override fun logError(message: String, e: Exception?, logger: Logger?, bank: BankData?) { + messageLogCollector.logError(message, e, logger, bank) + } + + } + /** * Retrieves information about bank (e.g. supported HBCI versions, FinTS server address, @@ -756,14 +765,14 @@ open class FinTsClient( return responseParser.parse(decodedResponse) } catch (e: Exception) { - log.error(e) { "Could not decode responseBody:\r\n'$responseBody'" } + logError("Could not decode responseBody:\r\n'$responseBody'", dialogContext, e) return BankResponse(false, errorMessage = e.getInnerExceptionMessage()) } } else { val bank = dialogContext.bank - log.error(webResponse.error) { "Request to $bank (${bank.finTs3ServerAddress}) failed" } + logError("Request to $bank (${bank.finTs3ServerAddress}) failed", dialogContext, webResponse.error) } return BankResponse(false, errorMessage = webResponse.error?.getInnerExceptionMessage()) @@ -788,6 +797,10 @@ open class FinTsClient( messageLogCollector.addMessageLog(message, type, dialogContext.bank) } + protected open fun logError(message: String, dialogContext: DialogContext, e: Exception?) { + messageLogAppender.logError(message, e, log, dialogContext.bank) + } + protected open fun handleMayRequiresTan(response: BankResponse, dialogContext: DialogContext, callback: (BankResponse) -> Unit) { // TODO: use response from DialogContext diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt index c8d9aa45..5ad69a71 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt @@ -3,6 +3,7 @@ package net.dankito.banking.fints.log import net.dankito.banking.fints.model.BankData import net.dankito.banking.fints.model.MessageLogEntry import net.dankito.banking.fints.model.MessageLogEntryType +import net.dankito.utils.multiplatform.log.Logger import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.utils.multiplatform.Date import net.dankito.utils.multiplatform.getInnerExceptionMessage @@ -39,7 +40,27 @@ open class MessageLogCollector { } - protected open fun removeSensitiveDataFromMessage(message: String, bank: BankData): String { + open fun logError(message: String, e: Exception? = null, logger: Logger? = null, bank: BankData?) { + val prettyPrintMessage = prettyPrintHbciMessage(message) + + val loggerToUse = logger ?: log + if (e != null) { + loggerToUse.error(e) { prettyPrintMessage } + } + else { + loggerToUse.error(prettyPrintMessage) + } + + // TODO: what to do when bank is not set? + messageLog.add(MessageLogEntry(prettyPrintMessage, MessageLogEntryType.Error, Date(), bank)) + } + + + protected open fun removeSensitiveDataFromMessage(message: String, bank: BankData?): String { + if (bank == null) { + return message + } + var prettyPrintMessageWithoutSensitiveData = message .replace(bank.customerId, "") .replace("+" + bank.pin, "+") diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt index 942aebf5..8ba02beb 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt @@ -7,7 +7,7 @@ open class MessageLogEntry( open val message: String, open val type: MessageLogEntryType, open val time: Date, - open val bank: BankData + open val bank: BankData? ) { override fun toString(): String { diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntryType.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntryType.kt index b64a76c6..d658768e 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntryType.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntryType.kt @@ -5,6 +5,8 @@ enum class MessageLogEntryType { Sent, - Received + Received, + + Error } \ No newline at end of file diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntryType.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntryType.kt index a2013687..733426cf 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntryType.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntryType.kt @@ -5,6 +5,8 @@ enum class MessageLogEntryType { Sent, - Received + Received, + + Error } \ No newline at end of file diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt index d84958de..027d9d26 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -799,12 +799,17 @@ open class BankingPresenter( }) return logEntries.map { entry -> - MessageLogEntryDateFormatter.format(entry.time) + " " + getMessageLogPrefix(entry) + "\r\n" + entry.bank.bankCode + " " + entry.message + MessageLogEntryDateFormatter.format(entry.time) + " " + entry.bank.bankCode + " " + getMessageLogPrefix(entry) + "\r\n" + entry.message } } protected open fun getMessageLogPrefix(entry: MessageLogEntry): String { - return "${if (entry.type == MessageLogEntryType.Sent) "Sending" else "Received"} message:" // TODO: translate + // TODO: translate + return when (entry.type) { + MessageLogEntryType.Sent -> "Sending message" + MessageLogEntryType.Received -> "Received message" + MessageLogEntryType.Error -> "Error" + } } diff --git a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt index acaa8c6a..80f6aefc 100644 --- a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt +++ b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt @@ -61,6 +61,7 @@ open class fints4kBankingClient( return when (type) { MessageLogEntryType.Sent -> net.dankito.banking.ui.model.MessageLogEntryType.Sent MessageLogEntryType.Received -> net.dankito.banking.ui.model.MessageLogEntryType.Received + MessageLogEntryType.Error -> net.dankito.banking.ui.model.MessageLogEntryType.Error } }