From a6c08d577ed33b3fab291a4bd81a8b56ff6c62e8 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Tue, 26 Feb 2019 20:07:17 +0100 Subject: [PATCH] [X] wrap typeconverter exceptions in XPE (#5388) - fixes #5378 --- .../Xaml/TypeConversionExtensions.cs | 10 ++++- .../Issues/Gh5378_1.xaml | 7 ++++ .../Issues/Gh5378_1.xaml.cs | 33 +++++++++++++++++ .../Issues/Gh5378_2.xaml | 4 ++ .../Issues/Gh5378_2.xaml.cs | 37 +++++++++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml.cs create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_2.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_2.xaml.cs diff --git a/Xamarin.Forms.Core/Xaml/TypeConversionExtensions.cs b/Xamarin.Forms.Core/Xaml/TypeConversionExtensions.cs index bcb59ab62..52f42a232 100644 --- a/Xamarin.Forms.Core/Xaml/TypeConversionExtensions.cs +++ b/Xamarin.Forms.Core/Xaml/TypeConversionExtensions.cs @@ -114,8 +114,14 @@ namespace Xamarin.Forms.Xaml var xfExtendedTypeConverter = xfTypeConverter as IExtendedTypeConverter; if (xfExtendedTypeConverter != null) return value = xfExtendedTypeConverter.ConvertFromInvariantString(str, serviceProvider); - if (xfTypeConverter != null) - return value = xfTypeConverter.ConvertFromInvariantString(str); + if (xfTypeConverter != null) { + try { + return value = xfTypeConverter.ConvertFromInvariantString(str); + } + catch (Exception e) { + throw new XamlParseException("Type conversion failed", serviceProvider, e); + } + } var converterType = converter?.GetType(); if (converterType != null) { diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml new file mode 100644 index 000000000..10c67bda6 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml @@ -0,0 +1,7 @@ + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml.cs new file mode 100644 index 000000000..df7127beb --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_1.xaml.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + [XamlCompilation(XamlCompilationOptions.Skip)] + public partial class Gh5378_1 : ContentPage + { + public Gh5378_1() => InitializeComponent(); + public Gh5378_1(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] class Tests + { + [SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices(); + [TearDown] public void TearDown() => Device.PlatformServices = null; + + [Test] + public void ReportSyntaxError([Values(false, true)]bool useCompiledXaml) + { + if (useCompiledXaml) + Assert.Throws(() => MockCompiler.Compile(typeof(Gh5378_1))); + else + Assert.Throws(() => new Gh5378_1(useCompiledXaml)); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_2.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_2.xaml new file mode 100644 index 000000000..6059d95fc --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh5378_2.xaml @@ -0,0 +1,4 @@ + + +