From: dliu5 Date: Mon, 7 Nov 2022 10:28:17 +0000 (+0800) Subject: Add exception handling for api side with trace log and X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=1d8827e06f6df75c8bbe826ab7cefa6c3b6978a2;p=pti%2Fo2.git Add exception handling for api side with trace log and With internal error to indicate to client. Signed-off-by: dliu5 Change-Id: I234ec021930c0e73f2725562d8273a7b581faa1a (cherry picked from commit 102f6523af2b9490181be8aca42af7696269e2eb) --- 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)