Skip to content

Commit

Permalink
Improve error message for YAML syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Nov 13, 2023
1 parent f6bda2e commit 0d5f2ef
Showing 1 changed file with 37 additions and 3 deletions.
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 @@ def _yaml_value_constructor(loader: yaml.Loader, node: yaml.Node) -> YamlValue:
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 @@ def _parse_yaml(yaml_stream: str | TextIO) -> YamlObject:
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 @@ class DslValidationError(ValueError):
pass


class InvalidYamlError(ValueError):
pass


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

0 comments on commit 0d5f2ef

Please sign in to comment.