Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back stack trace to YamlException.ToString #1000

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions YamlDotNet.Test/Core/YamlExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void TypeDiscriminatingNodeDeserializer_ThrowsWhen_MaxDepthExceeded()
Action act = () => bufferedDeserializer.Deserialize<object>(KubernetesServiceYaml);
act
.Should().Throw<YamlException>()
.WithMessage("Failed to buffer yaml node")
.WithMessage("*Failed to buffer yaml node")
.WithInnerException<ArgumentOutOfRangeException>()
.WithMessage("Parser buffer exceeded max depth*");
}
Expand All @@ -68,7 +68,7 @@ public void TypeDiscriminatingNodeDeserializer_ThrowsWhen_MaxLengthExceeded()
Action act = () => bufferedDeserializer.Deserialize<object>(KubernetesServiceYaml);
act
.Should().Throw<YamlException>()
.WithMessage("Failed to buffer yaml node")
.WithMessage("*Failed to buffer yaml node")
.WithInnerException<ArgumentOutOfRangeException>()
.WithMessage("Parser buffer exceeded max length*");
}
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void DeserializeIncompleteDirective()
Action action = () => Deserializer.Deserialize<object>(UsingReaderFor("%Y"));

action.Should().Throw<SyntaxErrorException>()
.WithMessage("While scanning a directive, found unexpected end of stream.");
.WithMessage("*While scanning a directive, found unexpected end of stream.");
}

[Fact]
Expand Down Expand Up @@ -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]
Expand Down
31 changes: 17 additions & 14 deletions YamlDotNet/Core/YamlException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,49 @@ public class YamlException : Exception
/// </summary>
public Mark End { get; }

/// <summary>
/// Gets the reason that originated the exception.
/// </summary>
public string Reason { get; }

/// <summary>
/// Initializes a new instance of the <see cref="YamlException"/> class.
/// </summary>
/// <param name="message">The message.</param>
public YamlException(string message)
: this(Mark.Empty, Mark.Empty, message)
/// <param name="reason">The message.</param>
public YamlException(string reason)
: this(Mark.Empty, Mark.Empty, reason)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="YamlException"/> class.
/// </summary>
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)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="YamlException"/> class.
/// </summary>
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;
}

/// <summary>
/// Initializes a new instance of the <see cref="YamlException"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="reason">The message.</param>
/// <param name="inner">The inner.</param>
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}";
}
}