Skip to content

Commit

Permalink
Better error messages (#445)
Browse files Browse the repository at this point in the history
* wip

* overhauled yaml validation error messages

* use yaml client (mostly) everywhere

* fix imports

* fix yaml client namespace for python2

* pep8

* code cleanup + typos


automatic commit by git-black, original commits:
  e9177e2
  • Loading branch information
drewbanin authored and iknox-fa committed Feb 8, 2022
1 parent 952e791 commit d07d932
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions core/dbt/clients/yaml_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ def line_no(i, line, width=3):


def prefix_with_line_numbers(string, no_start, no_end):
line_list = string.split('\n')
line_list = string.split("\n")

numbers = range(no_start, no_end)
relevant_lines = line_list[no_start:no_end]

return "\n".join([
line_no(i + 1, line) for (i, line) in zip(numbers, relevant_lines)
])
return "\n".join([line_no(i + 1, line) for (i, line) in zip(numbers, relevant_lines)])


def contextualized_yaml_error(raw_contents, error):
Expand All @@ -44,9 +42,9 @@ def contextualized_yaml_error(raw_contents, error):

nice_error = prefix_with_line_numbers(raw_contents, min_line, max_line)

return YAML_ERROR_MESSAGE.format(line_number=mark.line + 1,
nice_error=nice_error,
raw_error=error)
return YAML_ERROR_MESSAGE.format(
line_number=mark.line + 1, nice_error=nice_error, raw_error=error
)


def safe_load(contents) -> Optional[Dict[str, Any]]:
Expand All @@ -57,7 +55,7 @@ def load_yaml_text(contents):
try:
return safe_load(contents)
except (yaml.scanner.ScannerError, yaml.YAMLError) as e:
if hasattr(e, 'problem_mark'):
if hasattr(e, "problem_mark"):
error = contextualized_yaml_error(contents, e)
else:
error = str(e)
Expand Down

0 comments on commit d07d932

Please sign in to comment.