From 1a02aee1b0fa9979cda14b94879a75f3f9b6e06c Mon Sep 17 00:00:00 2001 From: huazhikui Date: Wed, 27 Mar 2019 22:52:47 +0800 Subject: [PATCH] Add InterceptorException --- .../TargetInvoker.cs | 19 +++++++++--- .../Dora.DynamicProxy/InterceptorException.cs | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/Dora.DynamicProxy/Dora.DynamicProxy/InterceptorException.cs diff --git a/src/Dora.DynamicProxy/Dora.DynamicProxy/DynamicProxyClassGenerator/TargetInvoker.cs b/src/Dora.DynamicProxy/Dora.DynamicProxy/DynamicProxyClassGenerator/TargetInvoker.cs index f8f3ae5..cba5277 100644 --- a/src/Dora.DynamicProxy/Dora.DynamicProxy/DynamicProxyClassGenerator/TargetInvoker.cs +++ b/src/Dora.DynamicProxy/Dora.DynamicProxy/DynamicProxyClassGenerator/TargetInvoker.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; namespace Dora.DynamicProxy { @@ -16,7 +17,8 @@ public static class TargetInvoker /// The task to invoke interceptor and target method. public static Task InvokeHandler(InterceptorDelegate interceptor, InterceptDelegate handler, InvocationContext context) { - InterceptDelegate wrapper = async _ => { + InterceptDelegate wrapper = async _ => + { await handler(_); var task = _.ReturnValue as Task; if (null != task) @@ -24,7 +26,16 @@ public static Task InvokeHandler(InterceptorDelegate interceptor, InterceptDeleg await task; } }; - return interceptor(wrapper)(context); + + var resultTask = interceptor(wrapper)(context); + // throw inner InterceptorException + var agregateException = resultTask.Exception; + if (agregateException != null && + agregateException.InnerExceptions.Any(ex => (ex is InterceptorException) && (ex as InterceptorException).RequireThrow)) + { + throw resultTask.Exception; + } + return resultTask; } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/Dora.DynamicProxy/Dora.DynamicProxy/InterceptorException.cs b/src/Dora.DynamicProxy/Dora.DynamicProxy/InterceptorException.cs new file mode 100644 index 0000000..cbdc165 --- /dev/null +++ b/src/Dora.DynamicProxy/Dora.DynamicProxy/InterceptorException.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Dora.DynamicProxy +{ + /// + /// Represents an InterceptorException. + /// + public class InterceptorException : Exception + { + /// + /// + /// + public bool RequireThrow { get; } + + /// + /// + /// + /// + /// + /// + public InterceptorException(string message, Exception innerException, bool requireThrow = true) + : base(message, innerException) + { + RequireThrow = requireThrow; + } + } +}