Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSV-310] Misleading error message when QuoteMode set to None #352

Merged
merged 1 commit into from
Sep 13, 2023

Conversation

gbidsilva
Copy link
Contributor

See related Jira issue: https://issues.apache.org/jira/browse/CSV-310

When we try to print CSV content using the CSVFormat and CSVPrinter, we can ger it done with following sample code.

public void testHappyPath() throws IOException {
    CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
            .setHeader("Col1", "Col2", "Col3", "Col4")
            .setQuoteMode(QuoteMode.NONE)
            .setEscape('#')
            .build();

    CSVPrinter printer = new CSVPrinter(System.out, csvFormat);

    List<String[]> temp = new ArrayList<>();

    temp.add(new String[] { "rec1", "rec2\"", "", "rec4" });
    temp.add(new String[] { "", "rec6", "rec7", "rec8" });

    for (String[] temp1 : temp) {
        printer.printRecord(temp1);
    }
    printer.close();
}

In above sample I have used setQuoteMode(QuoteMode.NONE). However, if we do not set the escape character using setEscape('#') then it would result in following exception.
java.lang.IllegalArgumentException: No quotes mode set but no escape character is set.

At first, I was wondering why I was getting this error. Then I looked into the source and figured out that CSVFormat.printWithEscapes() method internally using getEscapeCharacter().charValue() method to print records.

However, the error message which we are getting is sort of misleading. It implies that the code has not set any quote mode even though we set it.
Simple and more meaningful exception message could be: Quote mode set to NONE but no escape character is set.

@gbidsilva gbidsilva changed the title [CSV-310] Changing the exception message to be more meaningful [CSV-310] Misleading error message when QuoteMode set to None Sep 6, 2023
@gbidsilva
Copy link
Contributor Author

@garydgregory : Appreciate your feedback on this.

@garydgregory garydgregory merged commit c2cf305 into apache:master Sep 13, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants