-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,120 @@ | ||
import json | ||
from flask import Response, Flask | ||
from flask import Response, Blueprint | ||
from werkzeug.exceptions import ( | ||
HTTPException, | ||
NotFound, | ||
BadRequest, | ||
Unauthorized, | ||
Forbidden, | ||
InternalServerError, | ||
) | ||
|
||
error_bp = Blueprint("error", __name__) | ||
|
||
class ErrorRoutes: | ||
""" | ||
Error routing class | ||
""" | ||
|
||
@staticmethod | ||
def init_app(app: Flask) -> None: | ||
""" | ||
Register all error handlers with the Flask app | ||
@error_bp.app_errorhandler(404) | ||
def handle_404(error) -> Response: | ||
"""Specific handler for 404 Not Found errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
404, | ||
mimetype="application/json", | ||
) | ||
|
||
Args: | ||
app (Flask): The Flask app to register error handlers | ||
""" | ||
app.register_error_handler(404, ErrorRoutes.handle_404) | ||
app.register_error_handler(400, ErrorRoutes.handle_400) | ||
app.register_error_handler(401, ErrorRoutes.handle_401) | ||
app.register_error_handler(403, ErrorRoutes.handle_403) | ||
app.register_error_handler(500, ErrorRoutes.handle_500) | ||
app.register_error_handler( | ||
HTTPException, ErrorRoutes.handle_http_exception | ||
) | ||
app.register_error_handler(Exception, ErrorRoutes.handle_generic_error) | ||
|
||
@staticmethod | ||
def handle_http_exception(error: HTTPException) -> Response: | ||
"""Generic handler for HTTPException; should be raised if no specific handler is found""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": error.description, | ||
"result": None, | ||
} | ||
), | ||
error.code, | ||
mimetype="application/json", | ||
) | ||
@error_bp.app_errorhandler(400) | ||
def handle_400(error) -> Response: | ||
"""Specific handler for 400 Bad Request errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
400, | ||
mimetype="application/json", | ||
) | ||
|
||
@staticmethod | ||
def handle_404(error: NotFound) -> Response: | ||
"""Specific handler for 404 Not Found errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
404, | ||
mimetype="application/json", | ||
) | ||
|
||
@staticmethod | ||
def handle_400(error: BadRequest) -> Response: | ||
"""Specific handler for 400 Bad Request errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
400, | ||
mimetype="application/json", | ||
) | ||
@error_bp.app_errorhandler(401) | ||
def handle_401(error) -> Response: | ||
"""Specific handler for 401 Unauthorized errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
401, | ||
mimetype="application/json", | ||
) | ||
|
||
@staticmethod | ||
def handle_401(error: Unauthorized) -> Response: | ||
"""Specific handler for 401 Unauthorized errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
401, | ||
mimetype="application/json", | ||
) | ||
|
||
@staticmethod | ||
def handle_403(error: Forbidden) -> Response: | ||
"""Specific handler for 403 Forbidden errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
403, | ||
mimetype="application/json", | ||
) | ||
@error_bp.app_errorhandler(403) | ||
def handle_403(error) -> Response: | ||
"""Specific handler for 403 Forbidden errors""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
403, | ||
mimetype="application/json", | ||
) | ||
|
||
@staticmethod | ||
def handle_500(error: InternalServerError) -> Response: | ||
"""Specific handler for 500 Internal Server Error""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
500, | ||
mimetype="application/json", | ||
) | ||
|
||
@staticmethod | ||
def handle_generic_error(error: Exception) -> Response: | ||
"""Handler for any unhandled exceptions""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
500, | ||
mimetype="application/json", | ||
) | ||
@error_bp.app_errorhandler(500) | ||
def handle_500(error) -> Response: | ||
"""Specific handler for 500 Internal Server Error""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
500, | ||
mimetype="application/json", | ||
) | ||
|
||
|
||
@error_bp.app_errorhandler(HTTPException) | ||
def handle_http_exception(error: HTTPException) -> Response: | ||
"""Generic handler for HTTPException; should be raised if no specific handler is found""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": error.description, | ||
"result": None, | ||
} | ||
), | ||
error.code, | ||
mimetype="application/json", | ||
) | ||
|
||
|
||
@error_bp.app_errorhandler(Exception) | ||
def handle_generic_error(error: Exception) -> Response: | ||
"""Handler for any unhandled exceptions""" | ||
return Response( | ||
json.dumps( | ||
{ | ||
"status": "error", | ||
"message": str(error), | ||
"result": None, | ||
} | ||
), | ||
500, | ||
mimetype="application/json", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters