X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2common%2Fviews%2Froute_exception.py;h=b66a66322b1eaa5110ccdc3eb7345ec98d043fad;hb=58994b7d851b47456eed1820d36cc06803777e3b;hp=07a60a366c6245e9fbb99eef1f805fcdee4939c3;hpb=8a84a2ef10be51faf50b2880a3a94194d64459ba;p=pti%2Fo2.git diff --git a/o2common/views/route_exception.py b/o2common/views/route_exception.py index 07a60a3..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''' @@ -85,3 +99,9 @@ def configure_exception(app): '''Return a custom message and 500 status code''' problem = ProblemDetails(500, "Internal Server Error") return problem.serialize(), 500 + + @app.errorhandler(Exception) + def handle_general_exception(error): + '''Return a custom message and 500 status code''' + problem = ProblemDetails(500, "Internal Server Error") + return problem.serialize(), 500