load_yaml: _resolve_link(): Add context to errors
When a link's target path cannot be resolved, the raw KeyError from _resolve_path() propagates to the caller (or a raw TypeError when a path segment hits a list), without saying which link or which document location caused the failure. The parse-and-resolve sequence is also duplicated in the jw.link and jw.inplace-link branches of _expand_links_recursive(). Extract the sequence into _resolve_link() and wrap navigation failures in ValueError naming the link kind, the spec, and the location in the document. Document that path navigation operates on the unexpanded tree, so intermediate segments must not be link nodes themselves. Update the tests that pinned KeyError and add coverage for the list-navigation case and the error message content. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
b3a3fc0507
commit
503c620341
1 changed files with 47 additions and 14 deletions
|
|
@ -480,7 +480,7 @@ class TestMissingTargets:
|
|||
"""Tests for missing link targets."""
|
||||
|
||||
def test_missing_absolute_target(self, yaml_file: Callable) -> None:
|
||||
with pytest.raises(KeyError):
|
||||
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
||||
load_yaml(yaml_file('link:\n jw.link: nonexistent\n'))
|
||||
|
||||
def test_missing_relative_target(self, yaml_file: Callable) -> None:
|
||||
|
|
@ -488,17 +488,27 @@ class TestMissingTargets:
|
|||
load_yaml(yaml_file('link:\n jw.link: ..missing\n'))
|
||||
|
||||
def test_missing_deep_target(self, yaml_file: Callable) -> None:
|
||||
with pytest.raises(KeyError):
|
||||
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
||||
load_yaml(
|
||||
yaml_file('exists:\n val: ok\nlink:\n jw.link: exists.missing\n')
|
||||
)
|
||||
|
||||
def test_missing_relative_deep_target(self, yaml_file: Callable) -> None:
|
||||
with pytest.raises(KeyError):
|
||||
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
||||
load_yaml(
|
||||
yaml_file('a:\n b: val\nnode:\n link:\n jw.link: ..a.missing\n')
|
||||
)
|
||||
|
||||
def test_missing_target_in_list_raises(self, yaml_file: Callable) -> None:
|
||||
"""Navigating into a list by key name raises with context."""
|
||||
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
||||
load_yaml(yaml_file('items:\n - a\nlink:\n jw.link: items.x\n'))
|
||||
|
||||
def test_missing_target_error_names_link(self, yaml_file: Callable) -> None:
|
||||
"""The error message names the failing link and its location."""
|
||||
with pytest.raises(ValueError, match = "jw.link 'nope' at link"):
|
||||
load_yaml(yaml_file('link:\n jw.link: nope\n'))
|
||||
|
||||
# -- jw.link expansion - No links (regression) --------------------------------
|
||||
|
||||
class TestNoLinksRegression:
|
||||
|
|
@ -619,7 +629,7 @@ class TestEdgeCases:
|
|||
|
||||
def test_link_value_with_pipe_in_target_key(self, yaml_file: Callable) -> None:
|
||||
"""Key with pipe in name — pipe triggers named link parsing."""
|
||||
with pytest.raises(KeyError):
|
||||
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
||||
load_yaml(yaml_file('my|key: val\nlink:\n jw.link: my|key\n'))
|
||||
|
||||
def test_named_link_with_numeric_prefix(self, yaml_file: Callable) -> None:
|
||||
|
|
|
|||
Loading…
Reference in a new issue