Implemented adding errors to MessageLog

This commit is contained in:
dankito 2020-12-06 21:47:49 +01:00
parent ae7f6328e3
commit 55f5603cb9
7 changed files with 52 additions and 8 deletions

View File

@ -25,6 +25,7 @@ import net.dankito.banking.fints.transactions.IAccountTransactionsParser
import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser
import net.dankito.banking.fints.util.IBase64Service import net.dankito.banking.fints.util.IBase64Service
import net.dankito.banking.fints.util.PureKotlinBase64Service import net.dankito.banking.fints.util.PureKotlinBase64Service
import net.dankito.utils.multiplatform.log.Logger
import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.utils.multiplatform.log.LoggerFactory
import net.dankito.banking.fints.webclient.IWebClient import net.dankito.banking.fints.webclient.IWebClient
import net.dankito.banking.fints.webclient.KtorWebClient import net.dankito.banking.fints.webclient.KtorWebClient
@ -58,6 +59,14 @@ open class FinTsClient(
open val messageLogWithoutSensitiveData: List<MessageLogEntry> open val messageLogWithoutSensitiveData: List<MessageLogEntry>
get() = messageLogCollector.messageLogWithoutSensitiveData 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, * Retrieves information about bank (e.g. supported HBCI versions, FinTS server address,
@ -756,14 +765,14 @@ open class FinTsClient(
return responseParser.parse(decodedResponse) return responseParser.parse(decodedResponse)
} catch (e: Exception) { } 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()) return BankResponse(false, errorMessage = e.getInnerExceptionMessage())
} }
} }
else { else {
val bank = dialogContext.bank 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()) return BankResponse(false, errorMessage = webResponse.error?.getInnerExceptionMessage())
@ -788,6 +797,10 @@ open class FinTsClient(
messageLogCollector.addMessageLog(message, type, dialogContext.bank) 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 protected open fun handleMayRequiresTan(response: BankResponse, dialogContext: DialogContext, callback: (BankResponse) -> Unit) { // TODO: use response from DialogContext

View File

@ -3,6 +3,7 @@ package net.dankito.banking.fints.log
import net.dankito.banking.fints.model.BankData import net.dankito.banking.fints.model.BankData
import net.dankito.banking.fints.model.MessageLogEntry import net.dankito.banking.fints.model.MessageLogEntry
import net.dankito.banking.fints.model.MessageLogEntryType 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.log.LoggerFactory
import net.dankito.utils.multiplatform.Date import net.dankito.utils.multiplatform.Date
import net.dankito.utils.multiplatform.getInnerExceptionMessage 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 var prettyPrintMessageWithoutSensitiveData = message
.replace(bank.customerId, "<customer_id>") .replace(bank.customerId, "<customer_id>")
.replace("+" + bank.pin, "+<pin>") .replace("+" + bank.pin, "+<pin>")

View File

@ -7,7 +7,7 @@ open class MessageLogEntry(
open val message: String, open val message: String,
open val type: MessageLogEntryType, open val type: MessageLogEntryType,
open val time: Date, open val time: Date,
open val bank: BankData open val bank: BankData?
) { ) {
override fun toString(): String { override fun toString(): String {

View File

@ -5,6 +5,8 @@ enum class MessageLogEntryType {
Sent, Sent,
Received Received,
Error
} }

View File

@ -5,6 +5,8 @@ enum class MessageLogEntryType {
Sent, Sent,
Received Received,
Error
} }

View File

@ -799,12 +799,17 @@ open class BankingPresenter(
}) })
return logEntries.map { entry -> 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 { 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"
}
} }

View File

@ -61,6 +61,7 @@ open class fints4kBankingClient(
return when (type) { return when (type) {
MessageLogEntryType.Sent -> net.dankito.banking.ui.model.MessageLogEntryType.Sent MessageLogEntryType.Sent -> net.dankito.banking.ui.model.MessageLogEntryType.Sent
MessageLogEntryType.Received -> net.dankito.banking.ui.model.MessageLogEntryType.Received MessageLogEntryType.Received -> net.dankito.banking.ui.model.MessageLogEntryType.Received
MessageLogEntryType.Error -> net.dankito.banking.ui.model.MessageLogEntryType.Error
} }
} }