From 6a3a6648dcdff1bdef63bb3351b453a9e9d54d6c Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 27 Jul 2020 07:08:34 +0200 Subject: items: Improve ItemCache exception message --- rtemsspec/items.py | 9 +++++++-- rtemsspec/tests/spec-item-cache-2/a.yml | 2 ++ rtemsspec/tests/test_items_itemcache.py | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 rtemsspec/tests/spec-item-cache-2/a.yml diff --git a/rtemsspec/items.py b/rtemsspec/items.py index 06c13525..cdcf451d 100644 --- a/rtemsspec/items.py +++ b/rtemsspec/items.py @@ -234,8 +234,13 @@ class Item: def init_parents(self, item_cache: "ItemCache"): """ Initializes the list of links to parents of this items. """ for data in self._data["links"]: - link = Link(item_cache[self.to_abs_uid(data["uid"])], data) - self._links_to_parents.append(link) + try: + link = Link(item_cache[self.to_abs_uid(data["uid"])], data) + self._links_to_parents.append(link) + except KeyError as err: + msg = (f"item '{self.uid}' links " + f"to non-existing item '{data['uid']}'") + raise KeyError(msg) from err def add_link_to_child(self, link: Link): """ Adds a link to a child item of this items. """ diff --git a/rtemsspec/tests/spec-item-cache-2/a.yml b/rtemsspec/tests/spec-item-cache-2/a.yml new file mode 100644 index 00000000..d5b8c7db --- /dev/null +++ b/rtemsspec/tests/spec-item-cache-2/a.yml @@ -0,0 +1,2 @@ +links: +- uid: nix diff --git a/rtemsspec/tests/test_items_itemcache.py b/rtemsspec/tests/test_items_itemcache.py index 678ae2b2..62aab04e 100644 --- a/rtemsspec/tests/test_items_itemcache.py +++ b/rtemsspec/tests/test_items_itemcache.py @@ -62,6 +62,15 @@ def test_load(tmpdir): assert item_cache_3["/d/c"]["v"] == "x" +def test_load_link_error(tmpdir): + config = create_item_cache_config_and_copy_spec(tmpdir, + "spec-item-cache-2") + with pytest.raises( + KeyError, + match=r"^\"item '/a' links to non-existing item 'nix'\"$"): + ItemCache(config) + + class Mapper(ItemMapper): def __init__(self, item): super().__init__(item) -- cgit v1.2.3