Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-310

Misleading error message when QuoteMode set to None

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.10.0
    • 1.11.0
    • Parser
    • Patch

    Description

      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 internaly 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".

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              gbidsosc Buddhi De Silva
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: