Skip to content

Commit

Permalink
Fixed rethrowing exceptions without preserving the original stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-berg committed Dec 2, 2020
1 parent 9de13f2 commit d09abae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Appeaser.Tests/MediatorSettingsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public void Test_Query_Exception_Wrapping_Can_Be_Disabled()
Assert.Throws<Exception>(() => mediator.Request(new TestFeature.Query()));
}

[Fact]
public void Test_Query_Exception_Preserves_Stacktrace()
{
var mediator = new Mediator(_handlerFactory, new TestMediatorSettings { WrapExceptions = false });
var exception = Assert.Throws<Exception>(() => mediator.Request(new TestFeature.Query()));
Assert.Equal(typeof(TestFeature.Handler), exception.TargetSite.DeclaringType);
}

[Fact]
public void Test_Command_Exception_Wrapping_Can_Be_Disabled()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Appeaser/Appeaser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>netstandard2.0;netstandard1.0;net45</TargetFrameworks>
<Authors>Carl Berg</Authors>
<Copyright>Copyright (c) $([System.DateTime]::Now.Year) Carl Berg</Copyright>
<VersionPrefix>2.3.2</VersionPrefix>
<VersionPrefix>2.3.3</VersionPrefix>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' == 'net45' ">true</DisableImplicitFrameworkReferences>
<Description>Appeaser, the appeasing mediator</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
7 changes: 5 additions & 2 deletions src/Appeaser/Interception/MediatorInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

namespace Appeaser.Interception
Expand Down Expand Up @@ -42,7 +43,8 @@ public TResponse InvokeHandler<TResponse>(object handler, object parameter, Func
interceptor.Intercept(scope.CreateExceptionInterceptionContext<TResponse>(exception));
}

throw exception;
ExceptionDispatchInfo.Capture(exception).Throw();
throw new Exception("Should never happen, hopefully? :-)");
}
}

Expand Down Expand Up @@ -73,7 +75,8 @@ public async Task<TResponse> InvokeHandlerAsync<TResponse>(object handler, objec
await interceptor.InterceptAsync(scope.CreateExceptionInterceptionContext<TResponse>(exception));
}

throw exception;
ExceptionDispatchInfo.Capture(exception).Throw();
throw new Exception("Should never happen, hopefully? :-)");
}
}

Expand Down

0 comments on commit d09abae

Please sign in to comment.