vkhorikov/CSharpFunctionalExtensions

Question regarding handling soft vs hard Failures

JvSSD opened this issue · 1 comments

JvSSD commented

Say I have something like

var bars = foos.Select(foo => ConvertFooToBar(foo)
    .Combine();

Now suppose that the conversion of Foo to Bar in ConvertFooToBar(Foo foo) can fail in multiple different ways, some of which may mean that processing at the above code snippet should stop, others of which may just mean that they should be filtered out during the conversion of all the Foos to Bars.

What would be the best way to handle this situation?

Currently, I have been handling this by returning nulls from the ConvertFooToBar method and basically filtering them out like so: bars.Value.Where(x => x != null) (after have made sure that .Value won't throw). But this situation has the downside of not having an error message in those filter cases, which may of interest, for example, during debugging or when all Foo-to-Bar conversions have failed in this soft way and the above processing should still stop (i.e. why were all filtered out?).

Introduce a separate intermediate class to hold the results of the conversion. This is where C# records are a good fit. You can do something similar to this:

Result<Conversion>[] bars = foos
    .Select(foo => ConvertFooToBar(foo)
    .Combine();

record Conversion(Maybe<Bar> Bar, string ConversionFailure);