Skip to content

Commit a33b24c

Browse files
authored
Merge pull request #347 from gbidsilva/CSV-147
[CSV-147] Error message optimization during faulty CSV record read
2 parents f429824 + c0759cd commit a33b24c

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/main/java/org/apache/commons/csv/Lexer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
367367
}
368368
if (!Character.isWhitespace((char)c)) {
369369
// error invalid char between token and next delimiter
370-
throw new IOException("(line " + getCurrentLineNumber() +
371-
") invalid char between encapsulated token and delimiter");
370+
throw new IOException("Invalid char between encapsulated token and delimiter at line: " +
371+
getCurrentLineNumber() + ", position: " + getCharacterPosition());
372372
}
373373
}
374374
}

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,4 +1642,26 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
16421642
parser.close();
16431643
}
16441644

1645+
@Test
1646+
public void testFaultyCSVShouldThrowExceptionWithLineAndPosition() throws IOException {
1647+
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" +
1648+
"rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
1649+
1650+
StringReader stringReader = new StringReader(csvContent);
1651+
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
1652+
.setHeader()
1653+
.setSkipHeaderRecord(true)
1654+
.build();
1655+
1656+
CSVParser csvParser = csvFormat.parse(stringReader);
1657+
Exception exception = assertThrows(UncheckedIOException.class, () -> {
1658+
for (CSVRecord record : csvParser) {
1659+
// this will result in exception due to parsing issue
1660+
}
1661+
});
1662+
String expectedErrorMessage = "Invalid char between encapsulated token and delimiter at " +
1663+
"line: 2, position: 94";
1664+
String actualMessage = exception.getMessage();
1665+
assertTrue(actualMessage.contains(expectedErrorMessage));
1666+
}
16451667
}

0 commit comments

Comments
 (0)