Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chandra-tacc committed Nov 17, 2023
1 parent 1eee6bd commit f3409f0
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/data_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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



Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit f3409f0

Please sign in to comment.