You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
52 lines
1.2 KiB
from typing import Dict
|
|
from typing import Optional
|
|
from typing import Union
|
|
|
|
from flask import Response
|
|
from flask import jsonify
|
|
from flask_api import status
|
|
|
|
|
|
class MonsunError(Exception):
|
|
status_code = status.HTTP_501_NOT_IMPLEMENTED
|
|
|
|
data: Dict
|
|
message: Optional[str]
|
|
|
|
def __init__(
|
|
self,
|
|
content: Union[str, Dict],
|
|
):
|
|
if isinstance(content, str):
|
|
self.message = content
|
|
self.data = {"message": self.message}
|
|
elif isinstance(content, Dict):
|
|
self.data = content
|
|
else:
|
|
raise ValueError("'content' must be either a string or an dict")
|
|
|
|
@property
|
|
def response(self) -> Response:
|
|
response: Response = jsonify(self.data)
|
|
response.status_code = self.status_code
|
|
return response
|
|
|
|
|
|
class BadRequest(MonsunError):
|
|
status_code = status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
class Unauthorized(MonsunError):
|
|
status_code = status.HTTP_401_UNAUTHORIZED
|
|
|
|
|
|
class Forbidden(MonsunError):
|
|
status_code = status.HTTP_403_FORBIDDEN
|
|
|
|
|
|
class InternalServerError(MonsunError):
|
|
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
|
|
|
|
class ConfigurationError(InternalServerError):
|
|
"""raised in case of invalid configuration"""
|
|
|