Browse Source

Unwrap TargetInvocationExceptions.

Calling Delegate.DynamicInvoke wraps any exception - we want to expose
the actual exception to callers.
Steven Kirk 10 years ago
parent
commit
e85178e0c5

+ 10 - 1
src/Perspex.Interactivity/RoutedEvent.cs

@@ -5,6 +5,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq.Expressions;
 using System.Reflection;
+using System.Runtime.ExceptionServices;
 
 namespace Perspex.Interactivity
 {
@@ -100,7 +101,15 @@ namespace Perspex.Interactivity
                     ((e.Route == RoutingStrategies.Direct) || (e.Route & sub.Routes) != 0) &&
                     (!e.Handled || sub.AlsoIfHandled))
                 {
-                    sub.Handler.DynamicInvoke(sender, e);
+                    try
+                    {
+                        sub.Handler.DynamicInvoke(sender, e);
+                    }
+                    catch (TargetInvocationException ex)
+                    {
+                        // Unwrap the inner exception.
+                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
+                    }
                 }
             }
         }

+ 1 - 2
tests/Perspex.Controls.UnitTests/TopLevelTests.cs

@@ -311,9 +311,8 @@ namespace Perspex.Controls.UnitTests
 
                 target.Template = CreateTemplate();
                 target.Content = child;
-                target.ApplyTemplate();
 
-                Assert.Throws<InvalidOperationException>(() => target.Presenter.ApplyTemplate());
+                Assert.Throws<InvalidOperationException>(() => target.ApplyTemplate());
             }
         }