0

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.

shamp00
  • 11,106
  • 4
  • 38
  • 81
Ian Warburton
  • 15,170
  • 23
  • 107
  • 189

1 Answers1

1

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.

shamp00
  • 11,106
  • 4
  • 38
  • 81