[X] bubble markup exception to previewer (#5411)
authorStephane Delcroix <stephane@delcroix.org>
Fri, 1 Mar 2019 00:17:13 +0000 (01:17 +0100)
committerSamantha Houts <samhouts@users.noreply.github.com>
Fri, 1 Mar 2019 00:17:13 +0000 (16:17 -0800)
- fixes #5407

Xamarin.Forms.Xaml.UnitTests/DesignTimeLoaderTests.cs
Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml.cs
Xamarin.Forms.Xaml.UnitTests/XamlLoaderCreateTests.cs
Xamarin.Forms.Xaml/CreateValuesVisitor.cs

index 1c9e16f..4b1d76b 100644 (file)
@@ -24,6 +24,9 @@ namespace Xamarin.Forms.Xaml.UnitTests
                        XamlLoader.ValueCreatedCallback = null;
                        XamlLoader.InstantiationFailedCallback = null;
                        Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
+#pragma warning disable 0618
+                       Xamarin.Forms.Xaml.Internals.XamlLoader.DoNotThrowOnExceptions = false;
+#pragma warning restore 0618
                }
 
                [Test]
@@ -552,6 +555,21 @@ namespace Xamarin.Forms.Xaml.UnitTests
                }
 
                [Test]
+               public void IgnoreMarkupExtensionException()
+               {
+                       var xaml = @"
+                                               <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms""
+                                                       xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
+                                                       <ListView ItemsSource=""{x:Static Foo}"" />
+                                               </ContentPage>";
+
+                       var exceptions = new List<Exception>();
+                       Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+                       Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
+                       Assert.That(exceptions.Count, Is.GreaterThan(1));
+    }
+
+    [Test]
                public void CanResolveRootNode()
                {
                        string assemblyName = null;
index 261de06..fed73ab 100644 (file)
@@ -33,6 +33,12 @@ namespace Xamarin.Forms.Xaml.UnitTests
                        public void Setup()
                        {
                                Device.PlatformServices = new MockPlatformServices();
+
+                               //there's a test not resetting the values correctly, but can't find which one...
+                               Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
+#pragma warning disable 0618
+                               Xamarin.Forms.Xaml.Internals.XamlLoader.DoNotThrowOnExceptions = false;
+#pragma warning restore 0618
                        }
 
                        [TearDown]
index e5486e7..8cdc1af 100644 (file)
@@ -10,6 +10,17 @@ namespace Xamarin.Forms.Xaml.UnitTests
        [TestFixture]
        public class XamlLoaderCreateTests
        {
+               [TearDown]
+               public void TearDown()
+               {
+                       Device.PlatformServices = null;
+                       XamlLoader.FallbackTypeResolver = null;
+                       XamlLoader.ValueCreatedCallback = null;
+                       XamlLoader.InstantiationFailedCallback = null;
+                       Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
+                       Xamarin.Forms.Xaml.Internals.XamlLoader.DoNotThrowOnExceptions = false;
+               }
+
                [Test]
                public void CreateFromXaml ()
                {
index 9927e6c..b74c63b 100644 (file)
@@ -118,8 +118,17 @@ namespace Xamarin.Forms.Xaml
                                foreach (var cnode in node.CollectionItems)
                                        cnode.Accept(visitor, node);
 
-                               value = markup.ProvideValue(serviceProvider);
-
+                               try {
+                                       value = markup.ProvideValue(serviceProvider);
+                               }
+                               catch (Exception e) {
+                                       var xamlpe = e as XamlParseException ?? new XamlParseException("Markup extension failed", serviceProvider, e);
+                                       if (Context.ExceptionHandler != null) {
+                                               Context.ExceptionHandler(xamlpe);
+                                       }
+                                       else
+                                               throw xamlpe;
+                               }
                                if (!node.Properties.TryGetValue(XmlName.xKey, out INode xKey))
                                        xKey = null;