~sthagen/putki#8: 
Fix noisy exceptions from naive key access in traversal (cf. line 148)

https://git.sr.ht/~sthagen/putki/tree/default/item/putki/traverse.py#L148 spams the logs when target key not present:

2023-11-29T09:45:44.002787+00:00 INFO [PUTKI]: - com-pon-ent:
2023-11-29T09:45:44.003364+00:00 INFO [PUTKI]:   + design:
Traceback (most recent call last):

  File "putki", line 8, in 
    sys.exit(app())

  File "putki/cli.py", line 170, in verify_structures
    api.verify_structures(doc_root=doc, structures_name=structures, component=component, options=options)

  File "putki/api.py", line 42, in verify_structures
    code, message, root, claims = traverse.follow(structures_name)

  File "putki/traverse.py", line 148, in follow
    for facet in structure_info[target]:

KeyError: ' com-pon-ent_design'

Can we maybe either silence the exception or provide a concise useful information instead?

Proposal:

diff --git a/putki/traverse.py b/putki/traverse.py
index 9ddff46..ce8c344 100644
--- a/putki/traverse.py
+++ b/putki/traverse.py
@@ -145,8 +145,12 @@ def follow(structures_path: Path) -> tuple[int, str, pathlib.Path, dict[str, Any
             ssp = sub_p.parent / structure_path_str
             structure_info = load_yaml(ssp)
             if structure_info:
-                for facet in structure_info[target]:
-                    validate_facet(facet, ssp)
+                facets = structure_info.get(target)
+                if facets is None:
+                    log.warning(f'target ({target}) not found in data - skipping facet validation')
+                else:
+                    for facet in facets:
+                        validate_facet(facet, ssp)
     return 0, '', root_path, claims
Status
RESOLVED FIXED
Submitter
~sthagen
Assigned to
Submitted
1 year, 3 months ago
Updated
1 year, 3 months ago
Labels
Bug

~sthagen REPORTED FIXED 1 year, 3 months ago

Fixed in version 2023.11.29.

Register here or Log in to comment, or comment via email.