Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit ae8c0cd

Browse files
committed
removing last parsed content from exception message, instead making respective method public
1 parent 9c50c7b commit ae8c0cd

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,12 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
462462
* </pre>
463463
* @return parsed CSV content of current reading line
464464
*/
465-
private String getLastParsedContent() {
465+
public String getLastParsedContent() {
466466
String parsedContent = "";
467467
int recordListSize = this.recordList.size();
468468
if (recordListSize > 0) {
469469
if (recordListSize <= this.maxParsedTokenCount) {
470-
parsedContent = String.join("", this.recordList.toArray(Constants.EMPTY_STRING_ARRAY));
470+
parsedContent = String.join(this.format.getDelimiterString(), this.recordList.toArray(Constants.EMPTY_STRING_ARRAY));
471471
} else {
472472
// number of parsed token exceed required token count. Take the expected tokens from the end.
473473
int startIndex = recordListSize - maxParsedTokenCount;
@@ -810,8 +810,7 @@ CSVRecord nextRecord() throws IOException {
810810
this.lexer.nextToken(this.reusableToken);
811811
} catch (IOException ioe) {
812812
String errorMessage = "An exception occurred while tying to parse the CSV content. Issue in line: "
813-
+ this.lexer.getCurrentLineNumber() + ", position: " + this.lexer.getCharacterPosition()
814-
+ ", last parsed content: " + this.getLastParsedContent();
813+
+ this.lexer.getCurrentLineNumber() + ", position: " + this.lexer.getCharacterPosition();
815814
throw new IOException(errorMessage, ioe);
816815
}
817816
switch (this.reusableToken.type) {

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.junit.jupiter.api.Assertions.assertNull;
2828
import static org.junit.jupiter.api.Assertions.assertThrows;
2929
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.junit.jupiter.api.Assertions.fail;
3031

3132
import java.io.File;
3233
import java.io.IOException;
@@ -1643,7 +1644,7 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
16431644
}
16441645

16451646
@Test
1646-
public void testFaultyCSVShouldThrowErrorWithDetailedMessage() {
1647+
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
16471648
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" +
16481649
"rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
16491650

@@ -1653,20 +1654,59 @@ public void testFaultyCSVShouldThrowErrorWithDetailedMessage() {
16531654
.setSkipHeaderRecord(true)
16541655
.build();
16551656

1657+
CSVParser csvParser = null;
1658+
try {
1659+
csvParser = csvFormat.parse(stringReader);
1660+
} catch (IOException e) {
1661+
fail("Failed to parse the CSV content");
1662+
}
1663+
final Iterable<CSVRecord> finalRecords = csvParser;
16561664
Exception exception = assertThrows(UncheckedIOException.class, () -> {
1657-
Iterable<CSVRecord> records = csvFormat.parse(stringReader);
1658-
for (CSVRecord record : records) {
1665+
for (CSVRecord record : finalRecords) {
16591666
System.out.println(record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3)
16601667
+ " " + record.get(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7)
16611668
+ " " + record.get(8) + " " + record.get(9));
16621669
}
16631670
});
16641671
String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " +
1665-
"while tying to parse the CSV content. Issue in line: 2, position: 94, last parsed content: " +
1666-
"...rec4,rec5,rec6,rec7,rec8";
1672+
"while tying to parse the CSV content. Issue in line: 2, position: 94";
16671673
String actualMessage = exception.getMessage();
1668-
16691674
assertTrue(actualMessage.contains(expectedErrorMessage));
1675+
assertNotNull(csvParser);
1676+
String expectedLastParsedContent = "...rec4,rec5,rec6,rec7,rec8";
1677+
assertEquals(expectedLastParsedContent, csvParser.getLastParsedContent());
16701678
}
16711679

1680+
@Test
1681+
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() {
1682+
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8\n" +
1683+
"rec1,rec2,rec3,rec4,\"\"rec5\"\",rec6,rec7,rec8";
1684+
1685+
StringReader stringReader = new StringReader(csvContent);
1686+
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
1687+
.setHeader()
1688+
.setSkipHeaderRecord(true)
1689+
.build();
1690+
1691+
CSVParser csvParser = null;
1692+
try {
1693+
csvParser = csvFormat.parse(stringReader);
1694+
} catch (IOException e) {
1695+
fail("Failed to parse the CSV content");
1696+
}
1697+
final Iterable<CSVRecord> finalRecords = csvParser;
1698+
Exception exception = assertThrows(UncheckedIOException.class, () -> {
1699+
for (CSVRecord record : finalRecords) {
1700+
System.out.println(record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3)
1701+
+ " " + record.get(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7));
1702+
}
1703+
});
1704+
String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " +
1705+
"while tying to parse the CSV content. Issue in line: 2, position: 63";
1706+
String actualMessage = exception.getMessage();
1707+
assertTrue(actualMessage.contains(expectedErrorMessage));
1708+
assertNotNull(csvParser);
1709+
String expectedLastParsedContent = "rec1,rec2,rec3,rec4";
1710+
assertEquals(expectedLastParsedContent, csvParser.getLastParsedContent());
1711+
}
16721712
}

0 commit comments

Comments
 (0)