Skip to content

v2.1.0 — better parser errors

Latest

Choose a tag to compare

@TrySound TrySound released this 23 Aug 18:31
· 1 commit to master since this release

Better parser errors

This minor release makes malformed SVG easier to diagnose and parser failures easier to handle programmatically.

What changed

  • Parser errors now include concise source context with a caret pointing to the exact error location
  • Source snippets are bounded to 80 columns and one surrounding line on each side, with ellipses showing omitted content
  • Parser Error objects now expose zero-based line and column properties, plus the rendered snippet
  • Empty and whitespace-only input now reports SVG input is empty explicitly
  • Error messages no longer include issue tracker guidance

For example, long input is truncated both vertically and horizontally while preserving the error location:

Expected > (2:76)

…
  <g aria-label="Decorative curve">
…20 C30 40, 50 60, 70 80 S110 120, 130 140" fill="none" stroke"currentColor" />
                                                              ^
  </g>
…

The error metadata can be handled independently of the human-readable message:

try {
  parse(source);
} catch (error) {
  console.log(error.line, error.column, error.snippet);
}

Note: The human-readable text of parser errors has changed. Consumers matching exact error messages should update their expectations.

Install or update with:

npm install svg-parser@2.1.0

Thanks to Christopher Pfohl for contributing structured error metadata.

Full changelog: v2.0.5...v2.1.0