From e1c9a03c7681cd867907c00576b9a20c5c32b976 Mon Sep 17 00:00:00 2001 From: Kyle Slusser Date: Thu, 11 Jul 2024 20:47:01 -0400 Subject: [PATCH] Add Try with result return Adds new methods of Try that allow you to return a new result object --- README.md | 15 ++ .../ResultWithValueTests.cs | 178 +++++++++++++++--- .../ResultWithoutValueTests.cs | 163 +++++++++++++--- src/FluentResults/Factories/Results.cs | 115 ++++++++++- 4 files changed, 414 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 032e0e7..3d2cd06 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,21 @@ In some scenarios you want to execute an action. If this action throws an except var result = Result.Try(() => DoSomethingCritical()); ``` +You can also return your own `Result` object + +```csharp +var result = Result.Try(() => { + if(IsInvalid()) + { + return Result.Fail("Some error"); + } + + int id = DoSomethingCritical(); + + return Result.Ok(id); +}); +``` + In the above example the default catchHandler is used. The behavior of the default catchHandler can be overwritten via the global Result settings (see next example). You can control how the Error object looks. ```csharp diff --git a/src/FluentResults.Test/ResultWithValueTests.cs b/src/FluentResults.Test/ResultWithValueTests.cs index ea25e2e..803f016 100644 --- a/src/FluentResults.Test/ResultWithValueTests.cs +++ b/src/FluentResults.Test/ResultWithValueTests.cs @@ -294,7 +294,7 @@ public void Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResult() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultTask() { @@ -309,7 +309,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultTask .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultValueTask() { @@ -324,7 +324,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultValu .Should() .BeEquivalentTo("First error message"); } - + [Fact] public void Bind_ToResultWithFailedResult_ReturnFailedResult() { @@ -339,7 +339,7 @@ public void Bind_ToResultWithFailedResult_ReturnFailedResult() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResult_ReturnFailedResultTask() { @@ -354,7 +354,7 @@ public async Task Bind_ToResultWithFailedResult_ReturnFailedResultTask() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResult_ReturnFailedResultValueTask() { @@ -369,7 +369,7 @@ public async Task Bind_ToResultWithFailedResult_ReturnFailedResultValueTask() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public void Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_ReturnFailedResult() { @@ -384,7 +384,7 @@ public void Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_Retur .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_ReturnFailedResultTask() { @@ -399,7 +399,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_ReturnFailedResultValueTask() { @@ -414,7 +414,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public void Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedResult() { @@ -429,7 +429,7 @@ public void Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedRes .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedResultTask() { @@ -444,7 +444,7 @@ public async Task Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFai .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedResultValueTask() { @@ -466,8 +466,8 @@ public void Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResult() var valueResult = Result.Ok(1).WithSuccess("An int"); // Act - var result = valueResult.Bind(n => n == 1 - ? "One".ToResult().WithSuccess("It is one") + var result = valueResult.Bind(n => n == 1 + ? "One".ToResult().WithSuccess("It is one") : Result.Fail("Only one accepted")); // Assert @@ -479,15 +479,15 @@ public void Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResult() .Should() .BeEquivalentTo("An int", "It is one"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultTask() { var valueResult = Result.Ok(1).WithSuccess("An int"); // Act - var result = await valueResult.Bind(n => Task.FromResult(n == 1 - ? "One".ToResult().WithSuccess("It is one") + var result = await valueResult.Bind(n => Task.FromResult(n == 1 + ? "One".ToResult().WithSuccess("It is one") : Result.Fail("Only one accepted"))); // Assert @@ -499,15 +499,15 @@ public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultT .Should() .BeEquivalentTo("An int", "It is one"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultValueTask() { var valueResult = Result.Ok(1).WithSuccess("An int"); // Act - var result = await valueResult.Bind(n => new ValueTask>(n == 1 - ? "One".ToResult().WithSuccess("It is one") + var result = await valueResult.Bind(n => new ValueTask>(n == 1 + ? "One".ToResult().WithSuccess("It is one") : Result.Fail("Only one accepted"))); // Assert @@ -519,7 +519,7 @@ public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultV .Should() .BeEquivalentTo("An int", "It is one"); } - + [Fact] public void Bind_ToResultWhichIsSuccessful_ReturnsSuccessResult() { @@ -534,7 +534,7 @@ public void Bind_ToResultWhichIsSuccessful_ReturnsSuccessResult() .Should() .BeEquivalentTo("First number", "It is one"); } - + [Fact] public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultTask() { @@ -549,7 +549,7 @@ public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultTask() .Should() .BeEquivalentTo("First number", "It is one"); } - + [Fact] public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultValueTask() { @@ -564,7 +564,7 @@ public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultValueTask() .Should() .BeEquivalentTo("First number", "It is one"); } - + [Fact] public void Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResult() { @@ -580,7 +580,7 @@ public void Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResult .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResultTask() { @@ -596,7 +596,7 @@ public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailed .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResultValueTask() { @@ -612,7 +612,7 @@ public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailed .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public void Bind_ToResultWhichFailedTransformation_ReturnsFailedResult() { @@ -628,7 +628,7 @@ public void Bind_ToResultWhichFailedTransformation_ReturnsFailedResult() .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToResultWhichFailedTransformation_ReturnsFailedResultTask() { @@ -644,7 +644,7 @@ public async Task Bind_ToResultWhichFailedTransformation_ReturnsFailedResultTask .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToResultWhichFailedTransformation_ReturnsFailedResultValueTask() { @@ -797,6 +797,126 @@ public async Task Try_execute_failed_valuetask_async_action_with_custom_catchHan error.Message.Should().Be("xy"); } + [Fact] + public void Try_execute_failed_func_return_failed_result() + { + var error = new Error("xy"); + Result Action() => Result.Fail(error); + + var result = Result.Try(Action); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + result.Errors.First().Should().Be(error); + } + + [Fact] + public async Task Try_execute_failed_func_async_return_failed_result() + { + var error = new Error("xy"); + Task> Action() => Task.FromResult(Result.Fail(error)); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + result.Errors.First().Should().Be(error); + } + + [Fact] + public async Task Try_execute_failed_valuetask_func_async_return_failed_result() + { + var error = new Error("xy"); + ValueTask> Action() => new ValueTask>(Result.Fail(error)); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + result.Errors.First().Should().Be(error); + } + + [Fact] + public void Try_execute_success_func_return_success_result() + { + Result Action() => Result.Ok(5); + + var result = Result.Try(Action); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(5); + result.Errors.Should().BeEmpty(); + } + + [Fact] + public async Task Try_execute_success_func_async_return_success_result() + { + Task> Action() => Task.FromResult(Result.Ok(5)); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(5); + result.Errors.Should().BeEmpty(); + } + + [Fact] + public async Task Try_execute_success_valuetask_func_async_return_success_result() + { + ValueTask> Action() => new ValueTask>(Result.Ok(5)); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(5); + result.Errors.Should().BeEmpty(); + } + + [Fact] + public void Try_execute_withresult_failed_task_action_with_custom_catchHandler_return_failed_result() + { + var exception = new Exception("ex message"); + Result Action() => throw exception; + + var result = Result.Try(Action, _ => new Error("xy")); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + + var error = result.Errors.First(); + error.Message.Should().Be("xy"); + } + + [Fact] + public async Task Try_execute_withresult_failed_task_async_action_with_custom_catchHandler_return_failed_result() + { + var exception = new Exception("ex message"); + Task> Action() => throw exception; + + var result = await Result.Try(Action, _ => new Error("xy")); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + + var error = result.Errors.First(); + error.Message.Should().Be("xy"); + } + + [Fact] + public async Task Try_execute_withresult_failed_valuetask_async_action_with_custom_catchHandler_return_failed_result() + { + var exception = new Exception("ex message"); + ValueTask> Action() => throw exception; + + var result = await Result.Try(Action, _ => new Error("xy")); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + + var error = result.Errors.First(); + error.Message.Should().Be("xy"); + } + [Fact] public void Implicit_conversion_T_is_converted_to_Success_result_of_T() { @@ -954,7 +1074,7 @@ private static Result DynamicConvert(dynamic source, Type dest) { var result = new Result(); var converted = Convert.ChangeType(source, dest); - var x = result.WithValue(converted); + var x = result.WithValue(converted); return x; } } diff --git a/src/FluentResults.Test/ResultWithoutValueTests.cs b/src/FluentResults.Test/ResultWithoutValueTests.cs index dcefa38..1a24273 100644 --- a/src/FluentResults.Test/ResultWithoutValueTests.cs +++ b/src/FluentResults.Test/ResultWithoutValueTests.cs @@ -1,6 +1,6 @@ +using FluentAssertions; using System; using System.Collections.Generic; -using FluentAssertions; using System.Linq; using System.Threading.Tasks; using Xunit; @@ -486,6 +486,123 @@ public async Task Try_execute_failed_valuetask_async_action_with_custom_catchHan error.Message.Should().Be("xy"); } + [Fact] + public void Try_execute_failed_func_return_failed_result() + { + var error = new Error("xy"); + Result Action() => Result.Fail(error); + + var result = Result.Try(Action); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + result.Errors.First().Should().Be(error); + } + + [Fact] + public async Task Try_execute_failed_func_async_return_failed_result() + { + var error = new Error("xy"); + Task Action() => Task.FromResult(Result.Fail(error)); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + result.Errors.First().Should().Be(error); + } + + [Fact] + public async Task Try_execute_failed_valuetask_func_async_return_failed_result() + { + var error = new Error("xy"); + ValueTask Action() => new ValueTask(Result.Fail(error)); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + result.Errors.First().Should().Be(error); + } + + [Fact] + public void Try_execute_success_func_return_success_result() + { + Result Action() => Result.Ok(); + + var result = Result.Try(Action); + + result.IsSuccess.Should().BeTrue(); + result.Errors.Should().BeEmpty(); + } + + [Fact] + public async Task Try_execute_success_func_async_return_success_result() + { + Task Action() => Task.FromResult(Result.Ok()); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeTrue(); + result.Errors.Should().BeEmpty(); + } + + [Fact] + public async Task Try_execute_success_valuetask_func_async_return_success_result() + { + ValueTask Action() => new ValueTask(Result.Ok()); + + var result = await Result.Try(Action); + + result.IsSuccess.Should().BeTrue(); + result.Errors.Should().BeEmpty(); + } + + [Fact] + public void Try_execute_withresult_failed_task_action_with_custom_catchHandler_return_failed_result() + { + var exception = new Exception("ex message"); + Result Action() => throw exception; + + var result = Result.Try(Action, _ => new Error("xy")); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + + var error = result.Errors.First(); + error.Message.Should().Be("xy"); + } + + [Fact] + public async Task Try_execute_withresult_failed_task_async_action_with_custom_catchHandler_return_failed_result() + { + var exception = new Exception("ex message"); + Task Action() => throw exception; + + var result = await Result.Try(Action, _ => new Error("xy")); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + + var error = result.Errors.First(); + error.Message.Should().Be("xy"); + } + + [Fact] + public async Task Try_execute_withresult_failed_valuetask_async_action_with_custom_catchHandler_return_failed_result() + { + var exception = new Exception("ex message"); + ValueTask Action() => throw exception; + + var result = await Result.Try(Action, _ => new Error("xy")); + + result.IsSuccess.Should().BeFalse(); + result.Errors.Should().HaveCount(1); + + var error = result.Errors.First(); + error.Message.Should().Be("xy"); + } + [Fact] public void Can_deconstruct_non_generic_Ok_to_isSuccess_and_isFailed() { @@ -543,7 +660,7 @@ public void Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResult() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultTask() { @@ -558,7 +675,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultTask .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultValueTask() { @@ -573,7 +690,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResult_ReturnFailedResultValu .Should() .BeEquivalentTo("First error message"); } - + [Fact] public void Bind_ToResultWithFailedResult_ReturnFailedResult() { @@ -588,7 +705,7 @@ public void Bind_ToResultWithFailedResult_ReturnFailedResult() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResult_ReturnFailedResultTask() { @@ -603,7 +720,7 @@ public async Task Bind_ToResultWithFailedResult_ReturnFailedResultTask() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResult_ReturnFailedResultValueTask() { @@ -618,7 +735,7 @@ public async Task Bind_ToResultWithFailedResult_ReturnFailedResultValueTask() .Should() .BeEquivalentTo("First error message"); } - + [Fact] public void Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_ReturnFailedResult() { @@ -633,7 +750,7 @@ public void Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_Retur .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_ReturnFailedResultTask() { @@ -648,7 +765,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation_ReturnFailedResultValueTask() { @@ -663,7 +780,7 @@ public async Task Bind_ToAnotherValueTypeWithFailedResultAndFailedTransformation .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public void Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedResult() { @@ -678,7 +795,7 @@ public void Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedRes .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedResultTask() { @@ -693,7 +810,7 @@ public async Task Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFai .Should() .BeEquivalentTo("Original error message"); } - + [Fact] public async Task Bind_ToResultWithFailedResultAndFailedTransformation_ReturnFailedResultValueTask() { @@ -726,7 +843,7 @@ public void Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResult() .Should() .BeEquivalentTo("An int", "It is one"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultTask() { @@ -744,7 +861,7 @@ public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultT .Should() .BeEquivalentTo("An int", "It is one"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultValueTask() { @@ -762,7 +879,7 @@ public async Task Bind_ToAnotherValueTypeWhichIsSuccessful_ReturnsSuccessResultV .Should() .BeEquivalentTo("An int", "It is one"); } - + [Fact] public void Bind_ToResultWhichIsSuccessful_ReturnsSuccessResult() { @@ -777,7 +894,7 @@ public void Bind_ToResultWhichIsSuccessful_ReturnsSuccessResult() .Should() .BeEquivalentTo("First number", "It is one"); } - + [Fact] public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultTask() { @@ -792,7 +909,7 @@ public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultTask() .Should() .BeEquivalentTo("First number", "It is one"); } - + [Fact] public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultValueTask() { @@ -807,7 +924,7 @@ public async Task Bind_ToResultWhichIsSuccessful_ReturnsSuccessResultValueTask() .Should() .BeEquivalentTo("First number", "It is one"); } - + [Fact] public void Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResult() { @@ -823,7 +940,7 @@ public void Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResult .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResultTask() { @@ -839,7 +956,7 @@ public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailed .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailedResultValueTask() { @@ -855,7 +972,7 @@ public async Task Bind_ToAnotherValueTypeWhichFailedTransformation_ReturnsFailed .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public void Bind_ToResultWhichFailedTransformation_ReturnsFailedResult() { @@ -871,7 +988,7 @@ public void Bind_ToResultWhichFailedTransformation_ReturnsFailedResult() .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToResultWhichFailedTransformation_ReturnsFailedResultTask() { @@ -887,7 +1004,7 @@ public async Task Bind_ToResultWhichFailedTransformation_ReturnsFailedResultTask .Should() .BeEquivalentTo("Only one accepted"); } - + [Fact] public async Task Bind_ToResultWhichFailedTransformation_ReturnsFailedResultValueTask() { diff --git a/src/FluentResults/Factories/Results.cs b/src/FluentResults/Factories/Results.cs index 58921b5..ca12b4e 100644 --- a/src/FluentResults/Factories/Results.cs +++ b/src/FluentResults/Factories/Results.cs @@ -33,7 +33,7 @@ public static Result Ok() { return new Result(); } - + /// /// Creates a failed result with the given error /// @@ -53,7 +53,7 @@ public static Result Fail(string errorMessage) result.WithError(Settings.ErrorFactory(errorMessage)); return result; } - + /// /// Creates a failed result with the given error messages. Internally a list of error objects from the error factory is created /// @@ -61,7 +61,7 @@ public static Result Fail(IEnumerable errorMessages) { if (errorMessages == null) throw new ArgumentNullException(nameof(errorMessages), "The list of error messages cannot be null"); - + var result = new Result(); result.WithErrors(errorMessages.Select(Settings.ErrorFactory)); return result; @@ -109,7 +109,7 @@ public static Result Fail(string errorMessage) result.WithError(Settings.ErrorFactory(errorMessage)); return result; } - + /// /// Creates a failed result with the given error messages. Internally a list of error objects from the error factory is created. /// @@ -117,7 +117,7 @@ public static Result Fail(IEnumerable errorMessages) { if (errorMessages == null) throw new ArgumentNullException(nameof(errorMessages), "The list of error messages cannot be null"); - + var result = new Result(); result.WithErrors(errorMessages.Select(Settings.ErrorFactory)); return result; @@ -332,5 +332,110 @@ public static async ValueTask> Try(Func> action, Func< return Fail(catchHandler(e)); } } + + /// + /// Executes the action. If an exception is thrown within the action then this exception is transformed via the catchHandler to an Error object + /// + public static Result Try(Func action, Func catchHandler = null) + { + catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler; + + try + { + return action(); + } + catch (Exception e) + { + return Fail(catchHandler(e)); + } + + } + + /// + /// Executes the action. If an exception is thrown within the action then this exception is transformed via the catchHandler to an Error object + /// + public static async Task Try(Func> action, Func catchHandler = null) + { + catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler; + + try + { + return await action(); + } + catch (Exception e) + { + return Fail(catchHandler(e)); + } + } + + /// + /// Executes the action. If an exception is thrown within the action then this exception is transformed via the catchHandler to an Error object + /// + public static async ValueTask Try(Func> action, Func catchHandler = null) + { + catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler; + + try + { + return await action(); + } + catch (Exception e) + { + return Fail(catchHandler(e)); + } + } + + /// + /// Executes the action. If an exception is thrown within the action then this exception is transformed via the catchHandler to an Error object + /// + public static Result Try(Func> action, Func catchHandler = null) + { + catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler; + + try + { + return action(); + } + catch (Exception e) + { + return Fail(catchHandler(e)); + } + + } + + /// + /// Executes the action. If an exception is thrown within the action then this exception is transformed via the catchHandler to an Error object + /// + public static async Task> Try(Func>> action, Func catchHandler = null) + { + catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler; + + try + { + return await action(); + } + catch (Exception e) + { + return Fail(catchHandler(e)); + } + } + + /// + /// Executes the action. If an exception is thrown within the action then this exception is transformed via the catchHandler to an Error object + /// + public static async ValueTask> Try(Func>> action, Func catchHandler = null) + { + catchHandler = catchHandler ?? Settings.DefaultTryCatchHandler; + + try + { + return await action(); + } + catch (Exception e) + { + return Fail(catchHandler(e)); + } + } + } } \ No newline at end of file