diff --git a/YamlDotNet.Test/Core/YamlExceptionTests.cs b/YamlDotNet.Test/Core/YamlExceptionTests.cs index 2c3e66f2..bd6780bc 100644 --- a/YamlDotNet.Test/Core/YamlExceptionTests.cs +++ b/YamlDotNet.Test/Core/YamlExceptionTests.cs @@ -29,35 +29,35 @@ namespace YamlDotNet.Test.Core public class YamlExceptionTests { [Fact] - public void VerifyToStringWithEmptyMarks() + public void VerifyMessageWithEmptyMarks() { var exception = new YamlException(Mark.Empty, Mark.Empty, "Test exception message"); - exception.ToString().Should().Be("(Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Test exception message"); - exception.Message.Should().Be("Test exception message"); + exception.Message.Should().Be("(Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Test exception message"); + exception.Reason.Should().Be("Test exception message"); } [Fact] - public void VerifyToStringWithNonEmptyMarks() + public void VerifyMessageWithNonEmptyMarks() { var exception = new YamlException(new Mark(1, 1, 1), new Mark(10, 10, 10), "Test exception message"); - exception.ToString().Should().Be("(Line: 1, Col: 1, Idx: 1) - (Line: 10, Col: 10, Idx: 10): Test exception message"); - exception.Message.Should().Be("Test exception message"); + exception.Message.Should().Be("(Line: 1, Col: 1, Idx: 1) - (Line: 10, Col: 10, Idx: 10): Test exception message"); + exception.Reason.Should().Be("Test exception message"); } [Fact] - public void VerifyToStringWithInnerExceptionAndMarks() + public void VerifyMessageWithInnerExceptionAndMarks() { var exception = new YamlException(new Mark(1, 1, 1), new Mark(10, 10, 10), "Test exception message", new InvalidOperationException("Test inner exception")); - exception.ToString().Should().Be("(Line: 1, Col: 1, Idx: 1) - (Line: 10, Col: 10, Idx: 10): Test exception message"); - exception.Message.Should().Be("Test exception message"); + exception.Message.Should().Be("(Line: 1, Col: 1, Idx: 1) - (Line: 10, Col: 10, Idx: 10): Test exception message"); + exception.Reason.Should().Be("Test exception message"); } [Fact] - public void VerifyToStringWithInnerException() + public void VerifyMessageWithInnerException() { var exception = new YamlException("Test exception message", new InvalidOperationException("Test inner exception")); - exception.ToString().Should().Be("(Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Test exception message"); - exception.Message.Should().Be("Test exception message"); + exception.Message.Should().Be("(Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Test exception message"); + exception.Reason.Should().Be("Test exception message"); } } } diff --git a/YamlDotNet.Test/Serialization/BufferedDeserialization/TypeDiscriminatingNodeDeserializerTests.cs b/YamlDotNet.Test/Serialization/BufferedDeserialization/TypeDiscriminatingNodeDeserializerTests.cs index d19495d4..8e195be1 100644 --- a/YamlDotNet.Test/Serialization/BufferedDeserialization/TypeDiscriminatingNodeDeserializerTests.cs +++ b/YamlDotNet.Test/Serialization/BufferedDeserialization/TypeDiscriminatingNodeDeserializerTests.cs @@ -47,7 +47,7 @@ public void TypeDiscriminatingNodeDeserializer_ThrowsWhen_MaxDepthExceeded() Action act = () => bufferedDeserializer.Deserialize(KubernetesServiceYaml); act .Should().Throw() - .WithMessage("Failed to buffer yaml node") + .WithMessage("*Failed to buffer yaml node") .WithInnerException() .WithMessage("Parser buffer exceeded max depth*"); } @@ -68,7 +68,7 @@ public void TypeDiscriminatingNodeDeserializer_ThrowsWhen_MaxLengthExceeded() Action act = () => bufferedDeserializer.Deserialize(KubernetesServiceYaml); act .Should().Throw() - .WithMessage("Failed to buffer yaml node") + .WithMessage("*Failed to buffer yaml node") .WithInnerException() .WithMessage("Parser buffer exceeded max length*"); } diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs index 7bff67b1..f31a1421 100644 --- a/YamlDotNet.Test/Serialization/SerializationTests.cs +++ b/YamlDotNet.Test/Serialization/SerializationTests.cs @@ -225,7 +225,7 @@ public void DeserializeIncompleteDirective() Action action = () => Deserializer.Deserialize(UsingReaderFor("%Y")); action.Should().Throw() - .WithMessage("While scanning a directive, found unexpected end of stream."); + .WithMessage("*While scanning a directive, found unexpected end of stream."); } [Fact] @@ -1331,7 +1331,7 @@ public void DontIgnoreExtraPropertiesIfWanted() ((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'."); + ((YamlException)actual).Message.Should().Contain("Property 'bbb' not found on type 'YamlDotNet.Test.Serialization.Simple'."); } [Fact] diff --git a/YamlDotNet/Core/YamlException.cs b/YamlDotNet/Core/YamlException.cs index ffe1f5fe..e8fd3e3a 100644 --- a/YamlDotNet/Core/YamlException.cs +++ b/YamlDotNet/Core/YamlException.cs @@ -38,46 +38,49 @@ public class YamlException : Exception /// public Mark End { get; } + /// + /// Gets the reason that originated the exception. + /// + public string Reason { get; } + /// /// Initializes a new instance of the class. /// - /// The message. - public YamlException(string message) - : this(Mark.Empty, Mark.Empty, message) + /// The message. + public YamlException(string reason) + : this(Mark.Empty, Mark.Empty, reason) { } /// /// Initializes a new instance of the class. /// - public YamlException(in Mark start, in Mark end, string message) - : this(start, end, message, null) + public YamlException(in Mark start, in Mark end, string reason) + : this(start, end, reason, null) { } /// /// Initializes a new instance of the class. /// - public YamlException(in Mark start, in Mark end, string message, Exception? innerException) - : base(message, innerException) + public YamlException(in Mark start, in Mark end, string reason, Exception? innerException) + : base(null, innerException) { Start = start; End = end; + Reason = reason; } /// /// Initializes a new instance of the class. /// - /// The message. + /// The message. /// The inner. - public YamlException(string message, Exception inner) - : this(Mark.Empty, Mark.Empty, message, inner) + public YamlException(string reason, Exception inner) + : this(Mark.Empty, Mark.Empty, reason, inner) { } - public override string ToString() - { - return $"({Start}) - ({End}): {Message}"; - } + public override string Message => $"({Start}) - ({End}): {Reason}"; } }