From f3409f08ca9b6d168fde614b995140dbfb196d65 Mon Sep 17 00:00:00 2001 From: Chandra Y Date: Fri, 17 Nov 2023 10:25:37 -0600 Subject: [PATCH] Fix error handling --- src/data_loading.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/data_loading.py b/src/data_loading.py index 93a4758..bc73b3e 100644 --- a/src/data_loading.py +++ b/src/data_loading.py @@ -29,13 +29,13 @@ class TapisTokenRetrievalException(Exception): def handle_exception(ex, api_message): '''Handle errors for api requests. Provide error code for categorizing response''' - logger.error(("Error in {} request: {0}").format(api_message, str(ex))) + logger.error(("Error in {0} request: {1}").format(api_message, str(ex))) error_code = 'DATA_ERROR' if isinstance(ex, MissingPortalSessionIdException): error_code = "MISSING_SESSION_ID" elif isinstance(ex, TapisTokenRetrievalException): error_code = "INVALID_TAPIS_TOKEN" - return jsonify('error_code: {}, error: {}'.format(error_code, ex)) + return jsonify('error_code: {0}, error: {1}'.format(error_code, ex)) # ---------------------------------------------------------------------------- # Updating data checks @@ -262,13 +262,11 @@ def get_api_consort_data(tapis_token, return consort_data_json else: - logger.exception("Unauthorized attempt to access Consort data") - return None + raise TapisTokenRetrievalException() - except Exception as e: + except Exception: traceback.print_exc() - return None - + raise ## Function to rebuild dataset from apis @@ -307,12 +305,11 @@ def get_api_imaging_data(tapis_token): return imaging_data_json else: - logger.exception("Unauthorized attempt to access Imaging data") - return None + raise TapisTokenRetrievalException() - except Exception as e: + except Exception: traceback.print_exc() - return "exception: {}".format(e) + raise ## Function to rebuild dataset from apis @@ -364,12 +361,11 @@ def get_api_blood_data(tapis_token): return blood_data_json, request_status else: - logger.exception("Unauthorized attempt to access Blood data") - return None + raise TapisTokenRetrievalException() - except Exception as e: + except Exception: traceback.print_exc() - return None + raise @@ -399,12 +395,10 @@ def get_api_subjects_json(tapis_token): return subjects_json else: - logger.exception("Unauthorized attempt to access Subjects data") - return None - - except Exception as e: + raise TapisTokenRetrievalException() + except Exception: traceback.print_exc() - return None + raise # Retry handler for requests @retry(wait_exponential_multiplier=500, wait_exponential_max=5000, stop_max_attempt_number=3) @@ -417,7 +411,7 @@ def get_tapis_token(api_request): '''Get tapis token using the session cookie. If the session is not authenticated, this will fail.''' session_id = api_request.cookies.get("coresessionid") if session_id is None: - raise MissingPortalSessionId("Missing session id") + raise MissingPortalSessionIdException("Missing session id") try: cookies = {'coresessionid':session_id} response = make_request_with_retry(portal_api_root + '/auth/tapis/', cookies) @@ -427,7 +421,7 @@ def get_tapis_token(api_request): logger.info("Received tapis token.") return tapis_token except Exception as e: - raise InvalidTapisToken('Unable to get Tapis Token') from e + raise TapisTokenRetrievalException('Unable to get Tapis Token') from e def make_report_data_request(url, tapis_token):