Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error messages #282

Closed
DmitryEfimenko opened this issue Jul 2, 2014 · 3 comments
Closed

Parsing error messages #282

DmitryEfimenko opened this issue Jul 2, 2014 · 3 comments
Labels

Comments

@DmitryEfimenko
Copy link

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:

var e = new List<string>();
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:

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:

// 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.

@JoshClose
Copy link
Owner

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

@Maestro85282
Copy link

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.

@JoshClose
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants