JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.79k stars 1.07k forks source link

BadDataException does not hold the info column mismatches #2286

Open Manthazar opened 2 months ago

Manthazar commented 2 months ago

Is your feature request related to a problem? Please describe. Given there is a CSV file with quoted headers similar to:

"Business Groups", "Order Number"
"Group1",1

and colum header matching is set to: config.PrepareHeaderForMatch = args => args.Header.ToLower(),

When we attempt to load the file we get a Bad Data Exception with no useful infomation in it:

IReader state:
   ColumnCount: 2
   CurrentIndex: -1
   HeaderRecord:

IParser state:
   ByteCount: 0
   CharCount: 35
   Row: 1
   RawRow: 1
   Count: 2
   RawRecord:
"Business Groups", "Order Number"

'

--> note the error is the ` space character before the quoted header starts forOrder Number,CharCount 20`

Describe the solution you'd like

Describe alternatives you've considered

vivebhagat commented 2 months ago

@Manthazar Looks like library is already handling this as configuration while reading the header. CsvParser.ReadLine method is checking trimOptions, which is Enum with 3 values - None, Trim and InsideQuotes. The case you mentioned can be handled by assigning trimOptions to the configuration. Maybe that is what you are looking for.

Manthazar commented 2 months ago

Thanks @vivebhagat , but this feature request is about a better error message, not the capability itself to ignore such kind of behaviour. If your upstream (application) starts to add a space for some reason, CSV helper would not be helpful to flag the reason for the parser error. Once one understood the root cause, of course then one could amend the configuration as you described.