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 @@ ...@@ -18,6 +18,7 @@
package org.memobase package org.memobase
import ch.memobase.exceptions.InvalidInputException
import ch.memobase.reporting.Report import ch.memobase.reporting.Report
import ch.memobase.reporting.ReportStatus import ch.memobase.reporting.ReportStatus
import ch.memobase.settings.HeaderMetadata import ch.memobase.settings.HeaderMetadata
...@@ -44,7 +45,7 @@ class TableParser(settings: SettingsLoader) { ...@@ -44,7 +45,7 @@ class TableParser(settings: SettingsLoader) {
when (inputMessage.format) { when (inputMessage.format) {
Formats.xls, Formats.xlsx -> excelMapper(key, inputMessage, metadata) Formats.xls, Formats.xlsx -> excelMapper(key, inputMessage, metadata)
Formats.csv, Formats.tsv -> csvMapper(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) { } catch (ex: CSVFieldNumDifferentException) {
listOf( listOf(
...@@ -59,7 +60,7 @@ class TableParser(settings: SettingsLoader) { ...@@ -59,7 +60,7 @@ class TableParser(settings: SettingsLoader) {
listOf( listOf(
ResultMessage(key, ResultMessage(key,
null, null,
Report(ex.key, Report(key,
ReportStatus.fatal, ReportStatus.fatal,
"Invalid Input: ${ex.localizedMessage}. Could not process any lines.", "Invalid Input: ${ex.localizedMessage}. Could not process any lines.",
Service.name) Service.name)
...@@ -97,13 +98,11 @@ class TableParser(settings: SettingsLoader) { ...@@ -97,13 +98,11 @@ class TableParser(settings: SettingsLoader) {
val trimmedProperty = property.trim() val trimmedProperty = property.trim()
if (trimmedProperty.isEmpty()) { if (trimmedProperty.isEmpty()) {
throw InvalidInputException( throw InvalidInputException(
key,
"Missing a property name on row $count in column ${index + 1}!" "Missing a property name on row $count in column ${index + 1}!"
) )
} }
if (trimmedProperty.any { value -> invalidPropertyNameCharacters.contains(value) }) { if (trimmedProperty.any { value -> invalidPropertyNameCharacters.contains(value) }) {
throw InvalidInputException( throw InvalidInputException(
key,
"Invalid property name $trimmedProperty on row $count in column ${index + 1}! You may not use the any of the following characters: + , . " "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) { ...@@ -117,13 +116,11 @@ class TableParser(settings: SettingsLoader) {
when (identifierValue) { when (identifierValue) {
"" -> { "" -> {
throw InvalidInputException( throw InvalidInputException(
"$count.${metadata.tableIdentifierIndex}",
"The row $count has an empty identifier in column ${metadata.tableIdentifierIndex}." "The row $count has an empty identifier in column ${metadata.tableIdentifierIndex}."
) )
} }
in identifierSet -> { in identifierSet -> {
throw InvalidInputException( throw InvalidInputException(
"$count.${metadata.tableIdentifierIndex}",
"The row $count contains a duplicated identifier in column ${metadata.tableIdentifierIndex} with another row." "The row $count contains a duplicated identifier in column ${metadata.tableIdentifierIndex} with another row."
) )
} }
...@@ -136,10 +133,10 @@ class TableParser(settings: SettingsLoader) { ...@@ -136,10 +133,10 @@ class TableParser(settings: SettingsLoader) {
} catch (ex: InvalidInputException) { } catch (ex: InvalidInputException) {
resultMessages.add( resultMessages.add(
ResultMessage( ResultMessage(
ex.key, key,
null, null,
Report( Report(
ex.key, key,
ReportStatus.fatal, ReportStatus.fatal,
ex.localizedMessage, ex.localizedMessage,
Service.name Service.name
...@@ -186,7 +183,6 @@ class TableParser(settings: SettingsLoader) { ...@@ -186,7 +183,6 @@ class TableParser(settings: SettingsLoader) {
) )
}) { }) {
throw InvalidInputException( throw InvalidInputException(
key,
"The property in cell ${cell.address} contains one or more invalid characters: $invalidPropertyNameCharacters." "The property in cell ${cell.address} contains one or more invalid characters: $invalidPropertyNameCharacters."
) )
} else { } else {
...@@ -194,7 +190,6 @@ class TableParser(settings: SettingsLoader) { ...@@ -194,7 +190,6 @@ class TableParser(settings: SettingsLoader) {
} }
} else { } else {
throw InvalidInputException( throw InvalidInputException(
key,
"The header index is missing a value in cell ${cell.address}" "The header index is missing a value in cell ${cell.address}"
) )
} }
...@@ -208,13 +203,11 @@ class TableParser(settings: SettingsLoader) { ...@@ -208,13 +203,11 @@ class TableParser(settings: SettingsLoader) {
when (val cellValue = retrieveCellValue(cell)) { when (val cellValue = retrieveCellValue(cell)) {
"" -> { "" -> {
throw InvalidInputException( throw InvalidInputException(
"CellAddress: $count:${metadata.tableIdentifierIndex}",
"The row ${row.rowNum} has an empty identifier in column ${metadata.tableIdentifierIndex}." "The row ${row.rowNum} has an empty identifier in column ${metadata.tableIdentifierIndex}."
) )
} }
in identifierSet -> { in identifierSet -> {
throw InvalidInputException( throw InvalidInputException(
"CellAddress: $count:${metadata.tableIdentifierIndex}",
"The row ${row.rowNum} contains a duplicated identifier in column ${metadata.tableIdentifierIndex} with another row." "The row ${row.rowNum} contains a duplicated identifier in column ${metadata.tableIdentifierIndex} with another row."
) )
} }
...@@ -225,15 +218,14 @@ class TableParser(settings: SettingsLoader) { ...@@ -225,15 +218,14 @@ class TableParser(settings: SettingsLoader) {
} }
} else { } else {
throw InvalidInputException( throw InvalidInputException(
"${row.rowNum}.${metadata.tableIdentifierIndex}",
"No cell found in row ${row.rowNum} for column ${metadata.tableIdentifierIndex}." "No cell found in row ${row.rowNum} for column ${metadata.tableIdentifierIndex}."
) )
} }
} }
} catch (ex: InvalidInputException) { } catch (ex: InvalidInputException) {
return@map ResultMessage( return@map ResultMessage(
ex.key, null, Report( key, null, Report(
ex.key, key,
ReportStatus.fatal, ReportStatus.fatal,
ex.localizedMessage, ex.localizedMessage,
Service.name 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