CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Validation Exception Message is too technical for user.

Open ndaonguyen opened this issue 8 years ago • 14 comments

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.

ndaonguyen avatar Dec 12 '17 11:12 ndaonguyen

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.

JoshClose avatar Dec 13 '17 19:12 JoshClose

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.

ndaonguyen avatar Dec 18 '17 02:12 ndaonguyen

So exactly where is the place we can find the wrong validation fields? Thanks

ndaonguyen avatar Dec 20 '17 04:12 ndaonguyen

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.

JoshClose avatar Dec 20 '17 15:12 JoshClose

Please let us know once you got any updates. Thanks

ndaonguyen avatar Feb 02 '18 06:02 ndaonguyen

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?

prom3theu5 avatar Jun 17 '18 05:06 prom3theu5

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 .

jamesbascle avatar Jun 18 '18 00:06 jamesbascle

I agree it seems very difficult to get a user friendly error message.

mtissington avatar Aug 05 '18 19:08 mtissington

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 🗡

AltairCA avatar Aug 16 '18 13:08 AltairCA

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.

JoshClose avatar Nov 21 '18 05:11 JoshClose

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.

JoshClose avatar Nov 21 '18 07:11 JoshClose

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.

jons-aura avatar Sep 11 '19 21:09 jons-aura

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.

zhangsquall avatar Nov 01 '19 21:11 zhangsquall

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];

torndar avatar Nov 04 '19 13:11 torndar