Search code examples
c#csvhelper

How handle Multiple Type convertor exception in CSVhelper


Recently I upgraded CSVHelper libraries from 2.7.1 to 30 version. I have resolved all build errors. But after upgradation exceptions are not logging. Old Code:

csvReader.Configuration.ReadingExceptionCallback =
                        (ex, row) =>
                        {
                            if (ex is CsvHelper.TypeConversion.CsvTypeConverterException)
                            {
                                foreach (DictionaryEntry error in ex.Data)
                                {
                                    AddRowError(row.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(row) + "'");
                                }
                            }
                            else if (ex is FormatException)
                            {
                                AddRowError(row.Row, ex.Message + " Column Name: '" + GetColumnName(row) + "' Column Value: '" + GetColumnValue(row) + "'");
                            }
                            else
                            {
                                AddRowError(row.Row, string.Format("Line[{0}]: {1}", row.Row, ex.StackTrace));
                            }

                        };

New Code:

ReadingExceptionOccurred = args =>
                        {
                            var context = args.Exception.Context;
                            if (args.Exception is CsvHelper.TypeConversion.TypeConverterException)
                            {
                                foreach (DictionaryEntry error in args.Exception.Data)
                                {
                                    AddRowError(context.Parser.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(context.Reader) + "'");
                                }
                            }
                            else if (args.Exception.GetType() == typeof(FormatException))
                            {
                                AddRowError(context.Parser.Row, args.Exception.Message + " Column Name: '" + GetColumnName(context.Reader) + "' Column Value: '" + GetColumnValue(context.Reader) + "'");
                            }
                            else
                            {
                                AddRowError(context.Parser.Row, string.Format("Line[{0}]: {1}", context.Parser.Row, args.Exception.StackTrace));
                            }
                            return false;
                        },

1)how to handle ex.Data? I had written as args.Exception.Data. But in data I do not see any key value pair as previous.

  1. ex is FormatException How to handle FormatException

enter image description here


Solution

  • You can use args.Exception.Message. It gives the same information (plus some) as looping through ex.Data.

    looping through ex.Data in Version 2.7.1

    4: Row: '4' (1 based)
    Type: 'UserQuery+Foo'
    Field Index: '0' (0 based)
    Field Name: 'Id'
    Field Value: 'three'
    

    args.Exception.Message in Version 30.0.1

    The conversion cannot be performed.
        Text: 'three'
        MemberName: Id
        MemberType: System.Int32
        TypeConverter: 'CsvHelper.TypeConversion.Int32Converter'
    IReader state:
       ColumnCount: 0
       CurrentIndex: 0
       HeaderRecord:
    ["Id","Name"]
    IParser state:
       ByteCount: 0
       CharCount: 30
       Row: 4
       RawRow: 4
       Count: 2
       RawRecord:
    three,name3