Skip to content

Commit

Permalink
better error handling for transfesr
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpricethesecond committed Sep 5, 2024
1 parent bfd5676 commit 9d4f346
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
8 changes: 5 additions & 3 deletions service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 26 additions & 1 deletion service/controllers/transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions service/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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

Expand Down

0 comments on commit 9d4f346

Please sign in to comment.