mirror of
https://github.com/Casvt/MIND.git
synced 2026-04-03 03:00:22 -04:00
Refactored Flask error handling setup
This commit is contained in:
@@ -7,6 +7,54 @@ from backend.base.definitions import (ApiResponse, InvalidUsernameReason,
|
||||
from backend.base.logging import LOGGER
|
||||
|
||||
|
||||
class LogUnauthMindException(MindException):
|
||||
"""
|
||||
MindExceptions that inherit from this one will trigger a log of the
|
||||
requester's IP address once raised.
|
||||
"""
|
||||
|
||||
|
||||
# region REST responses
|
||||
class NotFound(MindException):
|
||||
@property
|
||||
def api_response(self) -> ApiResponse:
|
||||
return {
|
||||
'code': 404,
|
||||
'error': self.__class__.__name__,
|
||||
'result': {}
|
||||
}
|
||||
|
||||
|
||||
class BadRequest(MindException):
|
||||
@property
|
||||
def api_response(self) -> ApiResponse:
|
||||
return {
|
||||
'code': 400,
|
||||
'error': self.__class__.__name__,
|
||||
'result': {}
|
||||
}
|
||||
|
||||
|
||||
class MethodNotAllowed(MindException):
|
||||
@property
|
||||
def api_response(self) -> ApiResponse:
|
||||
return {
|
||||
'code': 405,
|
||||
'error': self.__class__.__name__,
|
||||
'result': {}
|
||||
}
|
||||
|
||||
|
||||
class InternalError(MindException):
|
||||
@property
|
||||
def api_response(self) -> ApiResponse:
|
||||
return {
|
||||
'code': 500,
|
||||
'error': self.__class__.__name__,
|
||||
'result': {}
|
||||
}
|
||||
|
||||
|
||||
# region Input/Output
|
||||
class KeyNotFound(MindException):
|
||||
"A key was not found in the input that is required to be given"
|
||||
@@ -56,7 +104,7 @@ class InvalidKeyValue(MindException):
|
||||
|
||||
|
||||
# region Auth
|
||||
class AccessUnauthorized(MindException):
|
||||
class AccessUnauthorized(LogUnauthMindException):
|
||||
"The password given is not correct"
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -74,7 +122,7 @@ class AccessUnauthorized(MindException):
|
||||
}
|
||||
|
||||
|
||||
class APIKeyInvalid(MindException):
|
||||
class APIKeyInvalid(LogUnauthMindException):
|
||||
"The API key is not correct"
|
||||
|
||||
def __init__(self, api_key: str) -> None:
|
||||
@@ -92,7 +140,7 @@ class APIKeyInvalid(MindException):
|
||||
}
|
||||
|
||||
|
||||
class APIKeyExpired(MindException):
|
||||
class APIKeyExpired(LogUnauthMindException):
|
||||
"The API key has expired"
|
||||
|
||||
def __init__(self, api_key: str) -> None:
|
||||
@@ -129,7 +177,7 @@ class OperationNotAllowed(MindException):
|
||||
}
|
||||
|
||||
|
||||
class NewAccountsNotAllowed(MindException):
|
||||
class NewAccountsNotAllowed(LogUnauthMindException):
|
||||
"It's not allowed to create a new account except for the admin"
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -266,7 +314,7 @@ class UsernameInvalid(MindException):
|
||||
}
|
||||
|
||||
|
||||
class UserNotFound(MindException):
|
||||
class UserNotFound(LogUnauthMindException):
|
||||
"The user requested can not be found"
|
||||
|
||||
def __init__(
|
||||
|
||||
@@ -14,14 +14,16 @@ from secrets import token_bytes
|
||||
from shutil import copy2, move
|
||||
from sys import base_exec_prefix, executable, platform, version_info
|
||||
from threading import current_thread
|
||||
from typing import Callable, Iterable, List, Sequence, Set, Tuple, Union, cast
|
||||
from typing import (Any, Callable, Dict, Iterable, List,
|
||||
Sequence, Set, Tuple, Union, cast)
|
||||
|
||||
from apprise import Apprise, LogCapture
|
||||
from cron_converter import Cron
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from backend.base.definitions import (WEEKDAY_NUMBER, GeneralReminderData,
|
||||
RepeatQuantity, SendResult, T, U)
|
||||
RepeatQuantity, SendResult,
|
||||
Serialisable, T, U)
|
||||
from backend.base.logging import LOGGER
|
||||
|
||||
|
||||
@@ -199,6 +201,14 @@ def current_thread_id() -> int:
|
||||
return current_thread().native_id or -1
|
||||
|
||||
|
||||
def return_api(
|
||||
result: Serialisable,
|
||||
error: Union[str, None] = None,
|
||||
code: int = 200
|
||||
) -> Tuple[Dict[str, Any], int]:
|
||||
return {'error': error, 'result': result}, code
|
||||
|
||||
|
||||
# region Security
|
||||
def get_hash(salt: bytes, data: str) -> bytes:
|
||||
"""Hash a string using the supplied salt.
|
||||
|
||||
Reference in New Issue
Block a user