Skip to content

Commit

Permalink
Merge pull request #456 from dodona-edu/enhance/yaml-errors
Browse files Browse the repository at this point in the history
Improve error message for YAML syntax errors
  • Loading branch information
niknetniko authored Nov 13, 2023
2 parents f6bda2e + 0d5f2ef commit 1f7da5b
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()

if exc.problem_mark is None:
# There is no additional information, so what can we do?
raise exc

sys.stderr.write(
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


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 1f7da5b

Please sign in to comment.