Csvhelper: Parsing error messages

Created on 3 Jul 2014  ·  3Comments  ·  Source: JoshClose/CsvHelper

I saw a pull request that should address issue of clarifying error messages, but it still would not provide enough flexibility.

Currently I have to write something like this in order to provide to the user a better level of details about error:

``` c#
var e = new List();
csvReader.Configuration.ReadingExceptionCallback = (ex, row) =>
{
foreach (var error in ex.Data.Values)
{
var info = error.ToString().Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var value = info[4].Split(':')[1].Trim();
var field = info[3].Split(':')[1].Trim();
e.Add(string.Format("Could not read value {0} in row {1}, field {2}.",
value, row.Row, field));
}
};
// return e to the webpage (for example as JSON)

This is relatively ok. It gives information where exactly parsing error has occured. However, as you can see, digging this information is ugly since I have to parse string to get the value and the field name. It would be nice if this information could be exposed via properties so that I could write something like this:

``` c#
csvReader.Configuration.ReadingExceptionCallback = (ex, row) =>
{
    foreach (var error in row.Errors)
    {
        e.Add(string.Format("Could not read value {0} in row {1}, field {2}.",
            error.Value, error.Row, error.FieldName));
    }
};

But above is ok... it works. What's more important is the following request.
It would also be nice to be able to provide information WHY parsing failed.
Let's say a field only accepts values "Red" or "Blue", but user types in "Yellow". We could solve this by:

c# // in the MyClassMap : CsvClassMap<MyClass> class Map(x => x.Color).ErrorMessage("Accepted values are 'Red' and 'Blue'"); ... // in the ReadingExceptionCallback e.Add(string.Format("Could not read value {0} in row {1}, field {2}. {3}", error.Value, error.Row, error.FieldName, error.Message));

This syntax would provide a lot of flexibility for developer and give clear feedback to the user.

Let me know your thoughts.

feature

Most helpful comment

This is an interesting idea. I'll mark this as a feature for now and take a deeper look later.

All 3 comments

This is an interesting idea. I'll mark this as a feature for now and take a deeper look later.

would just like to second this feature request.

since I'm here I'd just like to briefly say thanks. CsvHelper is great on so many levels.

In 3.0 the base CsvHelperException object has properties for all the value now. The callback will give back a CsvHelperException instead of Exception. 3.0 beta is on NuGet.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RizwanAhmedJutt picture RizwanAhmedJutt  ·  5Comments

shinriyo picture shinriyo  ·  6Comments

malinru picture malinru  ·  5Comments

SuperSkippy picture SuperSkippy  ·  5Comments

JoshClose picture JoshClose  ·  4Comments