CsvHelper
CsvHelper copied to clipboard
Would like a way to reduce the verbosity of exception messages
The issue we have right now is that the messages included in some exceptions thrown by the library include information for developers, not information that a user needs. For example: If you add a mapping for a column named "entity", and that column does not exist in the input, you get a CsvHelper.HeaderInvalidException with the message:
Header with name 'entity'[0] was not found.\r\nIf 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.\r\n\r\nIReader state:\r\n ColumnCount: 0\r\n CurrentIndex: -1\r\n HeaderRecord:\r\n["enttity","account","group"]\r\nIParser state:\r\n ByteCount: 0\r\n CharCount: 23\r\n Row: 1\r\n RawRow: 1\r\n Count: 3\r\n RawRecord:\r\nenttity,account,group\r\n\r\n
The message should simply be, "Header with name entity
was not found."
Either the rest of this message should be in another field, or perhaps there could be a configuration for whether exceptions contained verbose information.
Set the config ExceptionMessagesContainRawData
to false
.
How do you set this to false? It seems like it is a readonly property on a CsvReader
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ExceptionMessagesContainRawData = false,
};
var csv = new CsvReader(reader, config)
@JoshClose Setting that configuration doesn't seem to fully help. The RawRecord key doesn't output values, but instead just a message that the data is hidden because of the config. But the string from exception.Message
is still mostly for a developer and not an end user.
Here's my output with the recommended config set to false. (I've redacted actual CSV data with XXXX)
Field containing names 'XXXX' or 'XXXX' does not exist. You can ignore missing fields by setting MissingFieldFound to null.
IReader state:
ColumnCount: 0
CurrentIndex: -1
HeaderRecord:
["XXXX", ...]
IParser state:
ByteCount: 0
CharCount: 695
Row: 2
RawRow: 2
Count: 18
RawRecord:
Hidden because ExceptionMessagesContainRawData is false.
The end user just needs to know Field containing names 'XXXX' or 'XXXX' does not exist.
That config is doing what it says, but I think the bigger issue is having a non-developer friendly message accessible.
Btw, is there documentation for the available config or just the source code? (I'm new with .Net and if just the source code is the standard, all good :) )
It would be nice if the Hidden because ExceptionMessagesContainRawData is false.
message contained a little bit of information about the RawData - the total length, for instance.
@philiprenich You can catch the exception and rethrow with the information in it you'd like.
@JoshClose, how exactly, since the exception doesn't store the original validation message anywhere? It would be nice if one didn't have to parse the exception message in order to simply get at the validation message that was passed to the ValidationException's constructor. I think a lot of people would be helped by
- Adding a property to ValidationException containing the original validation message
- Changing the Field property on FieldValidationException to return the actual field name
- Adding a FieldValue property on FieldValidationException to return the field's value, like the Field property currently does.
As it stands, I have to split the Message property on newlines in order to get at the original validation message, which is rather fragile, as it assumes the exception message format never changes.
@Halofreak1990 Add a new issue for this.