Select Git revision
generate_ci_pipeline.py
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
errors.py 3.54 KiB
import json
import traceback
from flask.wrappers import Response
from functools import wraps
from api.miscellaneous.util 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")
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_404_NOT_FOUND,
"The specified request path does not exist")
ERROR_UNKNOWN_COURSE = ApiError("unknown_course", HTTP_404_NOT_FOUND,
"The specified course cannot be found")
ERROR_UNKNOWN_LECTURE = ApiError("unknown_lecture", HTTP_404_NOT_FOUND,
"The specified lecture cannot be found")
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")