Untitled
unknown
python
a month ago
933 B
6
Indexable
Never
# Define mappings of exceptions to status codes EXCEPTION_STATUS_CODE_MAPPING = { 400: [ValueError, KeyError], 404: [FileNotFoundError], 403: [PermissionError], 401: [AuthenticationError], 409: [ConflictError], # Add other status codes and exceptions as needed } # Flask error handler for all exceptions @views.errorhandler(Exception) def handle_view_exception(exception): # Iterate over the exception-to-status-code mapping for status_code, exception_list in EXCEPTION_STATUS_CODE_MAPPING.items(): if isinstance(exception, tuple(exception_list)): # Return the appropriate response for the exception return exception.message, status_code # If no matching exception, log the exception and return 500 Internal Server Error logging.exception(f"Unhandled exception: {exception}") # Return a generic 500 error response return 'Internal Server Error', 500
Leave a Comment