FluentResults icon indicating copy to clipboard operation
FluentResults copied to clipboard

[Enhancement] Extend Try() methods to allow manually returned results

Open theiam79 opened this issue 3 years ago • 1 comments

I think it would be a nice QoL/Convenience feature to extend the Result.Try() methods to allow for manually returned results. Essentially, if the given function itself returns a result, simply return that result unless an exception is thrown, in which case it is wrapped and returned as a result.

public static Result<T> Try<T>(Func<Result<T>> action, Func<Exception, IError> catchHandler = null)
{
    catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler;

    try
    {
        return action();
    }
    catch (Exception e)
    {
        return Fail(catchHandler(e));
    }
}

What are your thoughts on this? I'd be happy to submit a PR if you want.

Thanks for the excellent library, it's been great to use.

theiam79 avatar Nov 11 '22 22:11 theiam79

Thank you for the feedback. I think its a good idea. Please submit a pr and please don't forget also the async Task methods, the tests and the docu in the readme.

I will increase the version number and the changelog.

altmann avatar Nov 14 '22 18:11 altmann

Stumbled across this while looking for the same thing. I will create a PR later for this because I think it would be a great addition.

Kysluss avatar Jul 11 '24 18:07 Kysluss

@altmann PR submitted. Let me know if any changes.

Kysluss avatar Jul 12 '24 01:07 Kysluss