diff --git a/ChainSharp.Tests.Integration/IntegrationTests/WorkflowTests.cs b/ChainSharp.Tests.Integration/IntegrationTests/WorkflowTests.cs index 1d395d6..f1072b3 100644 --- a/ChainSharp.Tests.Integration/IntegrationTests/WorkflowTests.cs +++ b/ChainSharp.Tests.Integration/IntegrationTests/WorkflowTests.cs @@ -1,3 +1,4 @@ +using ChainSharp.Exceptions; using ChainSharp.Step; using ChainSharp.Tests.Examples.Brewery; using ChainSharp.Tests.Examples.Brewery.Steps.Bottle; @@ -319,6 +320,24 @@ public async Task TestChainWithShortCircuitStaysLeft() result.Should().NotBeNull(); } + [Theory] + public async Task TestWithException() + { + // Arrange + var workflow = new ChainTestWithException(); + + // Act + // Assert + + Assert.ThrowsAsync(async () => await workflow.Run(Unit.Default)); + } + + private class ThrowsStep : Step + { + public override Task Run(Unit input) => + throw new WorkflowException("This is a workflow exception."); + } + private class OuterProperty { public string OuterString { get; set; } @@ -616,4 +635,10 @@ Ingredients input .Resolve(); } } + + private class ChainTestWithException : Workflow + { + protected override async Task> RunInternal(Unit input) => + Activate(input).Chain().Resolve(); + } } diff --git a/ChainSharp/Step/Step.cs b/ChainSharp/Step/Step.cs index 4c0cb2f..1b249c3 100644 --- a/ChainSharp/Step/Step.cs +++ b/ChainSharp/Step/Step.cs @@ -1,3 +1,4 @@ +using System.Reflection; using LanguageExt; using LanguageExt.UnsafeValueAccess; @@ -20,8 +21,19 @@ public async Task> RailwayStep(Either pr } catch (Exception e) { - Console.WriteLine($"Found Exception ({e.InnerException ?? e})"); - return e.InnerException ?? e; + var messageField = typeof(Exception).GetField( + "_message", + BindingFlags.Instance | BindingFlags.NonPublic + ); + + if (messageField != null) + messageField.SetValue( + e, + $"{{ \"Step\": \"{GetType().Name}\", \"Type\": \"{e.GetType().Name}\", \"Message\": \"{e.Message}\" }}" + ); + + Console.WriteLine($"Step: ({GetType().Name}) failed with Exception: ({e.Message})"); + return e; } } } diff --git a/ChainSharp/Workflow/Workflow.cs b/ChainSharp/Workflow/Workflow.cs index 7054c83..6d1c14e 100644 --- a/ChainSharp/Workflow/Workflow.cs +++ b/ChainSharp/Workflow/Workflow.cs @@ -1,5 +1,6 @@ using ChainSharp.Extensions; using LanguageExt; +using LanguageExt.UnsafeValueAccess; namespace ChainSharp.Workflow; @@ -12,7 +13,15 @@ public abstract partial class Workflow : IWorkflow Run(TInput input) => await RunEither(input).Unwrap(); + public async Task Run(TInput input) + { + var resultEither = await RunEither(input); + + if (resultEither.IsLeft) + resultEither.Swap().ValueUnsafe().Rethrow(); + + return resultEither.Unwrap(); + } public Task> RunEither(TInput input) { diff --git a/Directory.Build.props b/Directory.Build.props index a78684f..2450a83 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 2.1.5 + 2.2.0