Add exception handling for api side with trace log and 16/9516/1
authordliu5 <david.liu@windriver.com>
Mon, 7 Nov 2022 10:28:17 +0000 (18:28 +0800)
committerJackie Huang <jackie.huang@windriver.com>
Tue, 8 Nov 2022 08:24:59 +0000 (08:24 +0000)
With internal error to indicate to client.

Signed-off-by: dliu5 <david.liu@windriver.com>
Change-Id: I234ec021930c0e73f2725562d8273a7b581faa1a
(cherry picked from commit 102f6523af2b9490181be8aca42af7696269e2eb)

o2common/authmw/authmiddleware.py

index c70adfc..13be910 100644 (file)
@@ -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)