Commit 4dbadde1 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Replace local exception with library version

parent d642ca7e
/*
* Table Data Import Service
* Copyright (C) 2020 Memoriav
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.memobase
class InvalidInputException(val key: String, message: String) : Exception(message)
......@@ -18,6 +18,7 @@
package org.memobase
import ch.memobase.exceptions.InvalidInputException
import ch.memobase.reporting.Report
import ch.memobase.reporting.ReportStatus
import ch.memobase.settings.HeaderMetadata
......@@ -44,7 +45,7 @@ class TableParser(settings: SettingsLoader) {
when (inputMessage.format) {
Formats.xls, Formats.xlsx -> excelMapper(key, inputMessage, metadata)
Formats.csv, Formats.tsv -> csvMapper(key, inputMessage, metadata)
else -> throw InvalidInputException(key, "Cannot parse the table with format ${inputMessage.format}.")
else -> throw InvalidInputException("Cannot parse the table with format ${inputMessage.format}.")
}
} catch (ex: CSVFieldNumDifferentException) {
listOf(
......@@ -59,7 +60,7 @@ class TableParser(settings: SettingsLoader) {
listOf(
ResultMessage(key,
null,
Report(ex.key,
Report(key,
ReportStatus.fatal,
"Invalid Input: ${ex.localizedMessage}. Could not process any lines.",
Service.name)
......@@ -97,13 +98,11 @@ class TableParser(settings: SettingsLoader) {
val trimmedProperty = property.trim()
if (trimmedProperty.isEmpty()) {
throw InvalidInputException(
key,
"Missing a property name on row $count in column ${index + 1}!"
)
}
if (trimmedProperty.any { value -> invalidPropertyNameCharacters.contains(value) }) {
throw InvalidInputException(
key,
"Invalid property name $trimmedProperty on row $count in column ${index + 1}! You may not use the any of the following characters: + , . "
)
}
......@@ -117,13 +116,11 @@ class TableParser(settings: SettingsLoader) {
when (identifierValue) {
"" -> {
throw InvalidInputException(
"$count.${metadata.tableIdentifierIndex}",
"The row $count has an empty identifier in column ${metadata.tableIdentifierIndex}."
)
}
in identifierSet -> {
throw InvalidInputException(
"$count.${metadata.tableIdentifierIndex}",
"The row $count contains a duplicated identifier in column ${metadata.tableIdentifierIndex} with another row."
)
}
......@@ -136,10 +133,10 @@ class TableParser(settings: SettingsLoader) {
} catch (ex: InvalidInputException) {
resultMessages.add(
ResultMessage(
ex.key,
key,
null,
Report(
ex.key,
key,
ReportStatus.fatal,
ex.localizedMessage,
Service.name
......@@ -186,7 +183,6 @@ class TableParser(settings: SettingsLoader) {
)
}) {
throw InvalidInputException(
key,
"The property in cell ${cell.address} contains one or more invalid characters: $invalidPropertyNameCharacters."
)
} else {
......@@ -194,7 +190,6 @@ class TableParser(settings: SettingsLoader) {
}
} else {
throw InvalidInputException(
key,
"The header index is missing a value in cell ${cell.address}"
)
}
......@@ -208,13 +203,11 @@ class TableParser(settings: SettingsLoader) {
when (val cellValue = retrieveCellValue(cell)) {
"" -> {
throw InvalidInputException(
"CellAddress: $count:${metadata.tableIdentifierIndex}",
"The row ${row.rowNum} has an empty identifier in column ${metadata.tableIdentifierIndex}."
)
}
in identifierSet -> {
throw InvalidInputException(
"CellAddress: $count:${metadata.tableIdentifierIndex}",
"The row ${row.rowNum} contains a duplicated identifier in column ${metadata.tableIdentifierIndex} with another row."
)
}
......@@ -225,15 +218,14 @@ class TableParser(settings: SettingsLoader) {
}
} else {
throw InvalidInputException(
"${row.rowNum}.${metadata.tableIdentifierIndex}",
"No cell found in row ${row.rowNum} for column ${metadata.tableIdentifierIndex}."
)
}
}
} catch (ex: InvalidInputException) {
return@map ResultMessage(
ex.key, null, Report(
ex.key,
key, null, Report(
key,
ReportStatus.fatal,
ex.localizedMessage,
Service.name
......
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