SharpYaml icon indicating copy to clipboard operation
SharpYaml copied to clipboard

Improve Readability of Parsing Error Messages

Open s97712 opened this issue 5 months ago • 2 comments
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?

s97712 avatar Jun 14 '25 17:06 s97712

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: |

s97712 avatar Jun 14 '25 17:06 s97712

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.

xoofx avatar Jun 15 '25 07:06 xoofx