Are there any established processes for error handling when parsing CSVs with binding? #192
Replies: 6 comments 12 replies
-
That could be useful (assuming it doesn't kill performance) In my case, the delegate would probably just log the error, happy to skip the record. |
Beta Was this translation helpful? Give feedback.
-
There is no support for recovering from malformed CSV files, in much the same way that System.Xml doesn't provide a mechanism to repair or recover from "malformed XML". A file is either valid XML or it isn't, and I think the same philosophy should apply to CSV files. There are other libraries that try to provide support for those scenarios, but I'm not interested in dealing with it in my library. A valid CSV file might have other issues that prevent it from processing correctly. Most frequently this happens when a column is expected to be a specific type, like a number or a date, but a value in the file doesn't conform to that schema. I have given some thought to that scenario, but the code is somewhat half-baked at the moment. I guess I'd need to know what specific scenarios you want to handle. |
Beta Was this translation helpful? Give feedback.
-
<<but a value in the file doesn't conform to that schema. I have given some thought to that scenario,>> At the moment if the schema declares a column to be double, int, bool and the field is missing in a row, an exception will occur. For what regards the case when an exception is thrown because a record is rejected for some other reason (field is of bad type/format, not enough fields, etc etc) I'd be happy with a simple option to switch behaviour from throwing to skipping; even better if it called a user-defined delegate, with a few arguments e.g. (int recordNumber, int columnFailing, enum reasonWhyRejected) which allows the user to log the problem for separate investigation. |
Beta Was this translation helpful? Give feedback.
-
I have now played with the above a bit, here's my 0.02$.
|
Beta Was this translation helpful? Give feedback.
-
What do you mean by "missing"? There is a cell without a value, or there is no cell?
Which one(s) are missing the age? Which ones are missing the name? And for strings being missing, is it a case of not being able to determine if empty string is a valid value or not? Are you drawing a distinction between |
Beta Was this translation helpful? Give feedback.
-
Tx - I'll give feedback asap |
Beta Was this translation helpful? Give feedback.
-
From a quick browse of the code, it looks like parsing a CSV just crashes and stops if problem data is encountered. Has any thought been put to perhaps having events that are raised if a malformed line is encountered? Or perhaps giving a handling delegate the opportunity to fix the line too, and it can be reparsed (or if the delegate returns null, skip this line, if it throws an exception, halt processing)..
Beta Was this translation helpful? Give feedback.
All reactions