Skip to content

Commit 48ef555

Browse files
committed
fix: improve exception handling with ExceptionDispatchInfo
- Updated PipelineBuilder and QueryProcessor to use `ExceptionDispatchInfo.Capture` for correct stack trace preservation. - Added compiler-required `throw` statements to unreachable code paths.
1 parent f8b1fe0 commit 48ef555

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/Paramore.Darker/PipelineBuilder.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Paramore.Darker.Attributes;
99
using Paramore.Darker.Exceptions;
1010
using Paramore.Darker.Logging;
11+
using System.Runtime.ExceptionServices; // ✨ new
1112

1213
namespace Paramore.Darker
1314
{
@@ -53,10 +54,10 @@ public Func<IQuery<TResult>, TResult> Build(IQuery<TResult> query, IQueryContext
5354
{
5455
return (TResult)executeMethodInfo.Invoke(_handler, new object[] { r });
5556
}
56-
catch (TargetInvocationException targetInvocationException)
57+
catch (TargetInvocationException ex) when (ex.InnerException != null)
5758
{
58-
// Unwrap the original exception instead of wrapping it in a FormatException
59-
throw targetInvocationException.InnerException;
59+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
60+
throw; // never reached
6061
}
6162
}
6263
};
@@ -97,10 +98,10 @@ public Func<IQuery<TResult>, CancellationToken, Task<TResult>> BuildAsync(IQuery
9798
{
9899
return (Task<TResult>)executeAsyncMethodInfo.Invoke(_handler, new object[] { r, ct });
99100
}
100-
catch (TargetInvocationException targetInvocationException)
101+
catch (TargetInvocationException ex) when (ex.InnerException != null)
101102
{
102-
// Unwrap the original exception instead of wrapping it in a FormatException
103-
throw targetInvocationException.InnerException;
103+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
104+
throw; // never reached for complier
104105
}
105106
}
106107
};

src/Paramore.Darker/QueryProcessor.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.Extensions.Logging;
88
using Paramore.Darker.Logging;
9+
using System.Runtime.ExceptionServices;
910

1011
namespace Paramore.Darker
1112
{
@@ -46,9 +47,10 @@ public TResult Execute<TResult>(IQuery<TResult> query)
4647
{
4748
return entryPoint.Invoke(query);
4849
}
49-
catch (TargetInvocationException targetInvocationException)
50+
catch (TargetInvocationException ex) when (ex.InnerException != null)
5051
{
51-
throw targetInvocationException.InnerException;
52+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
53+
throw; // never reached, but required by compiler
5254
}
5355
catch (Exception ex)
5456
{
@@ -70,9 +72,10 @@ public TResult Execute<TResult>(IQuery<TResult> query)
7072
_logger.LogDebug("Invoking async pipeline...");
7173
return await entryPoint.Invoke(query, cancellationToken).ConfigureAwait(false);
7274
}
73-
catch (TargetInvocationException targetInvocationException)
75+
catch (TargetInvocationException ex) when (ex.InnerException != null)
7476
{
75-
throw targetInvocationException.InnerException;
77+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
78+
throw; // never reached, but required by compiler
7679
}
7780
catch (Exception ex)
7881
{

0 commit comments

Comments
 (0)