diff --git a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs index 64781dce0..bb8edeba6 100644 --- a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs @@ -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) { diff --git a/test/RestSharp.Tests/RestSharp.Tests.csproj b/test/RestSharp.Tests/RestSharp.Tests.csproj index 3fce2a56b..b427dd2d5 100644 --- a/test/RestSharp.Tests/RestSharp.Tests.csproj +++ b/test/RestSharp.Tests/RestSharp.Tests.csproj @@ -3,12 +3,15 @@ net452;netcoreapp3.1 - + - + + + PreserveNewest + PreserveNewest diff --git a/test/RestSharp.Tests/SampleData/GoodreadsFormatError.xml b/test/RestSharp.Tests/SampleData/GoodreadsFormatError.xml new file mode 100644 index 000000000..c65f67bce --- /dev/null +++ b/test/RestSharp.Tests/SampleData/GoodreadsFormatError.xml @@ -0,0 +1,17 @@ + + + + + + 0345475836 + + + + + 1198344567 + + 0802775802 + + + + \ No newline at end of file diff --git a/test/RestSharp.Tests/XmlDeserializerTests.cs b/test/RestSharp.Tests/XmlDeserializerTests.cs index b6b628ddb..fdc6390c8 100644 --- a/test/RestSharp.Tests/XmlDeserializerTests.cs +++ b/test/RestSharp.Tests/XmlDeserializerTests.cs @@ -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(response); + var message = note; + } + ); + } + [Test] public void Can_Deserialize_Google_Weather_Xml() {