[Xaml] don't swallow exceptions from converters (#4100)

Compiled converters are invoked, by reflection, at compile time. Any
exception thrown there will be wrapped in a TargetInvocationException.
When that happens, we still want to expose the inner XamlParseException
to the user.

- fixes #4099
This commit is contained in:
Stephane Delcroix 2018-10-18 09:02:09 +02:00 коммит произвёл GitHub
Родитель 7bb3ebebd4
Коммит 933046d7ad
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 73 добавлений и 1 удалений

Просмотреть файл

@ -103,8 +103,13 @@ namespace Xamarin.Forms.Build.Tasks
if (compiledConverterName != null && (compiledConverterType = Type.GetType (compiledConverterName)) != null) {
var compiledConverter = Activator.CreateInstance (compiledConverterType);
var converter = typeof(ICompiledTypeConverter).GetMethods ().FirstOrDefault (md => md.Name == "ConvertFromString");
var instructions = (IEnumerable<Instruction>)converter.Invoke (compiledConverter, new object[] {
IEnumerable<Instruction> instructions;
try {
instructions = (IEnumerable<Instruction>)converter.Invoke(compiledConverter, new object[] {
node.Value as string, context, node as BaseNode});
} catch (System.Reflection.TargetInvocationException tie) when (tie.InnerException is XamlParseException) {
throw tie.InnerException;
}
foreach (var i in instructions)
yield return i;
if (targetTypeRef.IsValueType && boxValueTypes)

Просмотреть файл

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Xaml.UnitTests.Gh4099">
<StackLayout Padding="2 2 2 5" />
</ContentPage>

Просмотреть файл

@ -0,0 +1,54 @@
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 Gh4099 : ContentPage
{
public Gh4099()
{
InitializeComponent();
}
public Gh4099(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;
}
[TestCase(true)]
public void BetterExceptionReport(bool useCompiledXaml)
{
if(useCompiledXaml) {
try {
MockCompiler.Compile(typeof(Gh4099));
} catch (XamlParseException xpe) {
Assert.That(xpe.XmlInfo.LineNumber, Is.EqualTo(5));
Assert.Pass();
}
Assert.Fail();
}
}
}
}
}

Просмотреть файл

@ -652,6 +652,9 @@
<Compile Include="Issues\Gh3821View.xaml.cs">
<DependentUpon>Gh3821View.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Gh4099.xaml.cs">
<DependentUpon>Gh4099.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
@ -1200,6 +1203,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Gh4099.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />