From 202ba0591b1e95df2dde0dad963d7e65895ae132 Mon Sep 17 00:00:00 2001 From: Erika Zuljan Date: Tue, 6 Feb 2024 12:14:41 +0100 Subject: [PATCH 1/2] Added more verbose error message to the _resolve function, see issue #36 --- biomedsheets/ref_resolver.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/biomedsheets/ref_resolver.py b/biomedsheets/ref_resolver.py index 586db5e..a8d9243 100644 --- a/biomedsheets/ref_resolver.py +++ b/biomedsheets/ref_resolver.py @@ -55,20 +55,23 @@ def resolve(self, doc_uri, obj): session = requests.Session() session.mount("file://", requests_file.FileAdapter()) session.mount("resource://", requests_resource.ResourceAdapter()) + key=None with session: - return self._resolve(type(obj)(), obj, session) + return self._resolve(type(obj)(), obj, session, key) - def _resolve(self, base_obj, obj, session): + def _resolve(self, base_obj, obj, session, key): if isinstance(base_obj, (int, bool, float, str)): # JSON atomic return base_obj elif isinstance(base_obj, (dict, MutableMapping)): # JSON object return self._resolve_dict_entry(base_obj, obj, session) elif isinstance(base_obj, (list, MutableSequence)): # JSON list - return [self._resolve(elem, type(elem)(), session) for elem in base_obj] + return [self._resolve(elem, type(elem)(), session, key) for elem in base_obj] else: raise RefResolutionException( - "Can only resolve in dict and list container objects and " - "the atomic types int, bool, float, and str." + f"Can only resolve in dict and list container objects and " + f"the atomic types int, bool, float, and str. " + f"Encountered object of type {type(obj).__name__} with value: {repr(obj)}" + f"Error encountered at key: {str(key)}" ) def _resolve_dict_entry(self, base_obj, obj, session): @@ -94,9 +97,9 @@ def _resolve_dict_entry(self, base_obj, obj, session): for k, v in obj.items(): if k != "$ref": if k in result: - result[k] = self._resolve(v, result[k], session) + result[k] = self._resolve(v, result[k], session, k) else: - result[k] = self._resolve(v, type(v)(), session) + result[k] = self._resolve(v, type(v)(), session, k) return result def _load_ref(self, ref_uri, session): From a355c396b6e6b2f52f96f73d58cd26c3f1f5e18c Mon Sep 17 00:00:00 2001 From: Erika Zuljan Date: Tue, 6 Feb 2024 14:43:12 +0100 Subject: [PATCH 2/2] Formatted code --- biomedsheets/ref_resolver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biomedsheets/ref_resolver.py b/biomedsheets/ref_resolver.py index a8d9243..aec4cc9 100644 --- a/biomedsheets/ref_resolver.py +++ b/biomedsheets/ref_resolver.py @@ -55,7 +55,7 @@ def resolve(self, doc_uri, obj): session = requests.Session() session.mount("file://", requests_file.FileAdapter()) session.mount("resource://", requests_resource.ResourceAdapter()) - key=None + key = None with session: return self._resolve(type(obj)(), obj, session, key)