From 9d4f346dcfca209295e511f8fd06fd49b4262073 Mon Sep 17 00:00:00 2001 From: kevinpricethesecond Date: Thu, 5 Sep 2024 11:17:52 -0500 Subject: [PATCH] better error handling for transfesr --- service/api.py | 8 +++++--- service/controllers/transfers.py | 27 ++++++++++++++++++++++++++- service/errors.py | 5 +++++ service/utils.py | 17 +++++++++++++++-- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/service/api.py b/service/api.py index 2807b92..21bcc68 100644 --- a/service/api.py +++ b/service/api.py @@ -45,16 +45,18 @@ def __init__(self, url_map, *items): api.add_resource(HealthcheckResource, '/v3/globus-proxy/healthcheck') api.add_resource(HelloResource, '/v3/globus-proxy/hello') + @app.before_request def log_before(): - logger.debug(f'Beginning new request:: {request}') + logger.debug(f'========== Received new request:: {request} ==========') if request.json: logger.debug(f'json:: {request.json}') @app.after_request def log_after(response): # logger.debug(f'request complete with status:: {response.data['status']}') - logger.debug(f'request complete with status:: {response.status}\n') + if response.status == '500 INTERNAL SERVER ERROR': - logger.error(f'Encountered error during request: {response}') + logger.error(f'\tEncountered error during request: {response.json}') + logger.debug(f'========== Ended request with status:: {response.status} ==========\n') return response diff --git a/service/controllers/transfers.py b/service/controllers/transfers.py index cabd83a..52a9172 100644 --- a/service/controllers/transfers.py +++ b/service/controllers/transfers.py @@ -16,18 +16,43 @@ class TransferResource(Resource): def post(self, client_id): logger.info(f'Beginning creation of transfer task for client: {client_id} with args: {request.args} and json: {request.json}') logger.debug(f'request headers:: {request.headers}') + logger.debug(f'request json:: {request.json}') + + # setup + access_token = None + refresh_token = None + src = None + dest = None + items = None ## parse args try: access_token = request.args.get('access_token') + assert access_token is not None + except AssertionError: + logger.error('Missing required access_token param') + raise handle_transfer_error(GlobusInvalidRequestError(msg='Invalid request: Missing access_token')) + try: refresh_token = request.args.get('refresh_token') + assert refresh_token is not None + except AssertionError: + logger.error('Missing required refresh_token param') + raise handle_transfer_error(GlobusInvalidRequestError(msg='Invalid request: Missing refresh token')) + if request.json is None: + logger.error('Missing required transfer data') + raise handle_transfer_error(GlobusInvalidRequestError(msg='Invalid request: Missing required transfer data')) + + ## parse transfer data + try: + # access_token = request.args.get('access_token') + # refresh_token = request.args.get('refresh_token') src = request.json.get('source_endpoint') dest = request.json.get('destination_endpoint') items = request.json.get('transfer_items') except Exception as e: logger.error(f'Error parsing args for request:: {e}') - logger.debug(f'have setup args \n{access_token}\n{refresh_token}\n{src}\n{dest}\n{items}') + logger.debug(f'have setup args \naccess_token: {access_token}\nrefresh_token: {refresh_token}\nsrc: {src}\ndest: {dest}\nitems: {items}\n') if not access_token or not refresh_token: logger.error('error parsing args') diff --git a/service/errors.py b/service/errors.py index 752c161..75d809a 100644 --- a/service/errors.py +++ b/service/errors.py @@ -30,6 +30,11 @@ class GlobusError(BaseTapisError): def __init__(self, msg="Uncaught Globus error", code=400): super().__init__(msg, code) +class GlobusInvalidRequestError(BaseTapisError): + """malformed request or missing information""" + def __init__(self, msg="Invalid request", code=400): + super().__init__(msg, code) + class GlobusConsentRequired(BaseTapisError): def __init__(self, msg="Endpoint requires consent", code=403): super().__init__(msg, code) diff --git a/service/utils.py b/service/utils.py index f1dcf2d..f6eaea3 100644 --- a/service/utils.py +++ b/service/utils.py @@ -188,7 +188,18 @@ 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.critical(f'\nhandling transfer API error:: {exception.code}:: with message {exception.message}\n') + logger.debug('in handle transfer error') + message = None + try: + message = exception.message + except AttributeError: + logger.debug(f'exception has no attribute message. trying msg instead') + try: + message = exception.msg + except: + logger.debug(f'exception has no attribute msg.') + + logger.critical(f'\nhandling transfer API error:: {exception.code}:: with message {message}\n') 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') @@ -206,10 +217,12 @@ def handle_transfer_error(exception, endpoint_id=None, msg=None): if exception.code == 'ExternalError.MkdirFailed.Exists': error = GlobusPathExists(msg=f'Directory with given path already exists.', code=409) if exception.code == 'EndpointPermissionDenied': - error = GlobusUnauthorized(msg=e.http_reason) + error = GlobusUnauthorized(msg=exception.http_reason) if exception.code == 'EndpointError': if exception.http_reason == 'Bad Gateway': error = InternalServerError(msg="Bad Gateway", code=502) + if exception.code == 400: + error = GlobusInvalidRequestError(msg=message) logger.error(error) return error