Skip to content

Commit

Permalink
feature: Stack Trace Updates (#43)
Browse files Browse the repository at this point in the history
* Added functionality for Exceptions to Rethrow, such that their stack traces do not get eaten by Unwrap().

* CSharpier
  • Loading branch information
Theauxm authored Oct 14, 2024
1 parent 2e2d374 commit 71fe116
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
25 changes: 25 additions & 0 deletions ChainSharp.Tests.Integration/IntegrationTests/WorkflowTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ChainSharp.Exceptions;
using ChainSharp.Step;
using ChainSharp.Tests.Examples.Brewery;
using ChainSharp.Tests.Examples.Brewery.Steps.Bottle;
Expand Down Expand Up @@ -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<WorkflowException>(async () => await workflow.Run(Unit.Default));
}

private class ThrowsStep : Step<Unit, Unit>
{
public override Task<Unit> Run(Unit input) =>
throw new WorkflowException("This is a workflow exception.");
}

private class OuterProperty
{
public string OuterString { get; set; }
Expand Down Expand Up @@ -616,4 +635,10 @@ Ingredients input
.Resolve();
}
}

private class ChainTestWithException : Workflow<Unit, Unit>
{
protected override async Task<Either<Exception, Unit>> RunInternal(Unit input) =>
Activate(input).Chain<ThrowsStep>().Resolve();
}
}
16 changes: 14 additions & 2 deletions ChainSharp/Step/Step.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using LanguageExt;
using LanguageExt.UnsafeValueAccess;

Expand All @@ -20,8 +21,19 @@ public async Task<Either<Exception, TOut>> RailwayStep(Either<Exception, TIn> 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;
}
}
}
11 changes: 10 additions & 1 deletion ChainSharp/Workflow/Workflow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ChainSharp.Extensions;
using LanguageExt;
using LanguageExt.UnsafeValueAccess;

namespace ChainSharp.Workflow;

Expand All @@ -12,7 +13,15 @@ public abstract partial class Workflow<TInput, TReturn> : IWorkflow<TInput, TRet
private TReturn ShortCircuitValue { get; set; } = default!;
private bool ShortCircuitValueSet { get; set; } = false;

public async Task<TReturn> Run(TInput input) => await RunEither(input).Unwrap();
public async Task<TReturn> Run(TInput input)
{
var resultEither = await RunEither(input);

if (resultEither.IsLeft)
resultEither.Swap().ValueUnsafe().Rethrow();

return resultEither.Unwrap();
}

public Task<Either<Exception, TReturn>> RunEither(TInput input)
{
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>2.1.5</Version>
<Version>2.2.0</Version>
</PropertyGroup>
</Project>

0 comments on commit 71fe116

Please sign in to comment.