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 2021/01/27 17:06:00 UTC
[jira] [Commented] (CSV-270) Different Expeciton type on malformed
csv files
[ https://issues.apache.org/jira/browse/CSV-270?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17273002#comment-17273002 ]
Gary D. Gregory commented on CSV-270:
-------------------------------------
[~t.kamps@laudert.de]
Thank you for your report.
Please feel free to provide a PR on GitHub with a unit test.
> 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.3.4#803005)