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+ }
0 commit comments