I would like to set ErrorMode = ErrorMode.SaveAndContinue
so I can process errors after a CSV file has been written.
How can I generate row level errors artificially when calling WriteStream()
so that I can test my error handling?
In particular I would like to find out whether the AfterWriteRecord
event is fired if a row errors.
Looking at the source code there are a few ways:
You call WriteStream()
with an IEnumerable<T>
where T
is your FileHelpers record class. So in your test, create a List<T>
, but add null to it as one of the records. This will cause WriteStream()
to raise a BadUsageException
.
Alternatively, you could add an OnProgress
event in which you throw an exception.
You could also throw an exception from a BeforeWriteEvent
or an AfterWriteEvent
.
You could attach a custom field converter which throws an exception in the FieldToString()
override.
Any of the above exceptions will be trapped by the SaveAndContinue
option.