CsvHelper
CsvHelper copied to clipboard
Confusing exception when DateTimeOffsetConverter can't convert value
When a value cannot be converted to a DateTimeOffset, the exception message shows "Text: ''", instead of the actual column's value. Also, the member's name is not populated, making it difficult to debug.
Repro
[Test]
public void Repro()
{
var input = """
Timestamp
01-01-0001 00:00
""";
using var reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(input)));
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<Record>().ToArray();
}
record Record(DateTimeOffset Timestamp);
Throws:
CsvHelper.TypeConversion.TypeConverterException : The conversion cannot be performed.
Text: ''
MemberName:
MemberType:
TypeConverter: 'CsvHelper.TypeConversion.DateTimeOffsetConverter'
IReader state:
ColumnCount: 1
CurrentIndex: 0
HeaderRecord:
["Timestamp"]
IParser state:
ByteCount: 0
CharCount: 26
Row: 2
RawRow: 2
Count: 1
RawRecord:
01-01-0001 00:00
When there are a lot of columns, it is quite difficult to understand the problem. The column's value is not empty, but something that could not be parsed. And I would expect the MemberName / MemberType to also not be empty.
That works fine for me when I run it in LINQPad.