Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;

namespace Dora.DynamicProxy
{
Expand All @@ -16,15 +17,25 @@ public static class TargetInvoker
/// <returns>The task to invoke interceptor and target method.</returns>
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)
{
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;
}
}
}
}
29 changes: 29 additions & 0 deletions src/Dora.DynamicProxy/Dora.DynamicProxy/InterceptorException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Dora.DynamicProxy
{
/// <summary>
/// Represents an InterceptorException.
/// </summary>
public class InterceptorException : Exception
{
/// <summary>
///
/// </summary>
public bool RequireThrow { get; }

/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="innerException"></param>
/// <param name="requireThrow"></param>
public InterceptorException(string message, Exception innerException, bool requireThrow = true)
: base(message, innerException)
{
RequireThrow = requireThrow;
}
}
}