< Summary

Information
Class: Elsa.Common.Models.Result
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Models/Result.cs
Line coverage
66%
Covered lines: 2
Uncovered lines: 1
Coverable lines: 3
Total lines: 106
Line coverage: 66.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Success(...)100%11100%
Failure(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Models/Result.cs

#LineLine coverage
 1namespace Elsa.Common.Models;
 2
 3/// <summary>
 4/// A strongly typed monad that runs either the <see cref="OnSuccess"/> or <see cref="OnFailure"/> lambda, depending on 
 5/// </summary>
 6public class Result<T>(bool success, T? value, Exception? exception)
 7{
 8    /// <summary>
 9    /// True if the conversion succeeded, false otherwise.
 10    /// </summary>
 11    public bool IsSuccess { get; } = success;
 12
 13    /// <summary>
 14    /// The result value. Throws an exception if accessed when the result is a failure.
 15    /// </summary>
 16    public T Value
 17    {
 18        get
 19        {
 20            if (!IsSuccess)
 21                throw new InvalidOperationException("Cannot access Value on a failed result. Check IsSuccess first or us
 22            return value!;
 23        }
 24    }
 25
 26    /// <summary>
 27    /// The result value, or null/default if the result is a failure. Useful when null is a valid success value.
 28    /// </summary>
 29    public T? ValueOrDefault => value;
 30
 31    /// <summary>
 32    /// Any exception that may have occurred during the operation.
 33    /// </summary>
 34    public Exception? Exception { get; } = exception;
 35
 36    /// <summary>
 37    /// Runs the provided delegate if the result is successful.
 38    /// </summary>
 39    public Result<T> OnSuccess(Action<T> successHandler)
 40    {
 41        if (IsSuccess)
 42            successHandler(Value);
 43
 44        return this;
 45    }
 46
 47    /// <summary>
 48    /// Runs the provided async delegate if the result is successful.
 49    /// </summary>
 50    public async Task<Result<T>> OnSuccessAsync(Func<T, Task> successHandler)
 51    {
 52        if (IsSuccess)
 53            await successHandler(Value);
 54
 55        return this;
 56    }
 57
 58    /// <summary>
 59    /// Runs the provided delegate if the result is unsuccessful.
 60    /// </summary>
 61    public Result<T> OnFailure(Action<Exception> failureHandler)
 62    {
 63        if (Exception != null)
 64            failureHandler(Exception);
 65
 66        return this;
 67    }
 68
 69    /// <summary>
 70    /// Runs the provided async delegate if the result is unsuccessful.
 71    /// </summary>
 72    public async Task<Result<T>> OnFailureAsync(Func<Exception, Task> failureHandler)
 73    {
 74        if (Exception != null)
 75            await failureHandler(Exception);
 76
 77        return this;
 78    }
 79
 80    /// <summary>
 81    /// Throws the exception if the result is a failure.
 82    /// </summary>
 83    public Result<T> ThrowIfFailure()
 84    {
 85        if (!IsSuccess && Exception != null)
 86            throw Exception;
 87
 88        return this;
 89    }
 90}
 91
 92/// <summary>
 93/// A simple monad that runs either the <see cref="Result{T}.OnSuccess"/> or <see cref="Result{T}.OnFailure"/> lambda, d
 94/// </summary>
 2068395public class Result(bool success, object? value, Exception? exception) : Result<object>(success, value, exception)
 96{
 97    /// <summary>
 98    /// Creates a successful result with the specified value.
 99    /// </summary>
 2688100    public static Result<T> Success<T>(T value) => new(true, value, null);
 101
 102    /// <summary>
 103    /// Creates a failed result with the specified exception.
 104    /// </summary>
 0105    public static Result<T> Failure<T>(Exception exception) => new(false, default, exception);
 106}