Skip to content

Commit

Permalink
Unwrap original exception when dispatching entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jviau committed Nov 1, 2023
1 parent 25f4220 commit 760cb8c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Abstractions/Entities/TaskEntityOperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Reflection;
using System.Runtime.ExceptionServices;

namespace Microsoft.DurableTask.Entities;

Expand Down Expand Up @@ -74,9 +75,18 @@ internal static bool TryDispatch(
i++;
}

result = method.Invoke(target, inputs);
returnType = method.ReturnType;
return true;
try
{
result = method.Invoke(target, inputs);
returnType = method.ReturnType;
return true;
}
catch (TargetInvocationException ex)
{
// Re-throw the inner exception so that the stack trace is preserved.
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
throw; // Unreachable.
}

static void ThrowIfDuplicateBinding(
ParameterInfo? existing, ParameterInfo parameter, string bindingConcept, TaskEntityOperation operation)
Expand Down
21 changes: 21 additions & 0 deletions test/Abstractions.Tests/Entities/EntityTaskEntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Reflection;
using DotNext;
using FluentAssertions.Specialized;

namespace Microsoft.DurableTask.Entities.Tests;

Expand Down Expand Up @@ -153,6 +154,21 @@ public async Task ExplicitDelete_Overridden(string op)
operation.State.GetState(typeof(int)).Should().Be(0);
}

[Fact]
public async Task Throws_ExceptionPreserved()
{
string error = "this message should be preserved";
TestEntityOperation operation = new("throws", error);
TestEntity entity = new();

Func<Task> act = async () => await entity.RunAsync(operation);

ExceptionAssertions<InvalidOperationException> ex = await act.Should()
.ThrowAsync<InvalidOperationException>()
.WithMessage(error)
.Where(x => x.StackTrace!.StartsWith(" at Microsoft.DurableTask.Entities.Tests.EntityTaskEntityTests.TestEntity.Throws(String message)"));
}

#pragma warning disable CA1822 // Mark members as static
#pragma warning disable IDE0060 // Remove unused parameter
class TestEntity : TaskEntity<int>
Expand Down Expand Up @@ -222,6 +238,11 @@ static async Task<string> Slow()
return sync ? new("success") : new(Slow());
}

public void Throws(string message)
{
throw new InvalidOperationException(message);
}

int Add(int? value, Optional<TaskEntityContext> context)
{
if (context.HasValue)
Expand Down

0 comments on commit 760cb8c

Please sign in to comment.