From 2eea193dce52ee8551466deb4c9afdda429c8e98 Mon Sep 17 00:00:00 2001 From: Srinivas Gorur-Shandilya Date: Wed, 7 Aug 2024 14:19:49 -0400 Subject: [PATCH] Minor improvements and bug fixes (#66) ## changes - [x] pretty printing of errors for not found resources - [x] fixed a bug in printing of rows in the CLI --- src/data_hub/_api.py | 10 +++++++++- src/utils.py | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/data_hub/_api.py b/src/data_hub/_api.py index 81b484e6..b03e5f64 100644 --- a/src/data_hub/_api.py +++ b/src/data_hub/_api.py @@ -2,8 +2,9 @@ import sys from deeporigin import auth +from deeporigin.exceptions import DeepOriginException from deeporigin.utils import _get_method, _print_dict -from deeporigin_data import AuthenticationError, DeeporiginData +from deeporigin_data import AuthenticationError, DeeporiginData, NotFoundError # this list specifies methods in the low level API that should # not be automatically wrapped @@ -123,6 +124,13 @@ def dynamic_function( response = method(**kwargs) else: raise error + except NotFoundError as error: + message = error.body["errors"][0]["title"] + raise DeepOriginException( + message, + fix="Could not find resource. Please check ID and organization to make sure this resource exists. ", + title="Deep Origin Error: [Resource Not Found]", + ) if debug: print(f"Response request = {response.http_request}") diff --git a/src/utils.py b/src/utils.py index e3e42cd7..235557e9 100644 --- a/src/utils.py +++ b/src/utils.py @@ -78,6 +78,10 @@ def _truncate(txt: str) -> str: """Utility function for truncating text""" TERMINAL_WIDTH = int(shutil.get_terminal_size().columns / 2) + + if txt is None: + return txt + txt = str(txt) if len(txt) > TERMINAL_WIDTH: txt = txt[: TERMINAL_WIDTH - 3] + "..." return txt @@ -106,7 +110,7 @@ def _print_dict( else: # truncate values so that long strings # don't break the table - data = {key: _truncate(str(value)) for key, value in data.items()} + data = {key: _truncate(value) for key, value in data.items()} if transpose: data = data.items()