diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs index 613a259e..fef70abe 100644 --- a/YamlDotNet.Test/Serialization/SerializationTests.cs +++ b/YamlDotNet.Test/Serialization/SerializationTests.cs @@ -1204,6 +1204,13 @@ public void DontIgnoreExtraPropertiesIfWanted() var text = Lines("aaa: hello", "bbb: world"); var actual = Record.Exception(() => Deserializer.Deserialize(UsingReaderFor(text))); Assert.IsType(actual); + ((YamlException)actual).Start.Column.Should().Be(1); + ((YamlException)actual).Start.Line.Should().Be(2); + ((YamlException)actual).Start.Index.Should().Be(12); + ((YamlException)actual).End.Column.Should().Be(4); + ((YamlException)actual).End.Line.Should().Be(2); + ((YamlException)actual).End.Index.Should().Be(15); + ((YamlException)actual).Message.Should().Be("Property 'bbb' not found on type 'YamlDotNet.Test.Serialization.Simple'."); } [Fact] diff --git a/YamlDotNet/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs b/YamlDotNet/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs index 559a29f4..a401abf1 100644 --- a/YamlDotNet/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs +++ b/YamlDotNet/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs @@ -20,6 +20,7 @@ // SOFTWARE. using System; +using System.Runtime.Serialization; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization.Utilities; @@ -54,27 +55,42 @@ bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func(out var _)) { var propertyName = parser.Consume(); - var property = typeDescriptor.GetProperty(implementationType, null, propertyName.Value, ignoreUnmatched); - if (property == null) + try { - parser.SkipThisAndNestedEvents(); - continue; - } + var property = typeDescriptor.GetProperty(implementationType, null, propertyName.Value, ignoreUnmatched); + if (property == null) + { + parser.SkipThisAndNestedEvents(); + continue; + } - var propertyValue = nestedObjectDeserializer(parser, property.Type); - if (propertyValue is IValuePromise propertyValuePromise) - { - var valueRef = value; - propertyValuePromise.ValueAvailable += v => + var propertyValue = nestedObjectDeserializer(parser, property.Type); + if (propertyValue is IValuePromise propertyValuePromise) + { + var valueRef = value; + propertyValuePromise.ValueAvailable += v => + { + var convertedValue = TypeConverter.ChangeType(v, property.Type); + property.Write(valueRef, convertedValue); + }; + } + else { - var convertedValue = TypeConverter.ChangeType(v, property.Type); - property.Write(valueRef, convertedValue); - }; + var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type); + property.Write(value, convertedValue); + } + } + catch (SerializationException ex) + { + throw new YamlException(propertyName.Start, propertyName.End, ex.Message); + } + catch (YamlException) + { + throw; } - else + catch (Exception ex) { - var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type); - property.Write(value, convertedValue); + throw new YamlException(propertyName.Start, propertyName.End, "Exception during deserialization", ex); } } diff --git a/YamlDotNet/Serialization/NodeTypeResolvers/MappingNodeTypeResolver.cs b/YamlDotNet/Serialization/NodeTypeResolvers/MappingNodeTypeResolver.cs index 17960e87..5d974aa7 100644 --- a/YamlDotNet/Serialization/NodeTypeResolvers/MappingNodeTypeResolver.cs +++ b/YamlDotNet/Serialization/NodeTypeResolvers/MappingNodeTypeResolver.cs @@ -31,7 +31,10 @@ public class MappingNodeTypeResolver : INodeTypeResolver public MappingNodeTypeResolver(IDictionary mappings) { - if (mappings == null) throw new ArgumentNullException(nameof(mappings)); + if (mappings == null) + { + throw new ArgumentNullException(nameof(mappings)); + } foreach (var pair in mappings) {