Commit d6661bf5 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Improve sftp exception handling

parent b7778b13
......@@ -105,25 +105,31 @@ class FileValidation {
val dBuilder = dbFactory.newDocumentBuilder()
try {
dBuilder.parse(it)
Pair(Message(format, file.path), Report(
id = file.name,
status = ReportStatus.success,
message = ReportMessages.validatedFile(file.path, format)
))
} catch (ex: IOException) {
Pair(Message(Formats.error, file.path),
Report(
id = file.name,
status = ReportStatus.failure,
message = ReportMessages.formatError(file.path, format, ex.localizedMessage)
))
Pair(
Message(format, file.path), Report(
id = file.name,
status = ReportStatus.success,
message = ReportMessages.validatedFile(file.path, format)
)
)
} catch (ex: SAXException) {
Pair(Message(Formats.error, file.path),
Pair(
Message(Formats.error, file.path),
Report(
id = file.name,
status = ReportStatus.failure,
message = ReportMessages.formatError(file.path, format, ex.localizedMessage)
))
)
)
} catch (ex: IOException) {
Pair(
Message(Formats.error, file.path),
Report(
id = file.name,
status = ReportStatus.failure,
message = ReportMessages.formatError(file.path, format, ex.localizedMessage)
)
)
}
}
}
......
......@@ -48,14 +48,29 @@ class Service(fileName: String = "app.yml") {
val validator = FileValidation()
val files = sftp.listFiles(settings.appSettings.getProperty("directory")).map { File(it) }
val reports = mutableListOf<Report>()
log.info("There are a total of ${files.size} files to validate ")
for (file in files) {
log.info("Validate file ${file.absolutePath}.")
log.info("Validate file $file.")
val format = validator.validateExtension(file)
val validationResult = validator.validate(sftp.open(file), format, file)
producer.sendMessage(validationResult.second.id, validationResult.first)
producer.sendReport(validationResult.second)
reports.add(validationResult.second)
try {
val validationResult = validator.validate(sftp.open(file), format, file)
log.info("Validated file at path ${validationResult.first}")
producer.sendMessage(validationResult.second.id, validationResult.first)
producer.sendReport(validationResult.second)
reports.add(validationResult.second)
} catch (ex: SftpClientException) {
log.error("SFTP Exception: ${ex.localizedMessage}")
val report = Report(
file.name,
status = ReportStatus.failure,
message = "SFTP Exception: ${ex.localizedMessage}."
)
producer.sendMessage(file.name, Message(Formats.error, "SFTP Error"))
producer.sendReport(report)
reports.add(report)
}
}
log.info("Collected a total of ${reports.size} reports.")
val failures = reports.count { report -> report.status == ReportStatus.failure }
if (failures > 0) {
log.warn("Validation ended with $failures failures!")
......@@ -78,16 +93,6 @@ class Service(fileName: String = "app.yml") {
settings.processReportTopic
)
}
} catch (ex: SftpClientException) {
log.error("SFTP Exception: ${ex.localizedMessage}")
producer.sendJobReport(
Report(
"text-file-validation",
status = ReportStatus.failure,
message = "SFTP Exception: ${ex.localizedMessage}."
),
settings.processReportTopic
)
} catch (ex: Exception) {
ex.printStackTrace()
log.error(ex.javaClass.canonicalName + ": " + ex.localizedMessage)
......
......@@ -24,7 +24,7 @@
</Console>
</Appenders>
<Loggers>
<Root level="warn">
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment