From c04e6c7f5857d2566fcf15c3537c242be0188e0e Mon Sep 17 00:00:00 2001 From: kevinpricethesecond Date: Fri, 13 Sep 2024 13:02:26 -0500 Subject: [PATCH] better error handling --- .gitignore | 2 +- service/api.py | 5 +++-- service/errors.py | 4 ++++ service/utils.py | 16 +++++++++++----- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 12d4083..4fc5eed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ # Intellij files .idea/* -config-local.json *pycache* +*config-local.json* \ No newline at end of file diff --git a/service/api.py b/service/api.py index 7416eb6..2680551 100644 --- a/service/api.py +++ b/service/api.py @@ -48,7 +48,8 @@ def __init__(self, url_map, *items): @app.before_request def log_before(): - logger.info(f'========== Received new request:: {request} ==========') + logger.info('\n========== Received new request ==========') + logger.info(f'{request}\n') if request.json: logger.info(f'json:: {request.json}') @@ -58,5 +59,5 @@ def log_after(response): if response.status == '500 INTERNAL SERVER ERROR': logger.error(f'\tEncountered error during request: {response.json}') - logger.info(f'========== Ended request with status:: {response.status} ==========\n') + logger.info(f'\n========== Ended request with status:: {response.status} ==========\n') return response diff --git a/service/errors.py b/service/errors.py index 75d809a..66a33b5 100644 --- a/service/errors.py +++ b/service/errors.py @@ -47,3 +47,7 @@ def __init__(self, msg="Permission denied", code=401): class GlobusPathExists(BaseTapisError): def __init__(self, msg="A directory with given path already exists", code=409): super().__init__(msg, code) + +class CollectionNotFoundError(BaseTapisError): + def __init__(self, msg='Requested endpoint does not exist', code=404): + super().__init__(msg, code) diff --git a/service/utils.py b/service/utils.py index 7b32671..2322dfb 100644 --- a/service/utils.py +++ b/service/utils.py @@ -189,7 +189,6 @@ def get_valid_token(client_id, refresh_token): def handle_transfer_error(exception, endpoint_id=None, msg=None): '''Tanslates transfer api errors into the configured basetapiserrors in ./errors.py''' # logger.debug('in handle transfer error') - logger.exception(exception.__cause__) message = None try: message = exception.message @@ -201,6 +200,8 @@ def handle_transfer_error(exception, endpoint_id=None, msg=None): logger.debug(f'exception has no attribute msg.') logger.critical(f'\nhandling transfer API error:: {exception.code}:: with message {message}\n') + if exception.__cause__: + logger.exception('cause:: ', exception.__cause__) error = InternalServerError(msg='Interal server error', code=500) if getattr(exception, "code", None) == None: logger.debug(f'exception {exception} has no code, therefore returning InternalServerError') @@ -209,6 +210,8 @@ def handle_transfer_error(exception, endpoint_id=None, msg=None): error = AuthenticationError(msg='Could not authenticate transfer client', code=401) if exception.code == "ClientError.NotFound": error = PathNotFoundError(msg='Path does not exist on given endpoint', code=404) + if exception.code == 'EndpointNotFound': + error = CollectionNotFoundError(msg=f"The requested collection with id {endpoint_id} does not exist") if exception.code == "ExternalError.DirListingFailed.GCDisconnected": error = GlobusError(msg=f'Error connecting to endpoint {endpoint_id}. Please activate endpoint manually', code=407) if exception.code == 'ExternalError.DirListingFailed.LoginFailed': @@ -224,7 +227,8 @@ def handle_transfer_error(exception, endpoint_id=None, msg=None): error = InternalServerError(msg="Bad Gateway", code=502) if exception.code == 400: error = GlobusInvalidRequestError(msg=message) - logger.error(error) + if error: + logger.error(error) return error def is_endpoint_activated(tc, ep): @@ -266,12 +270,14 @@ def is_gcp(endpoint_id): res = client.get_endpoint(endpoint_id) except TransferAPIError as e: # assume it's a gcp - logger.error(f'got error checking collection type: {e}') + # logger.error(f'got transferapierror checking collection type: {e} -- {e.code} -- {e.message}') + if e.code == 'EndpointNotFound': + res['is_globus_connect'] = 'false' + raise handle_transfer_error(e, endpoint_id=endpoint_id) res['is_globus_connect'] = 'true' except: logger.error(f'got error checking collection type: {e}') - raise handle_transfer_error(e) - + raise handle_transfer_error(e, endpoint_id=endpoint_id) gcp = True if res["is_globus_connect"] == 'true' else False logger.debug(f'Is collection {endpoint_id} a gcp? : {gcp}')