47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
"""
|
|
Global error handlers for the application.
|
|
|
|
This module provides centralized error handling functions for common HTTP errors.
|
|
"""
|
|
from flask import jsonify, request
|
|
|
|
def register_error_handlers(app):
|
|
"""
|
|
Register global error handlers with the Flask application.
|
|
|
|
Args:
|
|
app: Flask application instance
|
|
"""
|
|
|
|
@app.errorhandler(400)
|
|
def bad_request(error):
|
|
"""Handle 400 Bad Request errors."""
|
|
app.logger.warning(f"400 Bad Request error for URL: {request.url}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': 'Bad request'
|
|
}), 400
|
|
|
|
@app.errorhandler(404)
|
|
def not_found_error(error):
|
|
"""Handle 404 Not Found errors."""
|
|
app.logger.warning(f"404 Not Found error triggered for URL: {request.url}")
|
|
return jsonify({"success": False, "message": "Resource not found"}), 404
|
|
|
|
@app.errorhandler(405)
|
|
def method_not_allowed(error):
|
|
"""Handle 405 Method Not Allowed errors."""
|
|
app.logger.warning(f"405 Method Not Allowed error for URL: {request.url}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': 'Method not allowed'
|
|
}), 405
|
|
|
|
@app.errorhandler(500)
|
|
def internal_error(error):
|
|
"""Handle 500 Internal Server errors."""
|
|
# Note: The specific error causing the 500 might have already been logged
|
|
app.logger.error(f"Global 500 Internal Server error handler triggered: {error}")
|
|
return jsonify({"success": False, "message": "Internal server error"}), 500
|
|
|
|
app.logger.info("Registered global error handlers") |