├── SimpleResult ├── logo.png ├── Error.cs ├── IError.cs ├── GlobalUsings.cs ├── IsExternalInit.cs ├── IResult.cs ├── Failure.cs ├── SimpleResult.csproj ├── RunCatchingExtensions.cs ├── Result.cs ├── ResultAsyncExtensions.cs └── ResultExtensions.cs ├── SimpleResult.Tests ├── CustomError.cs ├── SimpleResult.Tests.csproj ├── SucceedResultTests.cs ├── GetResultTests.cs ├── ResultTests.cs ├── GetericResultTests.cs ├── RunCatchingExtensionsTest.cs ├── MapResultTests.cs ├── ResultAsyncExtentionsTests.cs └── HandleFailureTests.cs ├── .github └── workflows │ ├── dotnet.yml │ └── dotnet-desktop.yml ├── SimpleResult.sln ├── .gitattributes ├── .gitignore └── README.md /SimpleResult/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/araxis/SimpleResult/HEAD/SimpleResult/logo.png -------------------------------------------------------------------------------- /SimpleResult.Tests/CustomError.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult.Tests; 2 | 3 | public record CustomError(string Message) : IError; -------------------------------------------------------------------------------- /SimpleResult/Error.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | 3 | public record Error(string Message) : IError; 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /SimpleResult/IError.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | 3 | public interface IError 4 | { 5 | public string Message { get; } 6 | } -------------------------------------------------------------------------------- /SimpleResult/GlobalUsings.cs: -------------------------------------------------------------------------------- 1 | // Global using directives 2 | 3 | global using Errors = System.Collections.Generic.IReadOnlyCollection; -------------------------------------------------------------------------------- /SimpleResult/IsExternalInit.cs: -------------------------------------------------------------------------------- 1 | 2 | //to fix Visual Studio 2019 bug !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 3 | namespace System.Runtime.CompilerServices 4 | { 5 | internal static class IsExternalInit {} 6 | } -------------------------------------------------------------------------------- /SimpleResult/IResult.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | 3 | public interface IResult 4 | { 5 | bool IsSuccess { get; } 6 | bool IsFailure { get; } 7 | IReadOnlyList Errors { get; } 8 | Exception? ExceptionOrNull(); 9 | bool HasException() where T:Exception; 10 | bool HasInnerException() where T:Exception; 11 | bool HasError() where T:IError; 12 | } 13 | 14 | public interface IResult : IResult 15 | { 16 | T GetOrDefault(); 17 | } -------------------------------------------------------------------------------- /SimpleResult/Failure.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | 3 | internal class Failure 4 | { 5 | public Exception? Exception { get; } 6 | public IReadOnlyList ErrorInfos { get; } 7 | public Failure(params IError[] errorInfos) 8 | { 9 | ErrorInfos = new List(errorInfos); 10 | Exception = null; 11 | } 12 | public Failure(Exception? exception,params IError[] errors) 13 | { 14 | Exception = exception; 15 | ErrorInfos = errors.ToList(); 16 | } 17 | } -------------------------------------------------------------------------------- /.github/workflows/dotnet.yml: -------------------------------------------------------------------------------- 1 | name: .NET 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Setup .NET 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: 7.0.x 20 | - name: Restore dependencies 21 | run: dotnet restore 22 | - name: Build 23 | run: dotnet build --no-restore 24 | - name: Test 25 | run: dotnet test --no-build --verbosity normal 26 | -------------------------------------------------------------------------------- /.github/workflows/dotnet-desktop.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build-and-test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v2 15 | 16 | - name: Install .NET SDK 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: 7.0 # Replace with the version of .NET 7 you want to use 20 | 21 | - name: Restore dependencies 22 | run: dotnet restore 23 | 24 | - name: Build 25 | run: dotnet build --configuration Release 26 | 27 | - name: Run Tests 28 | run: dotnet test --configuration Release 29 | -------------------------------------------------------------------------------- /SimpleResult/SimpleResult.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | netstandard2.0;net6.0;net7.0 6 | enable 7 | enable 8 | 11.0 9 | Meisam Alifallahi 10 | Result<T> is a monad for modelling success (Ok) or failure (Err) operations. 11 | Arax.SimpleResult 12 | 1.3.3 13 | https://github.com/araxis/SimpleResult 14 | https://github.com/araxis/SimpleResult 15 | logo.png 16 | 17 | 18 | 19 | 20 | 21 | True 22 | \ 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /SimpleResult/RunCatchingExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | public static class RunCatchingExtensions 3 | { 4 | public static IResult RunCatching(this Action block) 5 | { 6 | try 7 | { 8 | block(); 9 | return Result.Success(); 10 | } 11 | catch (Exception ex) 12 | { 13 | return Result.Fail(ex); 14 | } 15 | } 16 | 17 | public static IResult RunCatching(this Func block) 18 | { 19 | try 20 | { 21 | return Result.Success(block()); 22 | } 23 | catch (Exception ex) 24 | { 25 | return Result.Fail(ex); 26 | } 27 | } 28 | 29 | public static async Task> RunCatching(this Func> block) 30 | { 31 | try 32 | { 33 | var result = await block(); 34 | return Result.Success(result); 35 | } 36 | catch (Exception ex) 37 | { 38 | return Result.Fail(ex); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /SimpleResult.Tests/SimpleResult.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | all 17 | 18 | 19 | runtime; build; native; contentfiles; analyzers; buildtransitive 20 | all 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /SimpleResult.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31912.275 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleResult", "SimpleResult\SimpleResult.csproj", "{47B84BE8-E563-4BBD-A775-FD0ACAF1E96C}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleResult.Tests", "SimpleResult.Tests\SimpleResult.Tests.csproj", "{942BCEBD-E033-46DD-B271-EC13B44375E9}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {47B84BE8-E563-4BBD-A775-FD0ACAF1E96C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {47B84BE8-E563-4BBD-A775-FD0ACAF1E96C}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {47B84BE8-E563-4BBD-A775-FD0ACAF1E96C}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {47B84BE8-E563-4BBD-A775-FD0ACAF1E96C}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {942BCEBD-E033-46DD-B271-EC13B44375E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {942BCEBD-E033-46DD-B271-EC13B44375E9}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {942BCEBD-E033-46DD-B271-EC13B44375E9}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {942BCEBD-E033-46DD-B271-EC13B44375E9}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {28B7CC51-4490-4479-92D3-DB2FED93F9FD} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /SimpleResult.Tests/SucceedResultTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace SimpleResult.Tests; 5 | 6 | public class SucceedResultTests 7 | { 8 | [Fact(DisplayName = "OnSuccess: When result is success, calls action")] 9 | [Trait("Category", "OnSuccess")] 10 | public void OnSuccess_Success_CallsAction() 11 | { 12 | // Arrange 13 | var result = Result.Success(); 14 | bool actionCalled = false; 15 | 16 | // Act 17 | result.OnSuccess(() => actionCalled = true); 18 | 19 | // Assert 20 | actionCalled.Should().BeTrue(); 21 | } 22 | 23 | [Fact(DisplayName = "OnSuccess: When result is failure, does not call action")] 24 | [Trait("Category", "OnSuccess")] 25 | public void OnSuccess_Failure_DoesNotCallAction() 26 | { 27 | // Arrange 28 | var result = Result.Fail(new CustomError("error")); 29 | bool actionCalled = false; 30 | 31 | // Act 32 | result.OnSuccess(() => actionCalled = true); 33 | 34 | // Assert 35 | actionCalled.Should().BeFalse(); 36 | } 37 | 38 | [Fact(DisplayName = "OnSuccess: When result is success, calls action with value")] 39 | [Trait("Category", "OnSuccess")] 40 | public void OnSuccessT_Success_CallsActionWithValue() 41 | { 42 | // Arrange 43 | var result = Result.Success(42); 44 | int value = 0; 45 | 46 | // Act 47 | result.OnSuccess(x => value = x); 48 | 49 | // Assert 50 | value.Should().Be(42); 51 | } 52 | 53 | [Fact(DisplayName = "OnSuccess: When result is failure, does not call action with value")] 54 | [Trait("Category", "OnSuccess")] 55 | public void OnSuccessT_Failure_DoesNotCallActionWithValue() 56 | { 57 | // Arrange 58 | var result = Result.Fail(new CustomError("error")); 59 | int value = 0; 60 | 61 | // Act 62 | result.OnSuccess(x => value = x); 63 | 64 | // Assert 65 | value.Should().Be(0); 66 | } 67 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /SimpleResult.Tests/GetResultTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using System; 3 | using Xunit; 4 | 5 | namespace SimpleResult.Tests; 6 | 7 | public class GetResultTests 8 | { 9 | [Fact(DisplayName = "ThrowOnFailure throws exception when IResult is failure")] 10 | [Trait("Category", "OnFailure")] 11 | public void ThrowOnFailure_WhenIResultIsFailure_ThrowsException() 12 | { 13 | // Arrange 14 | var result = Result.Fail(new Exception("Something went wrong")); 15 | 16 | // Act and assert 17 | Assert.Throws(() => result.ThrowOnFailure()); 18 | } 19 | 20 | [Fact(DisplayName = "ThrowOnFailure does not throw exception when IResult is success")] 21 | [Trait("Category", "OnFailure")] 22 | public void ThrowOnFailure_WhenIResultIsSuccess_DoesNotThrowException() 23 | { 24 | // Arrange 25 | var result = Result.Success(); 26 | 27 | // Act 28 | result.ThrowOnFailure(); 29 | 30 | // Assert 31 | Assert.True(true); // No exception was thrown 32 | } 33 | 34 | [Fact(DisplayName = "GetOrThrow throws exception when IResult is failure")] 35 | [Trait("Category", "OnFailure")] 36 | public void GetOrThrow_WhenIResultIsFailure_ThrowsException() 37 | { 38 | // Arrange 39 | var result = Result.Fail(new Exception("Something went wrong")); 40 | 41 | // Act and assert 42 | Assert.Throws(() => result.GetOrThrow()); 43 | } 44 | 45 | [Fact(DisplayName = "GetOrThrow returns the value when IResult is success")] 46 | [Trait("Category", "OnSuccess")] 47 | public void GetOrThrow_WhenIResultIsSuccess_ReturnsValue() 48 | { 49 | // Arrange 50 | var expectedValue = 42; 51 | var result = Result.Success(expectedValue); 52 | 53 | // Act 54 | var actualValue = result.GetOrThrow(); 55 | 56 | // Assert 57 | actualValue.Should().Be(expectedValue); 58 | } 59 | 60 | [Fact(DisplayName = "GetOrDefault returns the default value when IResult is failure")] 61 | [Trait("Category", "OnFailure")] 62 | public void GetOrDefault_WhenIResultIsFailure_ReturnsDefaultValue() 63 | { 64 | // Arrange 65 | var defaultValue = 0; 66 | var result = Result.Fail(new Exception("Something went wrong")); 67 | 68 | // Act 69 | var actualValue = result.GetOrDefault(defaultValue); 70 | 71 | // Assert 72 | actualValue.Should().Be(defaultValue); 73 | } 74 | 75 | [Fact(DisplayName = "GetOrDefault returns the value when IResult is success")] 76 | [Trait("Category", "OnSuccess")] 77 | public void GetOrDefault_WhenIResultIsSuccess_ReturnsValue() 78 | { 79 | // Arrange 80 | var expectedValue = 42; 81 | var defaultValue = 0; 82 | var result = Result.Success(expectedValue); 83 | 84 | // Act 85 | var actualValue = result.GetOrDefault(defaultValue); 86 | 87 | // Assert 88 | actualValue.Should().Be(expectedValue); 89 | } 90 | } -------------------------------------------------------------------------------- /SimpleResult.Tests/ResultTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentAssertions; 3 | using Xunit; 4 | 5 | namespace SimpleResult.Tests; 6 | 7 | public class ResultTests 8 | { 9 | 10 | [Fact] 11 | public void Should_IndicateSuccess_When_ResultIsSuccess() 12 | { 13 | // Arrange 14 | var result = Result.Success(); 15 | 16 | // Act & Assert 17 | result.IsSuccess.Should().BeTrue(); 18 | } 19 | 20 | [Fact] 21 | public void Should_NotIndicateFailure_When_ResultIsSuccess() 22 | { 23 | // Arrange 24 | var result = Result.Success(); 25 | 26 | // Act & Assert 27 | result.IsFailure.Should().BeFalse(); 28 | } 29 | 30 | [Fact] 31 | public void Should_NotIndicateSuccess_When_ResultIsFailure() 32 | { 33 | // Arrange 34 | var result = Result.Fail(new InvalidOperationException()); 35 | 36 | // Act & Assert 37 | result.IsSuccess.Should().BeFalse(); 38 | } 39 | 40 | [Fact] 41 | public void Should_IndicateFailure_When_ResultIsFailure() 42 | { 43 | // Arrange 44 | var result = Result.Fail(new InvalidOperationException()); 45 | 46 | // Act & Assert 47 | result.IsFailure.Should().BeTrue(); 48 | } 49 | 50 | [Fact] 51 | public void Should_ContainException_When_ResultIsFailure() 52 | { 53 | // Arrange 54 | var expectedException = new InvalidOperationException(); 55 | var result = Result.Fail(expectedException); 56 | 57 | // Act 58 | var exception = result.ExceptionOrNull(); 59 | 60 | // Assert 61 | exception.Should().Be(expectedException); 62 | } 63 | 64 | [Fact] 65 | public void Should_NotContainException_When_ResultIsSuccess() 66 | { 67 | // Arrange 68 | var result = Result.Success(); 69 | 70 | // Act 71 | var exception = result.ExceptionOrNull(); 72 | 73 | // Assert 74 | exception.Should().BeNull(); 75 | } 76 | 77 | [Fact] 78 | public void Should_ContainErrors_When_ResultIsFailureWithErrors() 79 | { 80 | // Arrange 81 | var error1 = new Error("Error 1"); 82 | var error2 = new Error("Error 2"); 83 | var result = Result.Fail(new[] { error1, error2 }); 84 | 85 | // Act 86 | var errors = result.Errors; 87 | 88 | // Assert 89 | errors.Should().Contain(new[] { error1, error2 }); 90 | } 91 | 92 | [Fact] 93 | public void Should_CreateFailureResult_When_ImplicitConversionFromExceptionToResult() 94 | { 95 | // Arrange 96 | var exception = new InvalidOperationException(); 97 | 98 | // Act 99 | Result result = exception; 100 | 101 | // Assert 102 | result.IsFailure.Should().BeTrue(); 103 | result.ExceptionOrNull().Should().Be(exception); 104 | } 105 | 106 | [Fact] 107 | public void Should_CreateFailureResult_When_ImplicitConversionFromErrorArrayToResult() 108 | { 109 | // Arrange 110 | var errors = new[] { new Error("Error 1"), new Error("Error 2") }; 111 | 112 | // Act 113 | Result result = errors; 114 | 115 | // Assert 116 | result.IsFailure.Should().BeTrue(); 117 | result.Errors.Should().Contain(errors); 118 | } 119 | 120 | } -------------------------------------------------------------------------------- /SimpleResult/Result.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | 3 | public readonly record struct Result : IResult 4 | { 5 | private readonly Failure? _failure; 6 | 7 | // Use expression-bodied properties for IsSuccess and IsFailure 8 | public bool IsSuccess => _failure == null; 9 | public bool IsFailure => _failure != null; 10 | private Result(Failure? failure) 11 | { 12 | _failure = failure; 13 | } 14 | public IReadOnlyList Errors => _failure?.ErrorInfos ?? new List(); 15 | public Exception? ExceptionOrNull() => _failure?.Exception; 16 | public bool HasException() where TException : Exception => ExceptionOrNull() is TException; 17 | public bool HasInnerException() where TException : Exception => ExceptionOrNull()?.InnerException is TException; 18 | public bool HasError() where TError : IError => Errors.OfType().Any(); 19 | public static IResult Fail(Exception exception) => new Result(new Failure(exception)); 20 | public static Result Success() => new(null); 21 | public static Result Fail(IEnumerable resultErrors) => new(new Failure(resultErrors.ToArray())); 22 | public static Result Fail(params string[] message) 23 | { 24 | var errors = message.Select(m =>(IError) new Error(m)).ToArray(); 25 | return Fail(errors); 26 | } 27 | public static Result Fail(Exception exception,params string[] message) 28 | { 29 | var errors = message.Select(m =>(IError) new Error(m)).ToArray(); 30 | return Fail(exception,errors); 31 | } 32 | 33 | public static Result Fail(Exception exception,params IError[] errors) => new( new Failure(exception,errors)); 34 | public static Result Fail(Exception exception,IEnumerable errors) => Fail(exception,errors.ToArray()); 35 | public static Result Fail(params IError[] resultErrors) => new(new Failure(resultErrors)); 36 | 37 | public static implicit operator Result(Exception exception) => (Result)Fail(exception); 38 | public static implicit operator Result(IError[] errorInfo) => Fail(errorInfo); 39 | public static implicit operator Result(Error errorInfo) => Fail(errorInfo); 40 | } 41 | 42 | public readonly struct Result : IResult 43 | { 44 | private readonly T _value; 45 | private readonly Failure? _failure; 46 | 47 | // Use expression-bodied properties for IsSuccess and IsFailure 48 | public bool IsSuccess => _failure == null; 49 | public bool IsFailure => _failure != null; 50 | 51 | private Result(T value, Failure? failure) 52 | { 53 | _value = value; 54 | _failure = failure; 55 | } 56 | 57 | public IReadOnlyList Errors => _failure?.ErrorInfos ?? new List(); 58 | public T GetOrDefault() => IsFailure ? default : _value; 59 | public Exception? ExceptionOrNull() => _failure?.Exception; 60 | public bool HasException() where TException : Exception => ExceptionOrNull() is TException; 61 | public bool HasInnerException() where TException : Exception => ExceptionOrNull()?.InnerException is TException; 62 | public bool HasError() where TError : IError => Errors.OfType().Any(); 63 | public static Result Success(T value) => new(value, null); 64 | public static Result Fail(Exception exception) => new(default, new Failure(exception)); 65 | public static Result Fail(Exception? exception,params IError[] errors) => new(default, new Failure(exception,errors)); 66 | public static Result Fail(Exception? exception,IEnumerable errors) =>Fail(exception,errors.ToArray()); 67 | public static Result Fail(params string[] message) 68 | { 69 | var errors = message.Select(m =>(IError) new Error(m)).ToArray(); 70 | return Fail(errors); 71 | } 72 | public static Result Fail(Exception? exception,params string[] message) 73 | { 74 | var errors = message.Select(m =>(IError) new Error(m)).ToArray(); 75 | return Fail(exception,errors); 76 | } 77 | public static Result Fail(IEnumerable errors) => new(default, new Failure(errors.ToArray())); 78 | public static Result Fail(params IError[] errors) => new(default, new Failure(errors)); 79 | 80 | public static implicit operator Result(T value) => Success(value); 81 | public static implicit operator Result(Exception exception) => Fail(exception); 82 | public static implicit operator Result(IError[] errorInfos) => Fail(errorInfos); 83 | public static implicit operator Result(Error errorInfo) => Fail(errorInfo); 84 | } -------------------------------------------------------------------------------- /SimpleResult/ResultAsyncExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleResult; 4 | 5 | public static class ResultAsyncExtensions 6 | { 7 | public static async Task OnSuccess(this IResult result, Func action) 8 | { 9 | if (result.IsSuccess) await action(); 10 | return result; 11 | } 12 | public static async Task> OnSuccess(this IResult result, Func action) 13 | { 14 | if (result.IsSuccess) await action(result.GetOrDefault()); 15 | return result; 16 | } 17 | public static async Task OnFailure(this IResult result, Func action) 18 | { 19 | 20 | if (result.IsFailure) await action(result.ExceptionOrNull(), result.Errors); 21 | return result; 22 | } 23 | public static async Task OnFailure(this IResult result, Func, Task> action) where TError : IError 24 | { 25 | if (!result.IsFailure) return result; 26 | if (result.Errors.OfType().Any()) 27 | { 28 | await action(result.ExceptionOrNull(), result.Errors.OfType().ToList()); 29 | } 30 | return result; 31 | } 32 | public static async Task OnErrors(this IResult result, Func, Task> action) where TError : IError 33 | { 34 | if (result.Errors.OfType().Any()) 35 | { 36 | await action(result.Errors.OfType().ToList()); 37 | } 38 | return result; 39 | 40 | } 41 | public static async Task OnError(this IResult result, Func action) where TError : IError 42 | { 43 | var error = result.Errors.OfType().FirstOrDefault(); 44 | if (error != null) 45 | { 46 | await action(error); 47 | } 48 | return result; 49 | 50 | } 51 | public static async Task OnException(this IResult result, Func action) 52 | { 53 | var exception = result.ExceptionOrNull(); 54 | if (exception is not null) await action(exception, result.Errors); 55 | return result; 56 | } 57 | public static async Task OnException(this IResult result, Func action) where T : Exception 58 | { 59 | var exception = result.ExceptionOrNull(); 60 | if (exception is T ex) await action(ex, result.Errors); 61 | return result; 62 | } 63 | public static async Task OnInnerException(this IResult result, Func action) where T : Exception 64 | { 65 | var exception = result.ExceptionOrNull(); 66 | if (exception?.InnerException is T ex) await action(ex, result.Errors); 67 | return result; 68 | } 69 | public static async Task Switch(this TResult result, Func onSuccess, Func onFailure) where TResult : IResult 70 | { 71 | if (result.IsSuccess) 72 | { 73 | await onSuccess(); 74 | } 75 | else 76 | { 77 | await onFailure(result.ExceptionOrNull(), result.Errors); 78 | } 79 | } 80 | public static async Task Switch(this IResult result, Func onSuccess, Func onFailure) 81 | { 82 | if (result.IsSuccess) 83 | { 84 | await onSuccess(result.GetOrDefault()); 85 | } 86 | else 87 | { 88 | await onFailure(result.ExceptionOrNull(), result.Errors); 89 | } 90 | } 91 | public static async Task Fold(this TResult result, Func> onSuccess, Func> onFailure) where TResult : IResult 92 | { 93 | return result.IsSuccess 94 | ? await onSuccess() 95 | : await onFailure(result.ExceptionOrNull(), result.Errors); 96 | } 97 | public static async Task Fold(this IResult result, Func> onSuccess, Func> onFailure) 98 | { 99 | return result.IsSuccess 100 | ? await onSuccess(result.GetOrDefault()) 101 | : await onFailure(result.ExceptionOrNull(), result.Errors); 102 | } 103 | public static async Task> Map(this Result result, Func> transform) 104 | { 105 | return result.IsSuccess 106 | ? Result.Success(await transform(result.GetOrDefault())) 107 | : Result.Fail(result.ExceptionOrNull(), result.Errors); 108 | } 109 | } -------------------------------------------------------------------------------- /SimpleResult.Tests/GetericResultTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using System; 3 | using Xunit; 4 | 5 | namespace SimpleResult.Tests; 6 | 7 | public class GenericResultTests 8 | { 9 | [Fact] 10 | public void Should_IndicateSuccess_When_ResultIsSuccess() 11 | { 12 | // Arrange 13 | var result = Result.Success("Success"); 14 | 15 | // Act & Assert 16 | result.IsSuccess.Should().BeTrue(); 17 | } 18 | 19 | [Fact] 20 | public void Should_NotIndicateFailure_When_ResultIsSuccess() 21 | { 22 | // Arrange 23 | var result = Result.Success("Success"); 24 | 25 | // Act & Assert 26 | result.IsFailure.Should().BeFalse(); 27 | } 28 | 29 | [Fact] 30 | public void Should_NotIndicateSuccess_When_ResultIsFailure() 31 | { 32 | // Arrange 33 | var result = Result.Fail(new InvalidOperationException()); 34 | 35 | // Act & Assert 36 | result.IsSuccess.Should().BeFalse(); 37 | } 38 | 39 | [Fact] 40 | public void Should_IndicateFailure_When_ResultIsFailure() 41 | { 42 | // Arrange 43 | var result = Result.Fail(new InvalidOperationException()); 44 | 45 | // Act & Assert 46 | result.IsFailure.Should().BeTrue(); 47 | } 48 | 49 | [Fact] 50 | public void Should_ContainException_When_ResultIsFailure() 51 | { 52 | // Arrange 53 | var expectedException = new InvalidOperationException(); 54 | var result = Result.Fail(expectedException); 55 | 56 | // Act 57 | var exception = result.ExceptionOrNull(); 58 | 59 | // Assert 60 | exception.Should().Be(expectedException); 61 | } 62 | 63 | [Fact] 64 | public void Should_NotContainException_When_ResultIsSuccess() 65 | { 66 | // Arrange 67 | var result = Result.Success("Success"); 68 | 69 | // Act 70 | var exception = result.ExceptionOrNull(); 71 | 72 | // Assert 73 | exception.Should().BeNull(); 74 | } 75 | 76 | [Fact] 77 | public void Should_ContainErrors_When_ResultIsFailureWithErrors() 78 | { 79 | // Arrange 80 | var error1 = new Error("Error 1"); 81 | var error2 = new Error("Error 2"); 82 | var result = Result.Fail(new[] { error1, error2 }); 83 | 84 | // Act 85 | var errors = result.Errors; 86 | 87 | // Assert 88 | errors.Should().Contain(new[] { error1, error2 }); 89 | } 90 | 91 | [Fact] 92 | public void Should_NotContainErrors_When_ResultIsSuccess() 93 | { 94 | // Arrange 95 | var result = Result.Success("Success"); 96 | 97 | // Act 98 | var errors = result.Errors; 99 | 100 | // Assert 101 | errors.Should().BeEmpty(); 102 | } 103 | 104 | [Fact] 105 | public void Should_ReturnValue_When_GetOrDefaultIsCalledOnSuccess() 106 | { 107 | // Arrange 108 | var expectedResult = "Success"; 109 | var result = Result.Success(expectedResult); 110 | 111 | // Act 112 | var value = result.GetOrDefault(); 113 | 114 | // Assert 115 | value.Should().Be(expectedResult); 116 | } 117 | 118 | [Fact] 119 | public void Should_ReturnDefault_When_GetOrDefaultIsCalledOnFailure() 120 | { 121 | // Arrange 122 | var result = Result.Fail(new InvalidOperationException()); 123 | 124 | // Act 125 | var value = result.GetOrDefault(); 126 | 127 | // Assert 128 | value.Should().Be(default(string)); 129 | } 130 | 131 | [Fact] 132 | public void Should_CreateSuccessResult_When_ImplicitConversionFromValueToResult() 133 | { 134 | // Arrange 135 | string value = "Success"; 136 | 137 | // Act 138 | Result result = value; 139 | 140 | // Assert 141 | result.IsSuccess.Should().BeTrue(); 142 | result.GetOrDefault().Should().Be(value); 143 | } 144 | [Fact] 145 | public void Should_CreateFailureResult_When_ImplicitConversionFromExceptionToResult() 146 | { 147 | // Arrange 148 | var exception = new InvalidOperationException(); 149 | 150 | // Act 151 | Result result = exception; 152 | 153 | // Assert 154 | result.IsFailure.Should().BeTrue(); 155 | result.ExceptionOrNull().Should().Be(exception); 156 | } 157 | 158 | [Fact] 159 | public void Should_CreateFailureResult_When_ImplicitConversionFromErrorArrayToResult() 160 | { 161 | // Arrange 162 | var errors = new[] { new Error("Error 1"), new Error("Error 2") }; 163 | 164 | // Act 165 | Result result = errors; 166 | 167 | // Assert 168 | result.IsFailure.Should().BeTrue(); 169 | result.Errors.Should().Contain(errors); 170 | } 171 | } -------------------------------------------------------------------------------- /SimpleResult.Tests/RunCatchingExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using FluentAssertions; 4 | using Xunit; 5 | 6 | namespace SimpleResult.Tests; 7 | 8 | public class RunCatchingExtensionsTest 9 | { 10 | [Fact] 11 | public void RunCatching_WhenActionSucceeds_ReturnsSuccess() 12 | { 13 | // Arrange 14 | Action successfulAction = () => { }; 15 | 16 | // Act 17 | var result = successfulAction.RunCatching(); 18 | 19 | // Assert 20 | result.IsSuccess.Should().BeTrue(); 21 | } 22 | 23 | [Fact] 24 | public void RunCatching_WhenActionFails_ReturnsFailure() 25 | { 26 | // Arrange 27 | Action failingAction = () => throw new InvalidOperationException("An error occurred."); 28 | 29 | // Act 30 | var result = failingAction.RunCatching(); 31 | 32 | // Assert 33 | result.IsFailure.Should().BeTrue(); 34 | } 35 | 36 | [Fact] 37 | public void RunCatching_WhenActionFails_ReturnsCorrectException() 38 | { 39 | // Arrange 40 | var expectedException = new InvalidOperationException("An error occurred."); 41 | Action failingAction = () => throw expectedException; 42 | 43 | // Act 44 | var result = failingAction.RunCatching(); 45 | 46 | // Assert 47 | result.ExceptionOrNull().Should().Be(expectedException); 48 | } 49 | [Fact] 50 | public void RunCatching_WhenFunctionSucceeds_ReturnsSuccess() 51 | { 52 | // Arrange 53 | Func successfulFunction = () => 42; 54 | 55 | // Act 56 | var result = successfulFunction.RunCatching(); 57 | 58 | // Assert 59 | result.IsSuccess.Should().BeTrue(); 60 | } 61 | 62 | [Fact] 63 | public void RunCatching_WhenFunctionSucceeds_ReturnsCorrectValue() 64 | { 65 | // Arrange 66 | Func successfulFunction = () => 42; 67 | 68 | // Act 69 | var result = successfulFunction.RunCatching(); 70 | 71 | // Assert 72 | result.GetOrDefault().Should().Be(42); 73 | } 74 | 75 | [Fact] 76 | public void RunCatching_WhenFunctionFails_ReturnsFailure() 77 | { 78 | // Arrange 79 | Func failingFunction = () => throw new InvalidOperationException("An error occurred."); 80 | 81 | // Act 82 | var result = failingFunction.RunCatching(); 83 | 84 | // Assert 85 | result.IsFailure.Should().BeTrue(); 86 | } 87 | 88 | [Fact] 89 | public void RunCatching_WhenFunctionFails_ReturnsCorrectException() 90 | { 91 | // Arrange 92 | var expectedException = new InvalidOperationException("An error occurred."); 93 | Func failingFunction = () => throw expectedException; 94 | 95 | // Act 96 | var result = failingFunction.RunCatching(); 97 | 98 | // Assert 99 | result.ExceptionOrNull().Should().Be(expectedException); 100 | } 101 | [Fact] 102 | public async Task RunCatching_WhenAsyncFunctionSucceeds_ReturnsSuccess() 103 | { 104 | // Arrange 105 | Func> successfulFunction = async () => 106 | { 107 | await Task.Delay(100); 108 | return 42; 109 | }; 110 | 111 | // Act 112 | var result = await successfulFunction.RunCatching(); 113 | 114 | // Assert 115 | result.IsSuccess.Should().BeTrue(); 116 | } 117 | 118 | [Fact] 119 | public async Task RunCatching_WhenAsyncFunctionSucceeds_ReturnsCorrectValue() 120 | { 121 | // Arrange 122 | Func> successfulFunction = async () => 123 | { 124 | await Task.Delay(100); 125 | return 42; 126 | }; 127 | 128 | // Act 129 | var result = await successfulFunction.RunCatching(); 130 | 131 | // Assert 132 | result.GetOrDefault().Should().Be(42); 133 | } 134 | 135 | [Fact] 136 | public async Task RunCatching_WhenAsyncFunctionFails_ReturnsFailure() 137 | { 138 | // Arrange 139 | Func> failingFunction = async () => 140 | { 141 | await Task.Delay(100); 142 | throw new InvalidOperationException("An error occurred."); 143 | }; 144 | 145 | // Act 146 | var result = await failingFunction.RunCatching(); 147 | 148 | // Assert 149 | result.IsFailure.Should().BeTrue(); 150 | } 151 | 152 | [Fact] 153 | public async Task RunCatching_WhenAsyncFunctionFails_ReturnsCorrectException() 154 | { 155 | // Arrange 156 | var expectedException = new InvalidOperationException("An error occurred."); 157 | Func> failingFunction = async () => 158 | { 159 | await Task.Delay(100); 160 | throw expectedException; 161 | }; 162 | 163 | // Act 164 | var result = await failingFunction.RunCatching(); 165 | 166 | // Assert 167 | result.ExceptionOrNull().Should().Be(expectedException); 168 | } 169 | } -------------------------------------------------------------------------------- /SimpleResult.Tests/MapResultTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using System; 3 | using Xunit; 4 | 5 | namespace SimpleResult.Tests; 6 | 7 | public class MapResultTests 8 | { 9 | [Fact(DisplayName = "Switch_OnSuccessExecuted_WhenResultIsSuccess"), Trait("Category", "Switch")] 10 | public void Switch_OnSuccessExecuted_WhenResultIsSuccess() 11 | { 12 | // Arrange 13 | var successResult = Result.Success(); 14 | var successCalled = false; 15 | var failureCalled = false; 16 | 17 | // Act 18 | successResult.Switch(() => successCalled = true, ex => failureCalled = true); 19 | 20 | // Assert 21 | successCalled.Should().BeTrue(); 22 | failureCalled.Should().BeFalse(); 23 | } 24 | 25 | [Fact(DisplayName = "Switch_OnFailureExecuted_WhenResultIsFailure"), Trait("Category", "Switch")] 26 | public void Switch_OnFailureExecuted_WhenResultIsFailure() 27 | { 28 | // Arrange 29 | var exception = new InvalidOperationException(); 30 | var failureResult = Result.Fail(exception); 31 | var successCalled = false; 32 | var failureCalled = false; 33 | 34 | // Act 35 | failureResult.Switch(() => successCalled = true, ex => failureCalled = true); 36 | 37 | // Assert 38 | successCalled.Should().BeFalse(); 39 | failureCalled.Should().BeTrue(); 40 | } 41 | 42 | [Fact(DisplayName = "Switch_OnSuccessExecutedWithResult_WhenResultIsSuccess"), Trait("Category", "Switch")] 43 | public void Switch_OnSuccessExecutedWithResult_WhenResultIsSuccess() 44 | { 45 | // Arrange 46 | var expectedResult = "success"; 47 | var successResult = Result.Success(expectedResult); 48 | var actualResult = ""; 49 | var failureCalled = false; 50 | 51 | // Act 52 | successResult.Switch( 53 | res => actualResult = res, 54 | ex => failureCalled = true); 55 | 56 | // Assert 57 | actualResult.Should().Be(expectedResult); 58 | failureCalled.Should().BeFalse(); 59 | } 60 | 61 | [Fact(DisplayName = "Switch_OnFailureExecutedWithException_WhenResultIsFailure"), Trait("Category", "Switch")] 62 | public void Switch_OnFailureExecutedWithException_WhenResultIsFailure() 63 | { 64 | // Arrange 65 | var exception = new InvalidOperationException(); 66 | var failureResult = Result.Fail(exception); 67 | var successCalled = false; 68 | var actualException = new Exception(); 69 | 70 | // Act 71 | failureResult.Switch( 72 | () => successCalled = true, 73 | ex => actualException = ex); 74 | 75 | // Assert 76 | successCalled.Should().BeFalse(); 77 | actualException.Should().Be(exception); 78 | } 79 | 80 | [Fact(DisplayName = "Fold_WhenSuccess_ReturnsOnSuccessResult")] 81 | [Trait("Category", "Fold")] 82 | public void Fold_WhenSuccess_ReturnsOnSuccessResult() 83 | { 84 | // Arrange 85 | var result = Result.Success(); 86 | Func onSuccess = () => "success"; 87 | Func onFailure = _ => "failure"; 88 | 89 | // Act 90 | var output = result.Fold(onSuccess, onFailure); 91 | 92 | // Assert 93 | output.Should().Be("success"); 94 | } 95 | 96 | [Fact(DisplayName = "Fold_WhenFailure_ReturnsOnFailureResult")] 97 | [Trait("Category", "Fold")] 98 | public void Fold_WhenFailure_ReturnsOnFailureResult() 99 | { 100 | // Arrange 101 | var exception = new Exception("test exception"); 102 | var result = Result.Fail(exception); 103 | Func onSuccess = () => "success"; 104 | Func onFailure = ex => ex.Message; 105 | 106 | // Act 107 | var output = result.Fold(onSuccess, onFailure); 108 | 109 | // Assert 110 | output.Should().Be(exception.Message); 111 | } 112 | 113 | [Fact(DisplayName = "Fold_WhenSuccess_ReturnsOnSuccessResultWithGenericTypes")] 114 | [Trait("Category", "Fold")] 115 | public void Fold_WhenSuccess_ReturnsOnSuccessResultWithGenericTypes() 116 | { 117 | // Arrange 118 | var result = Result.Success(5); 119 | Func onSuccess = value => $"success {value}"; 120 | Func onFailure = _ => "failure"; 121 | 122 | // Act 123 | var output = result.Fold(onSuccess, onFailure); 124 | 125 | // Assert 126 | output.Should().Be("success 5"); 127 | } 128 | 129 | [Fact(DisplayName = "Fold_WhenFailure_ReturnsOnFailureResultWithGenericTypes")] 130 | [Trait("Category", "Fold")] 131 | public void Fold_WhenFailure_ReturnsOnFailureResultWithGenericTypes() 132 | { 133 | // Arrange 134 | var exception = new Exception("test exception"); 135 | var result = Result.Fail(exception); 136 | Func onSuccess = value => $"success {value}"; 137 | Func onFailure = ex => ex.Message; 138 | 139 | // Act 140 | var output = result.Fold(onSuccess, onFailure); 141 | 142 | // Assert 143 | output.Should().Be(exception.Message); 144 | } 145 | 146 | [Fact(DisplayName = "Map_WhenSuccess_ReturnsSuccessResultWithTransformedValue")] 147 | [Trait("Category", "Map")] 148 | public void Map_WhenSuccess_ReturnsSuccessResultWithTransformedValue() 149 | { 150 | // Arrange 151 | var result = Result.Success(5); 152 | 153 | // Act 154 | var output = result.Map(value => $"success {value}"); 155 | 156 | // Assert 157 | output.IsSuccess.Should().BeTrue(); 158 | output.GetOrDefault().Should().Be("success 5"); 159 | } 160 | 161 | [Fact(DisplayName = "Map_WhenFailure_ReturnsFailureResult")] 162 | [Trait("Category", "Map")] 163 | public void Map_WhenFailure_ReturnsFailureResult() 164 | { 165 | // Arrange 166 | var exception = new Exception("test exception"); 167 | var result = Result.Fail(exception); 168 | 169 | // Act 170 | var output = result.Map(value => $"success {value}"); 171 | 172 | // Assert 173 | output.IsFailure.Should().BeTrue(); 174 | output.ExceptionOrNull().Should().Be(exception); 175 | } 176 | } -------------------------------------------------------------------------------- /SimpleResult/ResultExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleResult; 2 | public static class ResultExtensions 3 | { 4 | public static void ThrowOnFailure(this IResult result) 5 | { 6 | var exception = result.ExceptionOrNull(); 7 | if (exception is not null) throw exception; 8 | } 9 | public static T GetOrThrow(this IResult result) 10 | { 11 | result.ThrowOnFailure(); 12 | return result.GetOrDefault(); 13 | } 14 | public static T GetOrDefault(this IResult result, T defaultValue) => result.IsFailure ? defaultValue : result.GetOrDefault(); 15 | public static IResult OnSuccess(this IResult result, Action action) 16 | { 17 | if (result.IsSuccess) action(); 18 | return result; 19 | } 20 | public static IResult OnSuccess(this IResult result, Action action) 21 | { 22 | if (result.IsSuccess) action(result.GetOrDefault()); 23 | return result; 24 | } 25 | 26 | [Obsolete("OnFailure is deprecated due to improved error handling. Please use the new version with additional error parameter.")] 27 | public static IResult OnFailure(this IResult result, Action action) 28 | { 29 | 30 | var exception = result.ExceptionOrNull(); 31 | if (exception is not null) action(exception); 32 | return result; 33 | 34 | } 35 | public static IResult OnFailure(this IResult result, Action action) 36 | { 37 | 38 | if (result.IsFailure) action(result.ExceptionOrNull(), result.Errors); 39 | return result; 40 | 41 | } 42 | public static IResult OnFailure(this IResult result, Action> action) where TError : IError 43 | { 44 | if (!result.IsFailure) return result; 45 | if (result.HasError()) 46 | { 47 | action(result.ExceptionOrNull(), result.Errors.OfType().ToList()); 48 | } 49 | return result; 50 | 51 | } 52 | public static IResult OnErrors(this IResult result, Action> action) where TError : IError 53 | { 54 | if (!result.IsFailure) return result; 55 | if (result.HasError()) 56 | { 57 | action(result.Errors.OfType().ToList()); 58 | } 59 | return result; 60 | 61 | } 62 | public static IResult OnError(this IResult result, Action action) where TError : IError 63 | { 64 | if (!result.IsFailure) return result; 65 | var error = result.Errors.OfType().FirstOrDefault(); 66 | if (error !=null) 67 | { 68 | action(error); 69 | } 70 | return result; 71 | 72 | } 73 | public static IResult OnException(this IResult result, Action action) 74 | { 75 | var exception = result.ExceptionOrNull(); 76 | if (exception is not null) action(exception, result.Errors); 77 | return result; 78 | } 79 | public static IResult OnException(this IResult result, Action action) where T : Exception 80 | { 81 | var exception = result.ExceptionOrNull(); 82 | if (exception is T ex) action(ex, result.Errors); 83 | return result; 84 | } 85 | public static IResult OnInnerException(this IResult result, Action action) where T : Exception 86 | { 87 | var exception = result.ExceptionOrNull(); 88 | if (exception?.InnerException is T ex) action(ex, result.Errors); 89 | return result; 90 | } 91 | 92 | 93 | [Obsolete("Switch is deprecated due to improved error handling. Please use the version accepting an Errors parameter instead.")] 94 | public static void Switch(this TResult result, Action onSuccess, Action onFailure) where TResult : IResult 95 | { 96 | try 97 | { 98 | result.ThrowOnFailure(); 99 | onSuccess(); 100 | } 101 | catch (Exception e) 102 | { 103 | onFailure(e); 104 | } 105 | } 106 | public static void Switch(this TResult result, Action onSuccess, Action onFailure) where TResult : IResult 107 | { 108 | if (result.IsSuccess) 109 | { 110 | onSuccess(); 111 | } 112 | else 113 | { 114 | onFailure(result.ExceptionOrNull(),result.Errors); 115 | } 116 | } 117 | 118 | [Obsolete("Switch is deprecated due to improved error handling. Please use the version accepting an Errors parameter instead.")] 119 | public static void Switch(this IResult result, Action onSuccess, Action onFailure) 120 | { 121 | try 122 | { 123 | var resultValue = result.GetOrThrow(); 124 | onSuccess(resultValue); 125 | } 126 | catch (Exception e) 127 | { 128 | onFailure(e); 129 | } 130 | } 131 | public static void Switch(this IResult result, Action onSuccess, Action onFailure) 132 | { 133 | if (result.IsSuccess) 134 | { 135 | onSuccess(result.GetOrDefault()); 136 | } 137 | else 138 | { 139 | onFailure(result.ExceptionOrNull(),result.Errors); 140 | } 141 | } 142 | 143 | [Obsolete("Switch is deprecated due to improved error handling. Please use the version accepting an Errors parameter instead.")] 144 | public static TReturn Fold(this TResult result, Func onSuccess, Func onFailure) where TResult : IResult 145 | { 146 | return result.ExceptionOrNull() switch 147 | { 148 | null => onSuccess(), 149 | var exception => onFailure(exception) 150 | 151 | }; 152 | } 153 | public static TReturn Fold(this TResult result, Func onSuccess, Func onFailure) where TResult : IResult 154 | { 155 | return result.IsSuccess 156 | ? onSuccess() 157 | : onFailure(result.ExceptionOrNull(), result.Errors); 158 | } 159 | 160 | [Obsolete("Fold is deprecated due to improved error handling. Please use the version accepting an Errors parameter instead.")] 161 | public static TReturn Fold(this IResult result, Func onSuccess, Func onFailure) 162 | { 163 | return result.ExceptionOrNull() switch 164 | { 165 | null => onSuccess(result.GetOrDefault()), 166 | var exception => onFailure(exception) 167 | 168 | }; 169 | } 170 | public static TReturn Fold(this IResult result, Func onSuccess, Func onFailure) 171 | { 172 | return result.IsSuccess 173 | ? onSuccess(result.GetOrDefault()) 174 | : onFailure(result.ExceptionOrNull(), result.Errors); 175 | } 176 | public static Result Map(this Result result, Func transform) 177 | { 178 | return result.IsSuccess 179 | ? Result.Success(transform(result.GetOrDefault())) 180 | : Result.Fail(result.ExceptionOrNull(), result.Errors); 181 | } 182 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /SimpleResult.Tests/ResultAsyncExtentionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using System.Threading.Tasks; 3 | using System; 4 | using Xunit; 5 | 6 | namespace SimpleResult.Tests; 7 | 8 | public class ResultAsyncExtensionsTests 9 | { 10 | private static async Task SuccessAsyncOperation() 11 | { 12 | await Task.Delay(50); 13 | return "Success"; 14 | } 15 | 16 | private static async Task FailureAsyncOperation() 17 | { 18 | await Task.Delay(50); 19 | throw new InvalidOperationException("Failure"); 20 | } 21 | 22 | [Fact] 23 | [Trait("Category", "OnSuccessAsync")] 24 | public async Task OnSuccessAsync_ShouldExecuteAction_WhenResultIsSuccess() 25 | { 26 | var result = Result.Success("Success"); 27 | bool executed = false; 28 | 29 | await result.OnSuccess(async _ => 30 | { 31 | executed = true; 32 | await Task.CompletedTask; 33 | }); 34 | 35 | executed.Should().BeTrue(); 36 | } 37 | 38 | [Fact] 39 | [Trait("Category", "OnSuccessAsync")] 40 | public async Task OnSuccessAsync_ShouldNotExecuteAction_WhenResultIsFailure() 41 | { 42 | var result = Result.Fail(new InvalidOperationException("Failure")); 43 | bool executed = false; 44 | 45 | await result.OnSuccess(async _ => 46 | { 47 | executed = true; 48 | await Task.CompletedTask; 49 | }); 50 | 51 | executed.Should().BeFalse(); 52 | } 53 | 54 | [Fact] 55 | [Trait("Category", "OnFailureAsync")] 56 | public async Task OnFailureAsync_ShouldExecuteAction_WhenResultIsFailure() 57 | { 58 | var result = Result.Fail(new InvalidOperationException("Failure")); 59 | bool executed = false; 60 | 61 | await result.OnFailure(async (ex, errors) => 62 | { 63 | executed = true; 64 | await Task.CompletedTask; 65 | }); 66 | 67 | executed.Should().BeTrue(); 68 | } 69 | 70 | [Fact] 71 | [Trait("Category", "OnFailureAsync")] 72 | public async Task OnFailureAsync_ShouldNotExecuteAction_WhenResultIsSuccess() 73 | { 74 | var result = Result.Success("Success"); 75 | bool executed = false; 76 | 77 | await result.OnFailure(async (ex, errors) => 78 | { 79 | executed = true; 80 | await Task.CompletedTask; 81 | }); 82 | 83 | executed.Should().BeFalse(); 84 | } 85 | [Fact] 86 | [Trait("Category", "OnExceptionAsync")] 87 | public async Task OnExceptionAsync_ShouldExecuteAction_WhenResultHasException() 88 | { 89 | var result = Result.Fail(new InvalidOperationException("Failure")); 90 | bool executed = false; 91 | 92 | await result.OnException(async (ex, errors) => 93 | { 94 | executed = true; 95 | await Task.CompletedTask; 96 | }); 97 | 98 | executed.Should().BeTrue(); 99 | } 100 | 101 | [Fact] 102 | [Trait("Category", "OnExceptionAsync")] 103 | public async Task OnExceptionAsync_ShouldNotExecuteAction_WhenResultHasNoException() 104 | { 105 | var result = Result.Success("Success"); 106 | bool executed = false; 107 | 108 | await result.OnException(async (ex, errors) => 109 | { 110 | executed = true; 111 | await Task.CompletedTask; 112 | }); 113 | 114 | executed.Should().BeFalse(); 115 | } 116 | 117 | [Fact] 118 | [Trait("Category", "OnInnerExceptionAsync")] 119 | public async Task OnInnerExceptionAsync_ShouldExecuteAction_WhenResultHasInnerException() 120 | { 121 | var innerException = new InvalidOperationException("Inner Failure"); 122 | var outerException = new ApplicationException("Outer Failure", innerException); 123 | var result = Result.Fail(outerException); 124 | bool executed = false; 125 | 126 | await result.OnInnerException(async (ex, errors) => 127 | { 128 | executed = true; 129 | await Task.CompletedTask; 130 | }); 131 | 132 | executed.Should().BeTrue(); 133 | } 134 | 135 | [Fact] 136 | [Trait("Category", "OnInnerExceptionAsync")] 137 | public async Task OnInnerExceptionAsync_ShouldNotExecuteAction_WhenResultHasNoInnerException() 138 | { 139 | var result = Result.Fail(new InvalidOperationException("Failure")); 140 | bool executed = false; 141 | 142 | await result.OnInnerException(async (ex, errors) => 143 | { 144 | executed = true; 145 | await Task.CompletedTask; 146 | }); 147 | 148 | executed.Should().BeFalse(); 149 | } 150 | 151 | [Fact] 152 | [Trait("Category", "SwitchAsync")] 153 | public async Task SwitchAsync_ShouldExecuteOnSuccessAction_WhenResultIsSuccess() 154 | { 155 | var result = Result.Success("Success"); 156 | bool onSuccessExecuted = false; 157 | bool onFailureExecuted = false; 158 | 159 | await result.Switch( 160 | async () => 161 | { 162 | onSuccessExecuted = true; 163 | await Task.CompletedTask; 164 | }, 165 | async (ex, errors) => 166 | { 167 | onFailureExecuted = true; 168 | await Task.CompletedTask; 169 | }); 170 | 171 | onSuccessExecuted.Should().BeTrue(); 172 | onFailureExecuted.Should().BeFalse(); 173 | } 174 | 175 | [Fact] 176 | [Trait("Category", "SwitchAsync")] 177 | public async Task SwitchAsync_ShouldExecuteOnFailureAction_WhenResultIsFailure() 178 | { 179 | var result = Result.Fail(new InvalidOperationException("Failure")); 180 | bool onSuccessExecuted = false; 181 | bool onFailureExecuted = false; 182 | 183 | await result.Switch( 184 | async () => 185 | { 186 | onSuccessExecuted = true; 187 | await Task.CompletedTask; 188 | }, 189 | async (ex, errors) => 190 | { 191 | onFailureExecuted = true; 192 | await Task.CompletedTask; 193 | }); 194 | 195 | onSuccessExecuted.Should().BeFalse(); 196 | onFailureExecuted.Should().BeTrue(); 197 | } 198 | [Fact] 199 | [Trait("Category", "FoldAsync")] 200 | public async Task FoldAsync_ShouldExecuteOnSuccessFunc_WhenResultIsSuccess() 201 | { 202 | var result = Result.Success("Success"); 203 | 204 | string returnedValue = await result.Fold( 205 | async () => 206 | { 207 | await Task.CompletedTask; 208 | return "OnSuccess"; 209 | }, 210 | async (ex, errors) => 211 | { 212 | await Task.CompletedTask; 213 | return "OnFailure"; 214 | }); 215 | 216 | returnedValue.Should().Be("OnSuccess"); 217 | } 218 | 219 | [Fact] 220 | [Trait("Category", "FoldAsync")] 221 | public async Task FoldAsync_ShouldExecuteOnFailureFunc_WhenResultIsFailure() 222 | { 223 | var result = Result.Fail(new InvalidOperationException("Failure")); 224 | 225 | string returnedValue = await result.Fold( 226 | async () => 227 | { 228 | await Task.CompletedTask; 229 | return "OnSuccess"; 230 | }, 231 | async (ex, errors) => 232 | { 233 | await Task.CompletedTask; 234 | return "OnFailure"; 235 | }); 236 | 237 | returnedValue.Should().Be("OnFailure"); 238 | } 239 | 240 | [Fact] 241 | [Trait("Category", "MapAsync")] 242 | public async Task MapAsync_ShouldTransformResultValue_WhenResultIsSuccess() 243 | { 244 | var result = Result.Success(2); 245 | 246 | var newResult = await result.Map(async value => 247 | { 248 | await Task.CompletedTask; 249 | return value * 2; 250 | }); 251 | 252 | newResult.IsSuccess.Should().BeTrue(); 253 | newResult.GetOrDefault().Should().Be(4); 254 | } 255 | 256 | [Fact] 257 | [Trait("Category", "MapAsync")] 258 | public async Task MapAsync_ShouldNotTransformResultValue_WhenResultIsFailure() 259 | { 260 | var result = Result.Fail(new InvalidOperationException("Failure")); 261 | 262 | var newResult = await result.Map(async value => 263 | { 264 | await Task.CompletedTask; 265 | return value * 2; 266 | }); 267 | 268 | newResult.IsFailure.Should().BeTrue(); 269 | } 270 | } 271 | 272 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ## Simple Result 6 | [![.NET](https://github.com/araxis/SimpleResult/actions/workflows/dotnet.yml/badge.svg)](https://github.com/araxis/SimpleResult/actions/workflows/dotnet.yml) 7 | [![NuGet](https://img.shields.io/nuget/vpre/Arax.SimpleResult.svg)](https://www.nuget.org/packages/Arax.SimpleResult) 8 | [![NuGet](https://img.shields.io/nuget/dt/Arax.SimpleResult.svg)](https://www.nuget.org/packages/Arax.SimpleResult) 9 | 10 | `SimpleResult` is a lightweight library that provides a convenient way to handle the results of operations in your .NET applications. The library offers a straightforward approach to managing success and failure scenarios, making your code more readable, maintainable, and less prone to errors. 11 | 12 | [SimpleResult: A Better Alternative to Traditional Exception Handling](https://medium.com/@araxis/simpleresult-a-better-alternative-to-traditional-exception-handling-aff600919a40) 13 | 14 | ### Installing SimpleResult 15 | 16 | You should install [SimpleResult with NuGet](https://www.nuget.org/packages/Arax.SimpleResult): 17 | 18 | Install-Package Arax.SimpleResult 19 | 20 | Or via the .NET Core command line interface: 21 | 22 | dotnet add package Arax.SimpleResult 23 | 24 | 25 | The following sections demonstrate various ways to use the `SimpleResult` library for handling the results of operations. 26 | 27 | ## Make Result 28 | ```csharp 29 | var successResult = Result.Success(); 30 | var intResult = Result.Success(12); 31 | Result intResult = 12; 32 | var failureResult = Result.Fail("An error occurred"); 33 | var failureResultWithValue = Result.Fail("An error occurred"); 34 | var failureResultErors = Result.Fail("An error occurred","Another error occurred"); 35 | var failureResultException = Result.Fail(new InvalidOperationException()); 36 | var failureResultException = Result.Fail(new InvalidOperationException(),"An error occurred"); 37 | Result failure = new InvalidOperationException(); 38 | ``` 39 | 40 | ## Basic Usage 41 | ```csharp 42 | public void UseResult() 43 | { 44 | var result = GetPerson(1); 45 | var isSuccess = result.IsSuccess; 46 | var isFailure = result.IsFailure; 47 | 48 | //default value is based on result type 49 | var person = result.GetOrDefault(); 50 | //or can passed as a parameter 51 | var personWithCustomDefault = result.GetOrDefault(new Person("custom")); 52 | 53 | //if IsFailure == false => exception is null and Errors is empty 54 | var exception = result.ExceptionOrNull(); 55 | var errors = result.Errors; 56 | } 57 | ``` 58 | ## Performing an action on success 59 | Imagine you have a method that returns a result,you can use the OnSuccess method to execute an action only when the result is successful. 60 | ```csharp 61 | public void UseResult() 62 | { 63 | var result = GetPerson(1); 64 | result.OnSuccess(person => { 65 | // Perform code 66 | }); 67 | } 68 | ``` 69 | ## Handling failures and exceptions 70 | If you want to handle failures, you can use the OnFailure or OnException method to perform an action when the result is unsuccessful: 71 | ```csharp 72 | 73 | var result = DangerousOperation(); 74 | 75 | // Here exception is nullable 76 | result.OnFailure((exception, errs) => Console.WriteLine($"Exception: {exception?.Message ?? "None"}, errors: {string.Join(", ", errs)}")); 77 | 78 | // if result failed because of any type exception 79 | result.OnException((ex, errs) => 80 | { 81 | Console.WriteLine($"Exception: {ex.Message}, errors: {string.Join(", ", errs)}")); 82 | } 83 | 84 | // if result failed because of a DivideByZeroException 85 | result.OnException((ex, errors) => 86 | { 87 | Console.WriteLine($"Caught DivideByZeroException: {ex.Message}"); 88 | }); 89 | // if result failed because of a exception with InvalidOperationException inner exception 90 | result.OnInnerException((ex, errors) => 91 | { 92 | Console.WriteLine($"Caught InvalidOperationException as InnerException: {ex.Message}"); 93 | }); 94 | 95 | ``` 96 | 97 | ## Switching Between Success and Failure Actions 98 | Use the Switch method to execute different actions based on the success or failure of the result: 99 | 100 | ```csharp 101 | var result = DoSomething(); 102 | result.Switch( 103 | onSuccess: () => Console.WriteLine("Operation succeeded"), 104 | onFailure: (exception, errors) => Console.WriteLine($"Operation failed with errors: {string.Join(", ", errors)}") 105 | ); 106 | 107 | ``` 108 | For results with values, you can access the value within the Switch method: 109 | ```csharp 110 | var result = DoSomethingWithValue(); 111 | result.Switch( 112 | onSuccess: value => Console.WriteLine($"Operation succeeded with value: {value}"), 113 | onFailure: (exception, errors) => Console.WriteLine($"Operation failed with errors: {string.Join(", ", errors)}") 114 | ); 115 | ``` 116 | ## Transform the Result Based on Success or Failure 117 | The Fold method allows you to transform the result into another type, depending on whether the result is successful or not: 118 | ```csharp 119 | var result = DoSomething(); 120 | string message = result.Fold( 121 | onSuccess: () => "Success", 122 | onFailure: (exception, errors) => $"Failure: {string.Join(", ", errors)}" 123 | ); 124 | 125 | Console.WriteLine(message); 126 | ``` 127 | For results with values, you can access the value within the Fold method: 128 | ```csharp 129 | var result = DoSomethingWithValue(); 130 | string message = result.Fold( 131 | onSuccess: value => $"Success: {value}", 132 | onFailure: (exception, errors) => $"Failure: {string.Join(", ", errors)}" 133 | ); 134 | 135 | Console.WriteLine(message); 136 | ``` 137 | ## Mapping a Result to Another Type 138 | The Map method allows you to transform the value of a Result object into another type, while preserving the success or failure state of the original result. The method accepts a function that takes the current value of the result and returns a new value of a different type. 139 | ```csharp 140 | var result = Result.Success(10); 141 | 142 | // Mapping to another type 143 | var newResult = result.Map(x => x.ToString()); 144 | 145 | // Output 146 | // newResult.IsSuccess == true 147 | // newResult.GetOrDefault() == "10" 148 | 149 | var result = Result.Fail("Something went wrong"); 150 | var newResult = result.Map(x => x.ToString()); 151 | 152 | // Output 153 | // newResult.IsFailure == true 154 | // newResult.Errors == { "Something went wrong" } 155 | 156 | 157 | ``` 158 | 159 | ## Asynchronous Extensions 160 | The SimpleResult library also provides asynchronous extensions, allowing you to handle success and failure scenarios in async methods. Here's a quick overview of the available async extension methods: 161 | 162 | - OnSuccess: Perform an async action when the result is successful. 163 | - OnFailure: Perform an async action when the result is unsuccessful. 164 | - OnFailure: Perform an async action when the result is unsuccessful and contains a specific error type. 165 | - OnException: Perform an async action when the result contains an exception. 166 | - OnException: Perform an async action when the result contains a specific exception type. 167 | - OnInnerException: Perform an async action when the result contains an inner exception of a specific type. 168 | - Switch: Execute different async actions based on the success or failure of the result. 169 | - Fold: Transform the result into another type using async actions based on the success or failure of the result. 170 | - Map: Map a result to another type using an async transformation function. 171 | 172 | Here's an example of using the OnSuccess async extension method: 173 | ```csharp 174 | public async Task UseResultAsync() 175 | { 176 | var result = await FetchPersonAsync(1); 177 | await result.OnSuccess(async person => 178 | { 179 | await DoSomethingAsync(person); 180 | }); 181 | } 182 | ``` 183 | And here's an example of using the OnFailure async extension method with a specific error type: 184 | ```csharp 185 | public async Task UseResultAsync() 186 | { 187 | var result = await DangerousOperationAsync(); 188 | await result.OnFailure(async (exception, errors) => 189 | { 190 | Console.WriteLine($"Caught CustomError: {string.Join(", ", errors)}"); 191 | await HandleCustomErrorAsync(errors); 192 | }); 193 | } 194 | ``` 195 | For more detailed examples and usage information related to asynchronous operations, please refer to the sections on [Performing Actions on Success](#performing-an-action-on-success), [Handling Failures and Exceptions](#handling-failures-and-exceptions), [Switching Between Success and Failure Actions](#switching-between-success-and-failure-actions), [Transform the Result Based on Success or Failure](#transform-the-result-based-on-success-or-failure), and [Mapping a Result to Another Type](#mapping-a-result-to-another-type) in this README. The async extension methods provided by the `ResultAsyncExtensions` class work similarly to their synchronous counterparts but support async actions and transformations for seamless integration with asynchronous operations in your code. 196 | 197 | ## Try-Catch 198 | ```csharp 199 | 200 | public void UseResult() 201 | { 202 | var result = GetPerson(1); 203 | try 204 | { 205 | var person = result.GetOrThrow(); 206 | //use person 207 | } 208 | catch (Exception e) 209 | { 210 | //catch exception 211 | } 212 | } 213 | ``` 214 | -------------------------------------------------------------------------------- /SimpleResult.Tests/HandleFailureTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using FluentAssertions; 5 | using Xunit; 6 | 7 | namespace SimpleResult.Tests; 8 | 9 | public class HandleFailureTests 10 | { 11 | 12 | [Fact] 13 | [Trait("Category", "OnFailure")] 14 | public void Should_InvokeAction_When_ResultIsFailureWithException() 15 | { 16 | // Arrange 17 | var exception = new InvalidOperationException("Test exception"); 18 | var result = Result.Fail(exception); 19 | var actionCalled = false; 20 | 21 | // Act 22 | result.OnFailure((ex, errs) => 23 | { 24 | actionCalled = true; 25 | ex.Should().Be(exception); 26 | errs.Should().BeEmpty(); 27 | }); 28 | 29 | // Assert 30 | actionCalled.Should().BeTrue(); 31 | } 32 | 33 | [Fact] 34 | [Trait("Category", "OnFailure")] 35 | public void Should_InvokeAction_When_ResultIsFailureWithoutException() 36 | { 37 | // Arrange 38 | var error = new Error("Test error"); 39 | var result = Result.Fail(error); 40 | var actionCalled = false; 41 | 42 | // Act 43 | result.OnFailure((ex, errs) => 44 | { 45 | actionCalled = true; 46 | ex.Should().BeNull(); 47 | errs.Should().HaveCount(1); 48 | errs.First().Should().Be(error); 49 | }); 50 | 51 | // Assert 52 | actionCalled.Should().BeTrue(); 53 | } 54 | 55 | [Fact] 56 | [Trait("Category", "OnFailure")] 57 | public void Should_InvokeAction_When_ResultIsFailureWithExceptionAndErrors() 58 | { 59 | // Arrange 60 | var error = new Error("Test error"); 61 | var exception = new InvalidOperationException("Test exception"); 62 | var result = Result.Fail(exception, error); 63 | var actionCalled = false; 64 | 65 | // Act 66 | result.OnFailure((ex, errs) => 67 | { 68 | actionCalled = true; 69 | ex.Should().Be(exception); 70 | errs.Should().HaveCount(1); 71 | errs.First().Should().Be(error); 72 | }); 73 | 74 | // Assert 75 | actionCalled.Should().BeTrue(); 76 | } 77 | 78 | [Fact] 79 | [Trait("Category", "OnFailure")] 80 | public void Should_InvokeAction_When_ResultIsFailureWithNoExceptionAndNoErrors() 81 | { 82 | // Arrange 83 | var result = Result.Fail(Array.Empty()); 84 | var actionCalled = false; 85 | 86 | // Act 87 | result.OnFailure((ex, errs) => 88 | { 89 | actionCalled = true; 90 | ex.Should().BeNull(); 91 | errs.Should().BeEmpty(); 92 | }); 93 | 94 | // Assert 95 | actionCalled.Should().BeTrue(); 96 | } 97 | 98 | [Fact] 99 | [Trait("Category", "OnFailure")] 100 | public void Should_NotInvokeAction_When_ResultIsSuccess() 101 | { 102 | // Arrange 103 | var result = Result.Success(); 104 | var actionCalled = false; 105 | 106 | // Act 107 | result.OnFailure((ex, errs) => 108 | { 109 | actionCalled = true; 110 | }); 111 | 112 | // Assert 113 | actionCalled.Should().BeFalse(); 114 | } 115 | 116 | [Fact] 117 | [Trait("Category", "OnFailure")] 118 | public void OnFailure_TError_InvokesAction_When_ResultIsFailureWithExceptionAndSpecifiedErrorType() 119 | { 120 | // Arrange 121 | var exception = new InvalidOperationException("Test exception"); 122 | var error = new CustomError("Test error"); 123 | var result = Result.Fail(exception, error); 124 | var actionCalled = false; 125 | Exception? capturedException = null; 126 | IReadOnlyCollection capturedErrors = new List(); 127 | 128 | // Act 129 | result.OnFailure((ex, errs) => 130 | { 131 | actionCalled = true; 132 | capturedException = ex; 133 | capturedErrors = errs; 134 | }); 135 | 136 | // Assert 137 | actionCalled.Should().BeTrue(); 138 | capturedException.Should().Be(exception); 139 | capturedErrors.Should().HaveCount(1); 140 | capturedErrors.First().Should().Be(error); 141 | } 142 | 143 | [Fact] 144 | [Trait("Category", "OnFailure")] 145 | public void OnFailure_TError_InvokesAction_When_ResultIsFailureWithErrorsOfDifferentTypes() 146 | { 147 | // Arrange 148 | var customError = new CustomError("Test custom error"); 149 | var anotherError = new Error("Test another error"); 150 | var result = Result.Fail(customError, anotherError); 151 | var actionCalled = false; 152 | Exception? capturedException = null; 153 | IReadOnlyCollection capturedErrors = new List(); 154 | 155 | // Act 156 | result.OnFailure((ex, errs) => 157 | { 158 | actionCalled = true; 159 | capturedException = ex; 160 | capturedErrors = errs; 161 | }); 162 | 163 | // Assert 164 | actionCalled.Should().BeTrue(); 165 | capturedException.Should().BeNull(); 166 | capturedErrors.Should().HaveCount(1); 167 | capturedErrors.First().Should().Be(customError); 168 | } 169 | 170 | [Fact] 171 | [Trait("Category", "OnFailure")] 172 | public void OnFailure_TError_DoesNotInvokeAction_When_ResultIsFailureWithExceptionAndWithoutSpecifiedErrorType() 173 | { 174 | // Arrange 175 | var exception = new InvalidOperationException("Test exception"); 176 | var result = Result.Fail(exception); 177 | var actionCalled = false; 178 | 179 | // Act 180 | result.OnFailure((ex, errs) => 181 | { 182 | actionCalled = true; 183 | }); 184 | 185 | // Assert 186 | actionCalled.Should().BeFalse(); 187 | } 188 | 189 | [Fact] 190 | [Trait("Category", "OnFailure")] 191 | public void OnFailure_TError_DoesNotInvokeAction_When_ResultIsSuccess() 192 | { 193 | // Arrange 194 | var result = Result.Success(); 195 | var actionCalled = false; 196 | 197 | // Act 198 | result.OnFailure((ex, errs) => 199 | { 200 | actionCalled = true; 201 | }); 202 | 203 | // Assert 204 | actionCalled.Should().BeFalse(); 205 | } 206 | 207 | [Fact] 208 | [Trait("Category", "OnException")] 209 | public void OnException_InvokesAction_When_ResultIsFailureWithException() 210 | { 211 | // Arrange 212 | var exception = new InvalidOperationException("Test exception"); 213 | var result = Result.Fail(exception); 214 | var actionCalled = false; 215 | Exception? capturedException = null; 216 | IReadOnlyCollection capturedErrors = new List(); 217 | 218 | // Act 219 | result.OnException((ex, errs) => 220 | { 221 | actionCalled = true; 222 | capturedException = ex; 223 | capturedErrors = errs; 224 | }); 225 | 226 | // Assert 227 | actionCalled.Should().BeTrue(); 228 | capturedException.Should().Be(exception); 229 | capturedErrors.Should().BeEmpty(); 230 | } 231 | 232 | [Fact] 233 | [Trait("Category", "OnException")] 234 | public void OnException_DoesNotInvokeAction_When_ResultIsFailureWithoutException() 235 | { 236 | // Arrange 237 | var error = new CustomError("Test error"); 238 | var result = Result.Fail(error); 239 | var actionCalled = false; 240 | 241 | // Act 242 | result.OnException((ex, errs) => 243 | { 244 | actionCalled = true; 245 | }); 246 | 247 | // Assert 248 | actionCalled.Should().BeFalse(); 249 | } 250 | 251 | [Fact] 252 | [Trait("Category", "OnException")] 253 | public void OnException_DoesNotInvokeAction_When_ResultIsSuccess() 254 | { 255 | // Arrange 256 | var result = Result.Success(); 257 | var actionCalled = false; 258 | 259 | // Act 260 | result.OnException((ex, errs) => 261 | { 262 | actionCalled = true; 263 | }); 264 | 265 | // Assert 266 | actionCalled.Should().BeFalse(); 267 | } 268 | 269 | 270 | [Fact] 271 | [Trait("Category", "OnExceptionTyped")] 272 | public void OnExceptionTyped_InvokesAction_When_ResultIsFailureWithMatchingExceptionType() 273 | { 274 | // Arrange 275 | var exception = new InvalidOperationException("Test exception"); 276 | var result = Result.Fail(exception); 277 | var actionCalled = false; 278 | Exception? capturedException = null; 279 | IReadOnlyCollection capturedErrors = new List(); 280 | 281 | // Act 282 | result.OnException((ex, errs) => 283 | { 284 | actionCalled = true; 285 | capturedException = ex; 286 | capturedErrors = errs; 287 | }); 288 | 289 | // Assert 290 | actionCalled.Should().BeTrue(); 291 | capturedException.Should().Be(exception); 292 | capturedErrors.Should().BeEmpty(); 293 | } 294 | 295 | [Fact] 296 | [Trait("Category", "OnExceptionTyped")] 297 | public void OnExceptionTyped_DoesNotInvokeAction_When_ResultIsFailureWithNonMatchingExceptionType() 298 | { 299 | // Arrange 300 | var exception = new InvalidOperationException("Test exception"); 301 | var result = Result.Fail(exception); 302 | var actionCalled = false; 303 | 304 | // Act 305 | result.OnException((ex, errs) => 306 | { 307 | actionCalled = true; 308 | }); 309 | 310 | // Assert 311 | actionCalled.Should().BeFalse(); 312 | } 313 | 314 | [Fact] 315 | [Trait("Category", "OnExceptionTyped")] 316 | public void OnExceptionTyped_DoesNotInvokeAction_When_ResultIsFailureWithoutException() 317 | { 318 | // Arrange 319 | var error = new CustomError("Test error"); 320 | var result = Result.Fail(error); 321 | var actionCalled = false; 322 | 323 | // Act 324 | result.OnException((ex, errs) => 325 | { 326 | actionCalled = true; 327 | }); 328 | 329 | // Assert 330 | actionCalled.Should().BeFalse(); 331 | } 332 | 333 | [Fact] 334 | [Trait("Category", "OnExceptionTyped")] 335 | public void OnExceptionTyped_DoesNotInvokeAction_When_ResultIsSuccess() 336 | { 337 | // Arrange 338 | var result = Result.Success(); 339 | var actionCalled = false; 340 | 341 | // Act 342 | result.OnException((ex, errs) => 343 | { 344 | actionCalled = true; 345 | }); 346 | 347 | // Assert 348 | actionCalled.Should().BeFalse(); 349 | } 350 | 351 | 352 | [Trait("Category", "OnInnerException")] 353 | [Fact] 354 | public void OnInnerException_InvokesAction_When_ResultIsFailureWithMatchingInnerExceptionType() 355 | { 356 | // Arrange 357 | var innerException = new InvalidOperationException("Inner exception"); 358 | var exception = new Exception("Test exception", innerException); 359 | var result = Result.Fail(exception); 360 | var actionCalled = false; 361 | Exception? capturedException = null; 362 | IReadOnlyCollection capturedErrors = new List(); 363 | 364 | // Act 365 | result.OnInnerException((ex, errs) => 366 | { 367 | actionCalled = true; 368 | capturedException = ex; 369 | capturedErrors = errs; 370 | }); 371 | 372 | // Assert 373 | actionCalled.Should().BeTrue(); 374 | capturedException.Should().Be(innerException); 375 | capturedErrors.Should().BeEmpty(); 376 | } 377 | 378 | [Trait("Category", "OnInnerException")] 379 | [Fact] 380 | public void OnInnerException_DoesNotInvokeAction_When_ResultIsFailureWithNonMatchingInnerExceptionType() 381 | { 382 | // Arrange 383 | var innerException = new InvalidOperationException("Inner exception"); 384 | var exception = new Exception("Test exception", innerException); 385 | var result = Result.Fail(exception); 386 | var actionCalled = false; 387 | 388 | // Act 389 | result.OnInnerException((ex, errs) => 390 | { 391 | actionCalled = true; 392 | }); 393 | 394 | // Assert 395 | actionCalled.Should().BeFalse(); 396 | } 397 | 398 | [Trait("Category", "OnInnerException")] 399 | [Fact] 400 | public void OnInnerException_DoesNotInvokeAction_When_ResultIsFailureWithoutInnerException() 401 | { 402 | // Arrange 403 | var exception = new InvalidOperationException("Test exception"); 404 | var result = Result.Fail(exception); 405 | var actionCalled = false; 406 | 407 | // Act 408 | result.OnInnerException((ex, errs) => 409 | { 410 | actionCalled = true; 411 | }); 412 | 413 | // Assert 414 | actionCalled.Should().BeFalse(); 415 | } 416 | 417 | [Trait("Category", "OnInnerException")] 418 | [Fact] 419 | public void OnInnerException_DoesNotInvokeAction_When_ResultIsSuccess() 420 | { 421 | // Arrange 422 | var result = Result.Success(); 423 | var actionCalled = false; 424 | 425 | // Act 426 | result.OnInnerException((ex, errs) => 427 | { 428 | actionCalled = true; 429 | }); 430 | 431 | // Assert 432 | actionCalled.Should().BeFalse(); 433 | } 434 | } --------------------------------------------------------------------------------