From 102f6523af2b9490181be8aca42af7696269e2eb Mon Sep 17 00:00:00 2001 From: dliu5 Date: Mon, 7 Nov 2022 18:28:17 +0800 Subject: [PATCH] Add exception handling for api side with trace log and With internal error to indicate to client. Signed-off-by: dliu5 Change-Id: I234ec021930c0e73f2725562d8273a7b581faa1a --- o2common/authmw/authmiddleware.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/o2common/authmw/authmiddleware.py b/o2common/authmw/authmiddleware.py index c70adfc..13be910 100644 --- a/o2common/authmw/authmiddleware.py +++ b/o2common/authmw/authmiddleware.py @@ -43,6 +43,11 @@ def _response_wrapper(environ, start_response, header): return res(environ, start_response) +def _internal_err_response_wrapper(environ, start_response): + res = Response(mimetype='text/plain', status=500) + return res(environ, start_response) + + class authmiddleware(): ''' @@ -67,7 +72,15 @@ class authmiddleware(): if ret is True: logger.info( "auth success with oauth token: " + auth_token) - return self.app(environ, start_response) + try: + return self.app(environ, start_response) + except Exception as ex: + logger.error( + 'Internal exception happend \ + ed {}'.format(str(ex)), exc_info=True) + return \ + _internal_err_response_wrapper(environ, + start_response) else: raise AuthFailureExp( 'Bearer realm="Authentication Failed"') @@ -77,7 +90,7 @@ class authmiddleware(): return _response_wrapper(environ, start_response, ex.dictize()) except AuthFailureExp as ex: return _response_wrapper(environ, start_response, ex.dictize()) - except Exception: - hint = 'Bearer realm="Authentication Required"' - return _response_wrapper(environ, start_response, - AuthRequiredExp(hint).dictize()) + except Exception as ex: + logger.error('Internal exception happended {}'.format( + str(ex)), exc_info=True) + return _internal_err_response_wrapper(environ, start_response) -- 2.16.6