Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpricethesecond committed Sep 13, 2024
1 parent 72b783e commit c04e6c7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Intellij files
.idea/*
config-local.json
*pycache*
*config-local.json*
5 changes: 3 additions & 2 deletions service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

Expand All @@ -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
4 changes: 4 additions & 0 deletions service/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 11 additions & 5 deletions service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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':
Expand All @@ -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):
Expand Down Expand Up @@ -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}')
Expand Down

0 comments on commit c04e6c7

Please sign in to comment.