-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_handlers.py
30 lines (24 loc) · 1.07 KB
/
error_handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import logging
from flask import jsonify
logger = logging.getLogger(__name__)
def handle_errors(app):
@app.errorhandler(400)
def bad_request_error(error):
logger.error(f"Bad request error: {error}")
return jsonify({"error": "Bad Request", "message": str(error)}), 400
@app.errorhandler(401)
def unauthorized_error(error):
logger.warning(f"Unauthorized request: {error}")
return jsonify({"error": "Unauthorized", "message": str(error)}), 401
@app.errorhandler(403)
def forbidden_error(error):
logger.warning(f"Forbidden request: {error}")
return jsonify({"error": "Forbidden", "message": str(error)}), 403
@app.errorhandler(404)
def not_found_error(error):
logger.warning(f"Not found error: {error}")
return jsonify({"error": "Not Found", "message": str(error)}), 404
@app.errorhandler(500)
def internal_error(error):
logger.error(f"Internal server error: {error}")
return jsonify({"error": "Internal Server Error", "message": str(error)}), 500