You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2024/09/14 15:47:00 UTC

[jira] [Updated] (CSV-270) Throw a different Expeciton type on malformed CSV files

     [ https://issues.apache.org/jira/browse/CSV-270?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gary D. Gregory updated CSV-270:
--------------------------------
    Summary: Throw a different Expeciton type on malformed CSV files  (was: Different Expeciton type on malformed csv files)

> Throw a different Expeciton type on malformed CSV files
> -------------------------------------------------------
>
>                 Key: CSV-270
>                 URL: https://issues.apache.org/jira/browse/CSV-270
>             Project: Commons CSV
>          Issue Type: Improvement
>          Components: Parser
>    Affects Versions: 1.8
>            Reporter: Thomas Kamps
>            Priority: Minor
>         Attachments: malformed_format.csv
>
>
> 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:
> {code:java}
> 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
> }
> {code}
> 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:
> {code:java}
> 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();
> }
> {code}
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)