From: Bin Yang Date: Thu, 1 Dec 2022 04:38:33 +0000 (+0800) Subject: Fix error route response X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=ded3e3d01ef6447b5e27a2be03227edd4b5a352b;p=pti%2Fo2.git Fix error route response Issue-ID: INF-386 Signed-off-by: Bin Yang Change-Id: Iad962f6c46394859134e22cfaa21c0868d951b01 --- diff --git a/o2app/entrypoints/flask_application.py b/o2app/entrypoints/flask_application.py index e464de3..c7e0ef9 100644 --- a/o2app/entrypoints/flask_application.py +++ b/o2app/entrypoints/flask_application.py @@ -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() diff --git a/o2common/views/route_exception.py b/o2common/views/route_exception.py index ccb439b..b66a663 100644 --- a/o2common/views/route_exception.py +++ b/o2common/views/route_exception.py @@ -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''' diff --git a/tests/o2app-api-entry2.sh b/tests/o2app-api-entry2.sh index 19b9a04..f343f83 100644 --- a/tests/o2app-api-entry2.sh +++ b/tests/o2app-api-entry2.sh @@ -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