Search code examples
c#csvcsvhelper

CSVHelper: logging errors when BadDataException is thrown


I'm trying to configure the BadDataException message to include more information like the where the exception has occurred for debugging purposes. I have written the code below but it seems that the exception thrown is printing out the generic error message and not the one I configured. I think I missing a logical error here but I'm not sure. Any help is appreciated!

public void HeaderColumnParser(string PathToFile) {

            try {
                using (TextReader fileReader = File.OpenText(PathToFile)) {
                var csv = new CsvReader(fileReader, CultureInfo.InvariantCulture);


                CsvConfiguration csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture) {
                    BadDataFound = context => {
                        throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));

                    }
                };

                // csv.Configuration.AllowComments = true;
                csv.Read();
                csv.ReadHeader();


                    //while condition returns true till last row
                    while (csv.Read()) {

                        string RefDes = "";

                        //Index 0 gets the ReferenceDesignator if header column exists
                        if (ColumnIndex[0] > 0) {

                            if (csv.TryGetField(ColumnIndex[0], out string value)) {
                                RefDes = value;
                            }
                        }

                        //gets MPN
                        if (ColumnIndex[1] > 0) {

                            if (csv.TryGetField(ColumnIndex[1], out string value)) {
                                MPN.Add(new ManufacturerPartNumber(RefDes, value));
                            }

                        }

                        //Gets Value
                        if (ColumnIndex[2] > 0) {

                            if (csv.TryGetField(ColumnIndex[2], out string value)) {
                                Values.Add(new ComponentValue(RefDes, value));
                            }

                        }

                        //Gets Short description
                        if (ColumnIndex[3] > 0) {

                            if (csv.TryGetField(ColumnIndex[3], out string value)) {
                                DescriptionShort.Add(new ShortDescription(RefDes, value));
                            }

                        }

                        //Gets Long description
                        if (ColumnIndex[4] > 0) {

                            if (csv.TryGetField(ColumnIndex[4], out string value)) {
                                DescriptionLong.Add(new LongDescription(RefDes, value));
                            }

                        }

                        //Gets Manufacturer
                        if (ColumnIndex[5] > 0) {

                            if (csv.TryGetField(ColumnIndex[5], out string value)) {
                                Manufacturer.Add(new Manufacturer(RefDes, value));
                            }

                        }

                        //Gets DNI components
                        if (ColumnIndex[6] > 0) {

                            if (csv.TryGetField(ColumnIndex[6], out string value)) {
                                DNI.Add(new DNI(RefDes, value));
                            }

                        }

                        //Gets the datasheet
                        if (ColumnIndex[7] > 0) {

                            if (csv.TryGetField(ColumnIndex[7], out string value)) {
                                DataSheet.Add(new DataSheet(RefDes, value));
                            }
                        }
                    }

                }

            }
            catch (BadDataException ex) {
                throw;

            }
        }

Solution

  • You created the CsvConfiguration, but you never use it. You can use it in the creation of the CsvReader.

    CsvConfiguration csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        BadDataFound = context => {
            throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
    
        }
    };
    
    var csv = new CsvReader(fileReader, csvConfig);
    

    Or you can configure BadDataFound after you create the CsvReader.

    Edit: Since Version 20.0.0, you can no longer edit CsvReader.Configuration. As Snympi mentioned, it is now read only.

    var csv = new CsvReader(fileReader, CultureInfo.InvariantCulture);
    
    csv.Configuration.BadDataFound = context =>
    {
        throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
    
    };