From 7a016bbc9f0d9ee89fc01433d6b4a5eb163ca9b9 Mon Sep 17 00:00:00 2001 From: "GLOBAL\\H245254" Date: Tue, 12 May 2020 10:52:56 +0530 Subject: [PATCH 1/2] Add property name to the FormatException #1452 Fixed format exception error --- .../Serializers/Xml/XmlDeserializer.cs | 9 ++++++++- test/RestSharp.Tests/RestSharp.Tests.csproj | 7 +++++-- .../SampleData/GoodreadsFormatError.xml | 17 +++++++++++++++++ test/RestSharp.Tests/XmlDeserializerTests.cs | 16 ++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 test/RestSharp.Tests/SampleData/GoodreadsFormatError.xml diff --git a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs index 64781dce0..c30253290 100644 --- a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs @@ -188,7 +188,14 @@ 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) + { + throw new FormatException("Format exception while Deserializing Property Name - " + prop.Name); + } } 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() { From 0fda3d4021c8ab56703a3f1cbda25008b549dc5a Mon Sep 17 00:00:00 2001 From: "GLOBAL\\H245254" Date: Tue, 12 May 2020 20:04:52 +0530 Subject: [PATCH 2/2] Add property name to the FormatException #1452 Added inner exception and string encoding. --- src/RestSharp/Serializers/Xml/XmlDeserializer.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs index c30253290..bb8edeba6 100644 --- a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs @@ -192,9 +192,11 @@ protected virtual object Map(object x, XElement root) { prop.SetValue(x, value.ChangeType(asType), null); } - catch (FormatException) + catch (FormatException ex) { - throw new FormatException("Format exception while Deserializing Property Name - " + prop.Name); + 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)