Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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