Skip to content

Commit

Permalink
feat: Added more verbose error message to the _resolve function, see …
Browse files Browse the repository at this point in the history
…issue #36 (#37)

Authored-by: Erika Zuljan <[email protected]>
  • Loading branch information
ErikaZ95 authored Feb 6, 2024
1 parent 54b6c14 commit 4e0a848
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions biomedsheets/ref_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 4e0a848

Please sign in to comment.