Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
errors.py 3.57 KiB
import json
import traceback
from flask.wrappers import Response
from functools import wraps
from api.miscellaneous.constants import *
class ApiError:
def __init__(self, error_code: str, http_status_code: int, message: str):
self.error_code = error_code
self.http_status_code = http_status_code
self.message = message
class ApiClientException(Exception):
def __init__(self, error: ApiError):
self.error = error
def api_handle_exceptions(func):
@wraps(func)
def decorator(*args, **kwargs):
try:
return func(*args, **kwargs)
except ApiClientException as e:
return api_on_error(e.error)
except Exception as e:
print("An exception occurred while handling api request:")
traceback.print_exception(e)
return api_on_error(ERROR_INTERNAL_SERVER_ERROR)
return decorator
def api_on_error(error: ApiError) -> Response:
error_json = {
"error_code": error.error_code,
"message": error.message
}
return Response(json.dumps(error_json), error.http_status_code, mimetype="application/json")
ERROR_BAD_REQUEST = ApiError("bad_request", HTTP_400_BAD_REQUEST,
"The request is invalid")
ERROR_METHOD_NOT_ALLOWED = ApiError("method_not_allowed", HTTP_405_METHOD_NOT_ALLOWED,
"The specified request method is not allowed")
ERROR_MALFORMED_JSON = ApiError("malformed_json", HTTP_400_BAD_REQUEST,
"The request json is malformed or missing (Or Content-Type is not application/json)")
def ERROR_REQUEST_MISSING_PARAMETER(parameter_name: str) -> ApiError:
return ApiError("request_missing_parameter", HTTP_400_BAD_REQUEST,
"Missing parameter " + parameter_name + " in request")
def ERROR_REQUEST_INVALID_PARAMETER(parameter_name: str, invalid_message: str) -> ApiError:
return ApiError("request_invalid_parameter", HTTP_400_BAD_REQUEST,
"Parameter " + parameter_name + " in request is invalid: " + invalid_message)
ERROR_UNKNOWN_REQUEST_PATH = ApiError("unknown_request_path", HTTP_400_BAD_REQUEST,
"The specified request path does not exist")
ERROR_INTERNAL_SERVER_ERROR = ApiError("internal_server_error", HTTP_500_INTERNAL_SERVER_ERROR,
"An unknown internal server error occurred")
ERROR_LECTURE_HAS_NO_PASSWORD = ApiError("lecture_has_no_password", HTTP_400_BAD_REQUEST,
"The specified lecture has no password authentication")
ERROR_AUTHENTICATION_FAILED = ApiError("authentication_failed", HTTP_403_FORBIDDEN,
"Authentication failed")
ERROR_AUTHENTICATION_SERVERS_NOT_AVAILABLE = ApiError("authentication_servers_not_available", HTTP_503_SERVICE_UNAVAILABLE,
"Authentication servers are currently not available")
ERROR_UNAUTHORIZED = ApiError("unauthorized", HTTP_401_UNAUTHORIZED,
"You are not authorized")
ERROR_ACCESS_FORBIDDEN = ApiError("access_forbidden", HTTP_403_FORBIDDEN,
"You do not not access to this resource")
ERROR_UNKNOWN_OBJECT = ApiError("unknown_object", HTTP_404_NOT_FOUND,
"Cannot find the specified object")
def ERROR_OBJECT_ERROR(message: str) -> ApiError:
return ApiError("object_error", HTTP_400_BAD_REQUEST,
message)