Fix error route response 43/9943/1
authorBin Yang <bin.yang@windriver.com>
Thu, 1 Dec 2022 04:38:33 +0000 (12:38 +0800)
committerJackie Huang <jackie.huang@windriver.com>
Mon, 5 Dec 2022 04:47:15 +0000 (12:47 +0800)
Issue-ID: INF-386

Signed-off-by: Bin Yang <bin.yang@windriver.com>
Change-Id: Iad962f6c46394859134e22cfaa21c0868d951b01

o2app/entrypoints/flask_application.py
o2common/views/route_exception.py
tests/o2app-api-entry2.sh

index e464de3..c7e0ef9 100644 (file)
@@ -52,9 +52,11 @@ app.config.SWAGGER_UI_DOC_EXPANSION = 'list'
 # app.config['RESTX_MASK_HEADER'] = 'fields'
 app.config['RESTX_MASK_SWAGGER'] = False
 app.config['ERROR_INCLUDE_MESSAGE'] = False
-api = Api(app, version=FLASK_API_VERSION,
-          title='INF O2 Services API',
-          description='Swagger OpenAPI document for the INF O2 Services',
+api = Api(
+    app, version=FLASK_API_VERSION,
+    catch_all_404s=True,
+    title='INF O2 Services API',
+    description='Swagger OpenAPI document for the INF O2 Services',
           )
 bus = bootstrap.bootstrap()
 
index ccb439b..b66a663 100644 (file)
@@ -18,6 +18,7 @@ from werkzeug.exceptions import (
     MethodNotAllowed,
     NotFound,
     InternalServerError,
+    HTTPException,
 )
 
 
@@ -61,6 +62,19 @@ class ProblemDetails():
 
 def configure_exception(app):
 
+    @app.errorhandler(HTTPException)
+    def default_error_handler(error):
+        '''Default error handler'''
+        status_code = getattr(error, 'code', 500)
+        problem = ProblemDetails(status_code, str(error))
+        return problem.serialize(), status_code
+
+    @app.errorhandler(NotFound)
+    def handle_notfound(error):
+        '''notfound handler'''
+        problem = ProblemDetails(404, str(error))
+        return problem.serialize(), 404
+
     @app.errorhandler(BadRequestException)
     def handle_badrequest_exception(error):
         '''Return a custom message and 400 status code'''
index 19b9a04..f343f83 100644 (file)
@@ -23,7 +23,7 @@ then
 cp /tests/my-root-ca-cert.pem /configs/my-root-ca-cert.pem
 cp /tests/my-server-cert.pem /configs/server.crt
 cp /tests/my-server-key.pem /configs/server.key
-gunicorn -b 0.0.0.0:80 o2app.entrypoints.flask_application:app --certfile /configs/server.crt  --keyfile /configs/server.key
+gunicorn -b 0.0.0.0:80 o2app.entrypoints.flask_application:app --certfile /configs/server.crt  --keyfile /configs/server.key --log-level debug
 else
-gunicorn -b 0.0.0.0:80 o2app.entrypoints.flask_application:app
+gunicorn -b 0.0.0.0:80 o2app.entrypoints.flask_application:app --log-level debug
 fi