rebuild(stage-3): establish accounts permissions and secure settings
This commit is contained in:
@@ -9,10 +9,12 @@ from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from backend.errors import BusinessError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@dataclass(slots=True)
|
||||
class AppError(Exception):
|
||||
code: str
|
||||
message: str
|
||||
@@ -39,6 +41,23 @@ def _response(request: Request, status_code: int, code: str, message: str) -> JS
|
||||
|
||||
|
||||
def install_error_handlers(application: FastAPI) -> None:
|
||||
@application.exception_handler(BusinessError)
|
||||
async def business_error(request: Request, error: BusinessError) -> JSONResponse:
|
||||
status_code = {
|
||||
"invalid_credentials": HTTPStatus.UNAUTHORIZED,
|
||||
"account_unavailable": HTTPStatus.FORBIDDEN,
|
||||
"username_taken": HTTPStatus.CONFLICT,
|
||||
"account_not_found": HTTPStatus.NOT_FOUND,
|
||||
"model_not_found": HTTPStatus.NOT_FOUND,
|
||||
"permanent_membership": HTTPStatus.CONFLICT,
|
||||
"model_in_use": HTTPStatus.CONFLICT,
|
||||
"model_name_taken": HTTPStatus.CONFLICT,
|
||||
"model_pool_full": HTTPStatus.CONFLICT,
|
||||
"model_not_configured": HTTPStatus.SERVICE_UNAVAILABLE,
|
||||
"profile_unavailable": HTTPStatus.SERVICE_UNAVAILABLE,
|
||||
}.get(error.code, HTTPStatus.BAD_REQUEST)
|
||||
return _response(request, status_code, error.code, error.message)
|
||||
|
||||
@application.exception_handler(AppError)
|
||||
async def application_error(request: Request, error: AppError) -> JSONResponse:
|
||||
return _response(request, error.status_code, error.code, error.message)
|
||||
|
||||
Reference in New Issue
Block a user