Select Git revision
Simon Künzel authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
errors.py 4.71 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_on_error(error: ApiError):
error_json = {
"error_code": error.error_code,
"message": error.message
}
return Response(json.dumps(error_json), error.http_status_code, mimetype="application/json")
def ERROR_BAD_REQUEST(message: str = "The request is invalid") -> ApiError:
return ApiError("bad_request", HTTP_400_BAD_REQUEST, message)
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_INVALID_CSRF_TOKEN = ApiError("invalid_csrf_token", HTTP_403_FORBIDDEN,
"The csrf token is missing or invalid")
ERROR_ACCESS_FORBIDDEN = ApiError("access_forbidden", HTTP_403_FORBIDDEN,
"You do not have access to this resource")
ERROR_RATE_LIMITED = ApiError("rate_limited", HTTP_429_TOO_MANY_REQUESTS,
"You are sending too many requests and are being rate limited. Try again later")
ERROR_UNKNOWN_OBJECT = ApiError("unknown_object", HTTP_404_NOT_FOUND,
"Cannot find the specified object")
ERROR_MODIFICATION_UNEXPECTED_CURRENT_VALUE = ApiError("modification_unexpected_current_value", HTTP_409_CONFLICT,
"The current value does not match the expected last known value")
def ERROR_OBJECT_ERROR(message: str) -> ApiError:
return ApiError("object_error", HTTP_400_BAD_REQUEST,
message)
ERROR_TOO_MANY_SUGGESTIONS = ApiError("too_many_suggestions", HTTP_403_FORBIDDEN,
"Due to too many suggestion, no more are accepted")
ERROR_SITE_IS_READONLY = ApiError("site_is_readonly", HTTP_503_SERVICE_UNAVAILABLE,
"The site is currently in read-only mode")
ERROR_SITE_IS_DISABLED = ApiError("site_is_disabled", HTTP_503_SERVICE_UNAVAILABLE,
"The site is currently disabled")
ALL_ERRORS_RANDOM = [
ERROR_BAD_REQUEST(),
ERROR_METHOD_NOT_ALLOWED,
ERROR_MALFORMED_JSON,
ERROR_REQUEST_MISSING_PARAMETER("?"),
ERROR_REQUEST_INVALID_PARAMETER("?", "?"),
ERROR_UNKNOWN_REQUEST_PATH,
ERROR_INTERNAL_SERVER_ERROR,
ERROR_LECTURE_HAS_NO_PASSWORD,
ERROR_AUTHENTICATION_FAILED,
ERROR_AUTHENTICATION_SERVERS_NOT_AVAILABLE,
ERROR_UNAUTHORIZED,
ERROR_INVALID_CSRF_TOKEN,
ERROR_ACCESS_FORBIDDEN,
ERROR_RATE_LIMITED,
ERROR_UNKNOWN_OBJECT,
ERROR_OBJECT_ERROR("?"),
ERROR_TOO_MANY_SUGGESTIONS,
ERROR_SITE_IS_READONLY,
ERROR_SITE_IS_DISABLED
]