Skip to content

Commit

Permalink
Make max chars in cell error message more helpful
Browse files Browse the repository at this point in the history
  • Loading branch information
techncl committed Jan 31, 2025
1 parent c8084f6 commit 7f77b61
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,23 @@ class RowIterator(parser: CsvParser, progress: Option[ProgressFor], maxCharsPerC

private var index = 1
private var current = toRow(Try(parser.parseNext()))
private var potentialHeaderRow: Option[Row] = None

@throws(classOf[IOException])
override def next(): Row = {
val row = current match {
case Success(row) => row
case Success(row) =>
if(index == 1 && potentialHeaderRow.isEmpty) potentialHeaderRow = Some(row) // this is here in case the old API is used that doesn't call 'skipHeader'
row
case Failure(ex: TextParsingException) if(ex.toString.contains("exceeds the maximum number of characters")) =>
val cellLocationMsg =
potentialHeaderRow match {
case Some(headerRow) => s"in the cell located at line: ${ex.getLineIndex}, column: ${headerRow.cells(ex.getColumnIndex).value},"
case None => s"in column ${ex.getColumnIndex + 1} of the header row"
}

val customMessage =
s"The number of characters in the cell located at line: ${ex.getLineIndex + 1}, column: ${ex.getColumnIndex + 1}, " +
s"is larger than the maximum number of characters allowed in a cell ($maxCharsPerCell); increase this limit and re-run."
s"The number of characters $cellLocationMsg is larger than the maximum number of characters allowed in a cell ($maxCharsPerCell); increase this limit and re-run."
throw new Exception(customMessage)
case Failure(ex) => throw ex
}
Expand All @@ -385,7 +393,9 @@ class RowIterator(parser: CsvParser, progress: Option[ProgressFor], maxCharsPerC
@throws(classOf[IOException])
def skipHeader(): Row = {
this.index = index - 1
next()
val row = next()
this.potentialHeaderRow = Some(row)
row
}

override def hasNext: Boolean = current match {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
col1,col2
row1Col1,row1Col2LongCellLength
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ class CsvValidatorMaxCharsPerCellSpec extends Specification with TestResources {
def app(maxChars: Int=4096) = new CsvValidator with AllErrorsMetaDataValidator { val pathSubstitutions: List[(String, String)] = List[(String,String)](); val enforceCaseSensitivePathChecks = false; val trace = false; val skipFileChecks = false; val maxCharsPerCell: Int = maxChars }
def parse(filePath: String, maxChars: Int=4096): Schema = app(maxChars).parseSchema(TextFile(Paths.get(filePath))) fold (f => throw new IllegalArgumentException(f.toString()), s => s)

"fail if the number of characters in a cell is more than the maxCharsPerCell number" in {
"fail if the number of characters in a cell in the header is more than the maxCharsPerCell number and indicate the column number" in {
val maxCharsAllowed = 2
val validatedNel = app(maxCharsAllowed).validate(TextFile(Paths.get(baseResourcePkgPath).resolve("metaData.csv")), parse(baseResourcePkgPath + "/schema.csvs", maxCharsAllowed), None).swap
validatedNel.toList.head.toList must beEqualTo(
List(FailMessage(ValidationError,"java.lang.Exception: The number of characters in the cell located at line: 1, column: 1, is larger than the maximum number of characters allowed in a cell (2); increase this limit and re-run.",None,None))
List(FailMessage(ValidationError,"java.lang.Exception: The number of characters in column 1 of the header row is larger than the maximum number of characters allowed in a cell (2); increase this limit and re-run.",None,None))
)
}

"fail if the number of characters in a cell in a non-header row is more than the maxCharsPerCell number and indicate the column name" in {
val maxCharsAllowed = 15
val validatedNel = app(maxCharsAllowed).validate(TextFile(Paths.get(baseResourcePkgPath).resolve("metaDataWithALongCellLength.csv")), parse(baseResourcePkgPath + "/schema.csvs"), None).swap
validatedNel.toList.head.toList must beEqualTo(
List(FailMessage(ValidationError,"java.lang.Exception: The number of characters in the cell located at line: 1, column: col2, is larger than the maximum number of characters allowed in a cell (15); increase this limit and re-run.",None,None))
)
}

Expand Down

0 comments on commit 7f77b61

Please sign in to comment.