Permalink
Browse files

changed error handlers to accept an ExceptionContext instead of Excep…

…tion
  • Loading branch information...
1 parent 6bf317b commit 20a9a8e7ac99fee29ac07cd30ac5f6623f5d15ca @adrianaisemberg adrianaisemberg committed Oct 31, 2011
Showing with 46 additions and 74 deletions.
  1. +1 −0 CLAP/CLAP.csproj
  2. +0 −4 CLAP/ErrorAttribute.cs
  3. +16 −0 CLAP/ExceptionContext.cs
  4. +2 −16 CLAP/ParserRegistration.cs
  5. +13 −43 CLAP/ParserRunner.cs
  6. +13 −8 Tests/Samples.cs
  7. +1 −3 Tests/Tests.cs
View
@@ -79,6 +79,7 @@
<Compile Include="CollectionValidationAttribute.cs" />
<Compile Include="CoverageExcludeAttribute.cs" />
<Compile Include="ErrorAttribute.cs" />
+ <Compile Include="ExceptionContext.cs" />
<Compile Include="FileSystemHelper.cs" />
<Compile Include="FW2Stuff.cs" />
<Compile Include="EmptyAttribute.cs" />
@@ -11,9 +11,5 @@ namespace CLAP
public sealed class ErrorAttribute : Attribute
{
#warning TODO: change to return a bool instead
- /// <summary>
- /// Whether to rethrow the execption after handling it
- /// </summary>
- public bool ReThrow { get; set; }
}
}
@@ -0,0 +1,16 @@
+using System;
+
+namespace CLAP
+{
+ public sealed class ExceptionContext
+ {
+ public Exception Exception { get; private set; }
+
+ public bool ReThrow { get; set; }
+
+ internal ExceptionContext(Exception ex)
+ {
+ Exception = ex;
+ }
+ }
+}
@@ -14,7 +14,7 @@ public sealed class ParserRegistration
internal Dictionary<string, GlobalParameterHandler> RegisteredGlobalHandlers { get; private set; }
internal Dictionary<string, Action<string>> RegisteredHelpHandlers { get; private set; }
internal Action RegisteredEmptyHandler { get; private set; }
- internal Func<Exception, bool> RegisteredErrorHandler { get; private set; }
+ internal Action<ExceptionContext> RegisteredErrorHandler { get; private set; }
internal Action<PreVerbExecutionContext> RegisteredPreVerbInterceptor { get; private set; }
internal Action<PostVerbExecutionContext> RegisteredPostVerbInterceptor { get; private set; }
@@ -85,21 +85,7 @@ public void EmptyHandler(Action handler)
/// Registers an error handler that is executed when an exception is thrown
/// </summary>
/// <param name="handler">The action to be executed</param>
- public void ErrorHandler(Action<Exception> handler)
- {
- ErrorHandler(new Func<Exception, bool>(ex =>
- {
- handler(ex);
-
- return false;
- }));
- }
-
- /// <summary>
- /// Registers an error handler that is executed when an exception is thrown
- /// </summary>
- /// <param name="handler">The action to be executed. Returning "true" will cause the parser to re-throw the original exception</param>
- public void ErrorHandler(Func<Exception, bool> handler)
+ public void ErrorHandler(Action<ExceptionContext> handler)
{
if (RegisteredErrorHandler != null)
{
View
@@ -714,15 +714,11 @@ private IEnumerable<MethodInfo> GetDefinedGlobals()
return globals;
}
- private static IEnumerable<ErrorHandler> GetDefinedErrorHandlers(Type type)
+ private static IEnumerable<MethodInfo> GetDefinedErrorHandlers(Type type)
{
var errorHandlers = type.GetMethodsWith<ErrorAttribute>();
- return errorHandlers.Select(m => new ErrorHandler
- {
- Method = m,
- ReThrow = m.GetAttribute<ErrorAttribute>().ReThrow,
- }).ToList();
+ return errorHandlers;
}
/// <summary>
@@ -934,11 +930,13 @@ private static void VerifyMethodAndTarget(MethodInfo method, object target)
private bool HandleError(Exception ex, object target)
{
+ var context = new ExceptionContext(ex);
+
if (m_registration.RegisteredErrorHandler != null)
{
- var rethrow = m_registration.RegisteredErrorHandler(ex);
+ m_registration.RegisteredErrorHandler(context);
- return rethrow;
+ return context.ReThrow;
}
else
{
@@ -950,21 +948,9 @@ private bool HandleError(Exception ex, object target)
if (handler != null)
{
- var parameters = handler.Method.GetParameters();
-
- if (parameters.None())
- {
- MethodInvoker.Invoke(handler.Method, target, null);
- }
- else
- {
- Debug.Assert(parameters.Length == 1);
- Debug.Assert(parameters[0].ParameterType == typeof(Exception));
-
- MethodInvoker.Invoke(handler.Method, target, new[] { ex });
- }
+ MethodInvoker.Invoke(handler, target, new[] { context });
- return handler.ReThrow;
+ return context.ReThrow;
}
// no handler - rethrow
@@ -995,29 +981,23 @@ private static void ValidateDefinedErrorHandlers(Type type)
// there is only one defined handler
//
- var method = definedErrorHandlers.First().Method;
+ var method = definedErrorHandlers.First();
var parameters = method.GetParameters();
- // no parameters - good
- //
- if (parameters.None())
- {
- return;
- }
- else if (parameters.Length > 1)
+ if (parameters.Length > 1)
{
throw new ArgumentMismatchException(
- "Method '{0}' is marked as [Error] so it should have a single Exception parameter or none".FormatWith(method));
+ "Method '{0}' is marked as [Error] so it should have a single parameter of type CLAP.ExceptionContext".FormatWith(method));
}
else
{
var parameter = parameters.First();
- if (parameter.ParameterType != typeof(Exception))
+ if (parameter.ParameterType != typeof(ExceptionContext))
{
throw new ArgumentMismatchException(
- "Method '{0}' is marked as [Error] so it should have a single Exception parameter or none".FormatWith(method));
+ "Method '{0}' is marked as [Error] so it should have a single parameter of type CLAP.ExceptionContext".FormatWith(method));
}
}
}
@@ -1030,15 +1010,5 @@ private Method GetDefaultVerb()
}
#endregion Private Methods
-
- #region Types
-
- private class ErrorHandler
- {
- internal MethodInfo Method { get; set; }
- internal bool ReThrow { get; set; }
- }
-
- #endregion Types
}
}
View
@@ -580,10 +580,12 @@ public class Sample_33 : BaseSample
{
public Exception Ex { get; set; }
- [Error(ReThrow = true)]
- public void Error(Exception ex)
+ [Error]
+ public void Error(ExceptionContext ex)
{
- Ex = ex;
+ Ex = ex.Exception;
+
+ ex.ReThrow = true;
}
[Verb]
@@ -597,10 +599,12 @@ public class Sample_34 : BaseSample
{
public bool Handled { get; set; }
- [Error(ReThrow = true)]
- public void Error()
+ [Error]
+ public void Error(ExceptionContext ex)
{
Handled = true;
+
+ ex.ReThrow = true;
}
[Verb]
@@ -896,7 +900,7 @@ public void Post(PostVerbExecutionContext context)
}
[Error]
- public void Error(Exception ex)
+ public void Error(ExceptionContext ex)
{
}
}
@@ -917,9 +921,10 @@ public void Post(PostVerbExecutionContext context)
Context = context;
}
- [Error(ReThrow = true)]
- public void Error(Exception ex)
+ [Error]
+ public void Error(ExceptionContext ex)
{
+ ex.ReThrow = true;
}
}
View
@@ -1285,8 +1285,6 @@ public void Execute_HandleError_Registered_NoRethrow()
p.Register.ErrorHandler(ex =>
{
handled = true;
-
- return false;
});
p.RunStatic(new string[] { });
@@ -1320,7 +1318,7 @@ public void Execute_HandleError_Registered_Rethrow()
{
handled = true;
- return true;
+ ex.ReThrow = true;
});
try

0 comments on commit 20a9a8e

Please sign in to comment.