Skip to content

Commit 6eac522

Browse files
committed
Added ErrorHandlingInterceptor
1 parent eb8cb48 commit 6eac522

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Added
10+
- Implemented an abstract `ErrorHandlingInterceptor`
11+
712
## [3.3.0] - 2017-09-26
813

914
### Added

src/Ninject.Extensions.Interception/AsyncInterceptor.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// </copyright>
77
// -------------------------------------------------------------------------------------------------
88

9-
#if !NET_35 && !SILVERLIGHT
109
namespace Ninject.Extensions.Interception
1110
{
1211
using System.Reflection;
@@ -109,5 +108,4 @@ private void InterceptTaskWithResult<TResult>(IInvocation invocation)
109108
});
110109
}
111110
}
112-
}
113-
#endif
111+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="ErrorHandlingInterceptor.cs" company="Ninject Project Contributors">
3+
// Copyright (c) 2007-2010, Enkari, Ltd.
4+
// Copyright (c) 2010-2017, Ninject Project Contributors
5+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
6+
// </copyright>
7+
// -------------------------------------------------------------------------------------------------
8+
9+
namespace Ninject.Extensions.Interception
10+
{
11+
using System;
12+
13+
/// <summary>
14+
/// A simple definition of an interceptor, which can take action both before and after
15+
/// the invocation proceeds.
16+
/// </summary>
17+
public abstract class ErrorHandlingInterceptor : IInterceptor
18+
{
19+
private readonly IInterceptor interceptor;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="ErrorHandlingInterceptor"/> class.
23+
/// </summary>
24+
protected ErrorHandlingInterceptor()
25+
{
26+
}
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ErrorHandlingInterceptor"/> class.
30+
/// </summary>
31+
/// <param name="interceptor">The interceptor to decorate.</param>
32+
protected ErrorHandlingInterceptor(IInterceptor interceptor)
33+
{
34+
this.interceptor = interceptor;
35+
}
36+
37+
/// <summary>
38+
/// Intercepts the specified invocation.
39+
/// </summary>
40+
/// <param name="invocation">The invocation to intercept.</param>
41+
public void Intercept(IInvocation invocation)
42+
{
43+
try
44+
{
45+
if (this.interceptor == null)
46+
{
47+
invocation.Proceed();
48+
}
49+
else
50+
{
51+
this.interceptor.Intercept(invocation);
52+
}
53+
}
54+
catch (Exception exception)
55+
{
56+
if (!this.HandleException(invocation, exception))
57+
{
58+
throw;
59+
}
60+
}
61+
finally
62+
{
63+
this.CompleteInvoke(invocation);
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Handles exception for the invocation proceeding.
69+
/// </summary>
70+
/// <param name="invocation">The invocation that is being intercepted.</param>
71+
/// <param name="exception">The exception when proceed the invocation.</param>
72+
/// <returns>A boolean value indicating whether the exception is being handled or not.</returns>
73+
protected virtual bool HandleException(IInvocation invocation, Exception exception)
74+
{
75+
return false;
76+
}
77+
78+
/// <summary>
79+
/// Takes some action after the invocation proceeds, no matter the proceeding is success or failed.
80+
/// </summary>
81+
/// <param name="invocation">The invocation that is being intercepted.</param>
82+
protected virtual void CompleteInvoke(IInvocation invocation)
83+
{
84+
}
85+
}
86+
}

src/Ninject.Extensions.Interception/Infrastructure/Language/KernelExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public static void AddMethodInterceptor(
6464
kernel.Components.Get<IMethodInterceptorRegistry>().Add(method, interceptor);
6565
}
6666

67-
#if !NETCF
68-
6967
/// <summary>
7068
/// Intercepts the given method call and replaces it with the proxy action.
7169
/// </summary>
@@ -346,7 +344,5 @@ private static PropertyInfo GetPropertyFromExpression<T>(Expression<Func<T, obje
346344

347345
return (PropertyInfo)memberExpr.Member;
348346
}
349-
350-
#endif
351347
}
352348
}

0 commit comments

Comments
 (0)