[Enhancement] Extend Try() methods to allow manually returned results
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.
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.
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.
@altmann PR submitted. Let me know if any changes.