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

Re-prettify errors #243

Merged
merged 1 commit into from
Mar 13, 2021
Merged

Re-prettify errors #243

merged 1 commit into from
Mar 13, 2021

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Mar 13, 2021

This re-enables the functionality behind the prettyErrors option (default: true). Now in v2 as the information available to errors is a bit different, they also need to get prettified a bit differently.

Errors now point to a specific location in the source, rather than a range. This means that error.linePos contains only what was previously the "start" value.

In order to determine the line/col position, a LineCounter instance may be constructed internally when parsing. If one is provided via the lineCounter option, that'll be used instead.

For errors at the start of a line, the previous line may also be included in the error.message context for it.

> YAML.parse('foo: bar: baz')

YAMLParseError: Nested mappings are not allowed in compact mappings at line 1, column 6:

foo: bar: baz
     ^

    at [...] {
  offset: 5,
  linePos: { line: 1, col: 6 }
}
> YAML.parse('foo: bar\n- baz\n')

YAMLParseError: A block sequence may not be used as an implicit map key at line 2, column 1:

foo: bar
- baz
^

    at [...] {
  offset: 9,
  linePos: { line: 2, col: 1 }
}
> YAML.parse('foo: bar\n- baz\n', { prettyErrors: false })

YAMLParseError: A block sequence may not be used as an implicit map key
    at [...] {
  offset: 9
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant