I am designing a long-running Dataflow pipeline that consists of multiple blocks. Items are fed to the input block of the pipeline, eventually make their way through it, and are displayed in the UI at the end (as a courtesy to the user -- the pipeline's real job is to save processing results to disk).
The lambda functions inside the pipeline blocks can throw exceptions, for a variety of reasons (bad input, network failure, error during calculation, whatever). In this case, instead of faulting the entire pipeline, I'd like to kick out the offending item, and display it in the UI under "Errors".
What's the best way to do that ? I understand that I can wrap every single lambda function in a try/catch:
var errorLoggingBlock = new ActionBlock<Tuple<WorkItem, Exception>>(...)
var workerBlock = new TransformBlock<WorkItem, WorkItem>(item =>
{
try {
return DoStuff(item);
} catch (Exception ex) {
errorLoggingBlock.SendAsync(Tuple.Create(item, ex));
return null;
}
}
But I have about 10 blocks in the pipeline, and copy/pasting that code into each one seems silly. Also, I don't like the idea of returning null, since now all of the downstream blocks will have to check for it.
My next best idea is to create a function that returns a lambda that does the wrapping for me:
private Func<TArg, TResult> HandleErrors<TArg, TResult>(Func<TArg, TResult> f) where TArg:WorkItem
{
return arg =>
{
try {
return f(arg);
} catch (Exception ex) {
errorLoggingBlock.SendAsync(Tuple.Create(item, ex));
return default(TResult);
}
};
}
But this seems a bit too meta. Is there a better way ?
Try
library. It allows to pass a message through all the blocks of a pipeline, and then observe any exception that occurred to this message at the end. – Melanimelania