SharpYaml
SharpYaml copied to clipboard
Improve Readability of Parsing Error Messages
trafficstars
The current parsing error messages are almost unreadable. Could we add more readable and user-friendly error information to help users understand and resolve issues more easily?
I created a simple version:
var serializer = new Serializer();
try
{
return serializer.Deserialize<T>(doc) ?? throw new Exception("Parse error");
} catch (YamlException ex)
{
var lines = doc.Replace("\r\n", "\n").Split("\n");
var start = ex.Start;
var before = lines[Math.Max(start.Line - 3, 0)..Math.Max(start.Line + 1, 0)].ToDelimitedString("\n");
var after = lines[(start.Line + 1)..(start.Line + 5)].ToDelimitedString("\n");
var indicator = new string(' ', ex.Start.Column - 1) + "^";
var lens = $"{before}\n{indicator}\n{after}";
throw new YamlException($"{ex.Message}\n{lens}");
}
It will print errors like this:
(Lin: 0, Col: 10, Chr: 10) - (Lin: 0, Col: 10, Chr: 10): Expected 'SequenceStart', got 'Scalar' (at line 0, character 10).
variables:
^
templates:
reply: |
The current parsing error messages are almost unreadable. Could we add more readable and user-friendly error information to help users understand and resolve issues more easily?
SharpYaml is in maintenance mode, so I don't plan to add new features. As you figured out, it's relatively easy to make your own prettier and opiniated message handler.