Skip to content

Commit b471dd5

Browse files
Merge pull request #1462 from adityamagadi/master
Add property name to the FormatException #1452
2 parents 28d40a3 + 0fda3d4 commit b471dd5

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/RestSharp/Serializers/Xml/XmlDeserializer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,16 @@ protected virtual object Map(object x, XElement root)
188188
}
189189
else if (type.IsPrimitive)
190190
{
191-
prop.SetValue(x, value.ChangeType(asType), null);
191+
try
192+
{
193+
prop.SetValue(x, value.ChangeType(asType), null);
194+
}
195+
catch (FormatException ex)
196+
{
197+
throw new FormatException(message: $"Couldn't parse the value of '{value}' into the '{prop.Name}'" +
198+
$" property, because it isn't a type of '{prop.PropertyType}'."
199+
, innerException: ex.InnerException);
200+
}
192201
}
193202
else if (type.IsEnum)
194203
{

test/RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
<TargetFrameworks>net452;netcoreapp3.1</TargetFrameworks>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageReference Include="Moq" Version="4.13.1"/>
6+
<PackageReference Include="Moq" Version="4.13.1" />
77
</ItemGroup>
88
<ItemGroup>
9-
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj"/>
9+
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
1010
</ItemGroup>
1111
<ItemGroup>
12+
<None Update="SampleData\GoodreadsFormatError.xml">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</None>
1215
<None Update="SampleData\header_and_rows.xml">
1316
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1417
</None>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<GoodreadsResponse>
3+
<reviews start="1.0230000000000001" end="2" total="288">
4+
<review>
5+
<book id="1208943892">
6+
<isbn>0345475836</isbn>
7+
</book>
8+
</review>
9+
10+
<review>
11+
<Id>1198344567</Id>
12+
<book>
13+
<isbn>0802775802</isbn>
14+
</book>
15+
</review>
16+
</reviews>
17+
</GoodreadsResponse>

test/RestSharp.Tests/XmlDeserializerTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,22 @@ public void Can_Deserialize_Goodreads_Xml()
695695
Assert.AreEqual("1198344567", output.Reviews[1].Id);
696696
}
697697

698+
[Test]
699+
public void Can_throw_format_exception_xml()
700+
{
701+
var xmlpath = PathFor("GoodreadsFormatError.xml");
702+
var doc = XDocument.Load(xmlpath);
703+
var response = new RestResponse { Content = doc.ToString() };
704+
var d = new XmlDeserializer();
705+
Assert.Throws(
706+
typeof(FormatException), () =>
707+
{
708+
var note = d.Deserialize<GoodReadsReviewCollection>(response);
709+
var message = note;
710+
}
711+
);
712+
}
713+
698714
[Test]
699715
public void Can_Deserialize_Google_Weather_Xml()
700716
{

0 commit comments

Comments
 (0)