Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added more verbose error message to the _resolve function, see issue #36 #37

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading