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

Improve sftp exception handling

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