Skip to content

Commit

Permalink
Merge pull request #1462 from adityamagadi/master
Browse files Browse the repository at this point in the history
Add property name to the FormatException #1452
  • Loading branch information
alexeyzimarev authored May 13, 2020
2 parents 28d40a3 + 0fda3d4 commit b471dd5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/RestSharp/Serializers/Xml/XmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,16 @@ protected virtual object Map(object x, XElement root)
}
else if (type.IsPrimitive)
{
prop.SetValue(x, value.ChangeType(asType), null);
try
{
prop.SetValue(x, value.ChangeType(asType), null);
}
catch (FormatException ex)
{
throw new FormatException(message: $"Couldn't parse the value of '{value}' into the '{prop.Name}'" +
$" property, because it isn't a type of '{prop.PropertyType}'."
, innerException: ex.InnerException);
}
}
else if (type.IsEnum)
{
Expand Down
7 changes: 5 additions & 2 deletions test/RestSharp.Tests/RestSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
<TargetFrameworks>net452;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.13.1"/>
<PackageReference Include="Moq" Version="4.13.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj"/>
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="SampleData\GoodreadsFormatError.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SampleData\header_and_rows.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
17 changes: 17 additions & 0 deletions test/RestSharp.Tests/SampleData/GoodreadsFormatError.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<GoodreadsResponse>
<reviews start="1.0230000000000001" end="2" total="288">
<review>
<book id="1208943892">
<isbn>0345475836</isbn>
</book>
</review>

<review>
<Id>1198344567</Id>
<book>
<isbn>0802775802</isbn>
</book>
</review>
</reviews>
</GoodreadsResponse>
16 changes: 16 additions & 0 deletions test/RestSharp.Tests/XmlDeserializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,22 @@ public void Can_Deserialize_Goodreads_Xml()
Assert.AreEqual("1198344567", output.Reviews[1].Id);
}

[Test]
public void Can_throw_format_exception_xml()
{
var xmlpath = PathFor("GoodreadsFormatError.xml");
var doc = XDocument.Load(xmlpath);
var response = new RestResponse { Content = doc.ToString() };
var d = new XmlDeserializer();
Assert.Throws(
typeof(FormatException), () =>
{
var note = d.Deserialize<GoodReadsReviewCollection>(response);
var message = note;
}
);
}

[Test]
public void Can_Deserialize_Google_Weather_Xml()
{
Expand Down

0 comments on commit b471dd5

Please sign in to comment.