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

Different Expeciton type on malformed csv files

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 1.8
    • None
    • Parser
    • None

    Description

      In our application we support to read CSV files with a custom definable format. The problem is now, that a suer could give a CSV file, which doies not match the defined pattern, the CSVParser throws an IOException. This exception type could also be throws, if the reading of the itself fails.

      Wed like to have a simple distiction beween IO errors and file content errors.

      We could parse the IOException's message, but those messages could change and we have to know about all kinds of content errors in advance.

       

      So my suggestion is to throw a specialied exception, when malformed content is detected during parsing. So we could distinguish between thsoe two kind of errors very easily:

      try (
          final CSVParser csvParser = new CSVParser(
              new FileReader(soneFile, encoding),
              csvFormat
          )
      ) {
          csvParser.getRecords();
      }
      catch (IOException e) {
          //File cannot be read for some reason
      }
      catch (MalformedCSVException e) {
          //CSV content is malformed compared to given CSVFormat
      }
      

      Currently we wold have to get the message from the IOExcpetion and check its pattern to get the problem.

       

      Here is a simple example how to get an IOException that occurs, when the files content does not match the given CSVFormat:

      try (
          final CSVParser csvParser = new CSVParser(
              new FileReader("path/to/malformed_format.csv", StandardCharsets.UTF_8),
              CSVFormat.DEFAULT
          )
      ) {
          csvParser.getRecords();
      }
      catch (IOException e) {
          e.printStackTrace();
      }
      
      

       

      Attachments

        1. malformed_format.csv
          0.0 kB
          Thomas Kamps

        Activity

          People

            Unassigned Unassigned
            t.kamps@laudert.de Thomas Kamps
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: