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

Improve error message for YAML syntax errors #456

Merged
merged 1 commit into from
Nov 13, 2023
Merged
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
40 changes: 37 additions & 3 deletions tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import sys
import textwrap
from collections.abc import Callable
from decimal import Decimal
from pathlib import Path
from typing import Any, Literal, TextIO, TypeVar, cast
from typing import Any, Literal, TypeVar, cast

import yaml
from attrs import define, evolve
Expand Down Expand Up @@ -100,7 +102,7 @@
return YamlValue(value=_parse_yaml_value(loader, node))


def _parse_yaml(yaml_stream: str | TextIO) -> YamlObject:
def _parse_yaml(yaml_stream: str) -> YamlObject:
"""
Parse a string or stream to YAML.
"""
Expand All @@ -110,7 +112,35 @@
yaml.add_constructor("!" + actual_type, _custom_type_constructors, loader)
yaml.add_constructor("!v", _yaml_value_constructor, loader)
yaml.add_constructor("!value", _yaml_value_constructor, loader)
return yaml.load(yaml_stream, loader)

try:
return yaml.load(yaml_stream, loader)
except yaml.MarkedYAMLError as exc:
lines = yaml_stream.splitlines()

Check warning on line 119 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L118-L119

Added lines #L118 - L119 were not covered by tests

if exc.problem_mark is None:

Check warning on line 121 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L121

Added line #L121 was not covered by tests
# There is no additional information, so what can we do?
raise exc

Check warning on line 123 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L123

Added line #L123 was not covered by tests

sys.stderr.write(

Check warning on line 125 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L125

Added line #L125 was not covered by tests
textwrap.dedent(
f"""
YAML error while parsing test suite. This means there is a YAML syntax error.

The YAML parser indicates the problem lies at line {exc.problem_mark.line + 1}, column {exc.problem_mark.column + 1}:

{lines[exc.problem_mark.line]}
{" " * exc.problem_mark.column + "^"}

The error message was:
{exc.problem} {exc.context}

The detailed exception is provided below.
You might also find help by validating your YAML file with a YAML validator.\n
"""
)
)
raise exc

Check warning on line 143 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L143

Added line #L143 was not covered by tests


def _load_schema_validator():
Expand All @@ -131,6 +161,10 @@
pass


class InvalidYamlError(ValueError):
pass


@define(frozen=True)
class DslContext:
"""
Expand Down
Loading