Validation Exception Message is too technical for user.
Dear,
I'm trying to make the exception message more friendly for user to read and understand. Current the exception message is too technical for user:
Here is the message when ContactId is wrong format: "Header matching ['ContactId'] names at index 0 was not found. If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue."
Can you please give me advice how can I know the ContactId is wrong format in validation exception? Or in the other words, where can I get the field that got wrong validation in the CsvHelper.ValidationException?
Thanks alot.
There should probably be a property on the ValidationException with the the field.
For now, I don't see a good way to get the data besides pulling it out of the exception message.
We have looked at every single information but couldn't find the property with the wrong validation fields. That's why need your advice.
Because the message is too technical for user to understand, we looked for the error validation fields but couldnt find. And we stuck here.
Looking forward to receiving your response. Thanks.
So exactly where is the place we can find the wrong validation fields? Thanks
You need to pull it out of the Exception.Message. I need to add some properties on that exception with the extra information, so you don't have to do that.
Please let us know once you got any updates. Thanks
How exactly do you pull it out of the Exception.Message? It seems to be static text for me in Version 7.1.1
Part of Class Map (just a test)
Map(c => c.Agent).Name("AGENT").Validate(field => {
return field.Length <= 1;
});
Then in my processor's configuration
csv.Configuration.ReadingExceptionOccurred = (exception) => {
if (exception.GetBaseException() is ValidationException) {
Log.Error("Data Validation > CSV ROW: '{row}' Error: '{exception}'", exception.ReadingContext.Row, exception.GetBaseException().Message);
} else {
throw exception;
}
};
For me, exception.Message and exception.GetBaseException().Message both just equal the text "Exception of type 'CsvHelper.ValidationException' was thrown.". Is it not possible to extract the field on which the error has occured?
Check the various values of the Reading/Writing context. You should be able to craft a helpful error message from that.
On Sun, Jun 17, 2018 at 5:22 AM, prom3theu5 [email protected] wrote:
How exactly do you pull it out of the Exception.Message? It seems to be static text for me in Version 7.1.1
Part of Class Map (just a test)
Map(c => c.Agent).Name("AGENT").Validate(field => { return field.Length <= 1; });
Then in my processor's configuration
csv.Configuration.ReadingExceptionOccurred = (exception) => { if (exception.GetBaseException() is ValidationException) { Log.Error("Data Validation > CSV ROW: '{row}' Error: '{exception}'", exception.ReadingContext.Row, exception.GetBaseException().Message); } else { throw exception; } };
For me, exception.Message and exception.GetBaseException().Message both just equal the text "Exception of type 'CsvHelper.ValidationException' was thrown.". Is it not possible to extract the field on which the error has occured?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JoshClose/CsvHelper/issues/874#issuecomment-397855563, or mute the thread https://github.com/notifications/unsubscribe-auth/AD_ohcJnwLCJxVXSdD37fNUTuDX0B1_8ks5t9eeOgaJpZM4Q-zVk .
I agree it seems very difficult to get a user friendly error message.
Map(c => c.Agent).Name("AGENT").Validate(field => {
if(field.Length > 1){
throw new Exception($"ExceptionIdentifier:Field : {"AGENT"} is not valid at ");
}
return true;
});
then
csv.Configuration.ReadingExceptionOccurred = (exception) => {
if (exception.GetType() == typeof(ReaderException))
{
string innterMessage = exception.InnerException?.Message;
if (!string.IsNullOrWhiteSpace(innterMessage) && innterMessage.Contains("ExceptionIdentifier:"))
{
innterMessage = innterMessage.Replace("ExceptionIdentifier:", "");
Log.Error($"{innterMessage} row {exception.ReadingContext.Row}");
}
} else {
throw exception;
}
};
GG WP 🗡
You can set your own function and throw whatever you like with whatever message you like.
csv.Configuration.HeaderValidated = (bool isValid, string[] headerNames, int headerNameIndex, ReadingContext context) =>
{
if (!isValid) throw new ValidationException("My custom message");
}
I will make the message more friendly and also put some properties on ValidationException.
Version 10.0.0 will have more friendly messages. Validation has been split up into HeaderValidationException and FieldValidationException. Each has some properties that pertain to the data it was given. Both inherit ValidationException.
I have Map(m => m.Product_No).Validate(f => !string.IsNullOrWhiteSpace(f)); in my ClassMap but the validation exception I'm getting has an empty string as the value for the field name when handled via ReadingExceptionOccurred. Is this expected? The exception message is just "Exception of type 'CsvHelper.FieldValidationException' was thrown." so I can't pull the name out of that either.
I have
Map(m => m.Product_No).Validate(f => !string.IsNullOrWhiteSpace(f));in my ClassMap but the validation exception I'm getting has an empty string as the value for the field name when handled via ReadingExceptionOccurred. Is this expected? The exception message is just "Exception of type 'CsvHelper.FieldValidationException' was thrown." so I can't pull the name out of that either.
I have the same issue. the Field is empty string in the exception.
I did a bit more digging and haven't verified it but I think the Field property is supposed to be the text value of the field itself. I wound up getting the name this way string columnName = ex.ReadingContext.HeaderRecord[ex.ReadingContext.CurrentIndex];