Add Constants
This commit is contained in:
parent
539bd8c0f9
commit
de000905b3
31
api/src/core/CONSTANTS.py
Normal file
31
api/src/core/CONSTANTS.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
VALIDATION_PREFIXES = (
|
||||||
|
"computed_field:",
|
||||||
|
"normative_field:",
|
||||||
|
"unknown_column:",
|
||||||
|
"key_field:",
|
||||||
|
"structural_field:",
|
||||||
|
"bad_column_format:",
|
||||||
|
"bad_booking_year:",
|
||||||
|
"bad_booking_quarter:",
|
||||||
|
"unsupported_scope:",
|
||||||
|
)
|
||||||
|
|
||||||
|
PG_404_ERRORS = (
|
||||||
|
"не существует",
|
||||||
|
"не принадлежит",
|
||||||
|
"not found",
|
||||||
|
)
|
||||||
|
PG_409_ERRORS = (
|
||||||
|
"unique",
|
||||||
|
"duplicate",
|
||||||
|
"foreign key",
|
||||||
|
)
|
||||||
|
PG_400_ERRORS = (
|
||||||
|
"violates check constraint",
|
||||||
|
)
|
||||||
|
PG_403_ERRORS = (
|
||||||
|
"permission denied",
|
||||||
|
"insufficient privilege",
|
||||||
|
"role_not_allowed:",
|
||||||
|
"window_closed:",
|
||||||
|
)
|
||||||
@ -9,21 +9,14 @@ from src.core.errors import (
|
|||||||
UsernameConflictException,
|
UsernameConflictException,
|
||||||
ValidationException,
|
ValidationException,
|
||||||
)
|
)
|
||||||
|
from src.core.CONSTANTS import (
|
||||||
|
VALIDATION_PREFIXES,
|
||||||
VALIDATION_PREFIXES = (
|
PG_404_ERRORS,
|
||||||
"computed_field:",
|
PG_409_ERRORS,
|
||||||
"normative_field:",
|
PG_400_ERRORS,
|
||||||
"unknown_column:",
|
PG_403_ERRORS,
|
||||||
"key_field:",
|
|
||||||
"structural_field:",
|
|
||||||
"bad_column_format:",
|
|
||||||
"bad_booking_year:",
|
|
||||||
"bad_booking_quarter:",
|
|
||||||
"unsupported_scope:",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _error_payload(code: str | int, message: str, field: str | None = None) -> dict:
|
def _error_payload(code: str | int, message: str, field: str | None = None) -> dict:
|
||||||
"""Формирует структурированный payload ошибки по спеке."""
|
"""Формирует структурированный payload ошибки по спеке."""
|
||||||
return {
|
return {
|
||||||
@ -60,19 +53,15 @@ def map_sqlalchemy_error(exc: SQLAlchemyError) -> tuple[int, str, str, str | Non
|
|||||||
if any(low.startswith(prefix) for prefix in VALIDATION_PREFIXES):
|
if any(low.startswith(prefix) for prefix in VALIDATION_PREFIXES):
|
||||||
code, field = _parse_prefixed_validation_error(message)
|
code, field = _parse_prefixed_validation_error(message)
|
||||||
return status.HTTP_422_UNPROCESSABLE_ENTITY, code, message, field
|
return status.HTTP_422_UNPROCESSABLE_ENTITY, code, message, field
|
||||||
if low.startswith("role_not_allowed:") or low.startswith("window_closed:"):
|
if any(error in low for error in PG_404_ERRORS):
|
||||||
return status.HTTP_403_FORBIDDEN, "access_denied", message, None
|
|
||||||
if "не существует" in low or "не принадлежит" in low or "not found" in low:
|
|
||||||
return status.HTTP_404_NOT_FOUND, "not_found", message, None
|
return status.HTTP_404_NOT_FOUND, "not_found", message, None
|
||||||
if "permission denied" in low or "insufficient privilege" in low:
|
if any(error in low for error in PG_403_ERRORS):
|
||||||
return status.HTTP_403_FORBIDDEN, "access_denied", message, None
|
return status.HTTP_403_FORBIDDEN, "access_denied", message, None
|
||||||
|
|
||||||
if isinstance(exc, IntegrityError):
|
if isinstance(exc, IntegrityError):
|
||||||
if "unique" in low or "duplicate" in low:
|
if any(error in low for error in PG_409_ERRORS):
|
||||||
return status.HTTP_409_CONFLICT, "conflict", message, None
|
return status.HTTP_409_CONFLICT, "conflict", message, None
|
||||||
if "foreign key" in low:
|
if any(error in low for error in PG_400_ERRORS):
|
||||||
return status.HTTP_409_CONFLICT, "conflict", message, None
|
|
||||||
if "violates check constraint" in low:
|
|
||||||
return status.HTTP_400_BAD_REQUEST, "db_error", message, None
|
return status.HTTP_400_BAD_REQUEST, "db_error", message, None
|
||||||
return status.HTTP_400_BAD_REQUEST, "db_error", message, None
|
return status.HTTP_400_BAD_REQUEST, "db_error", message, None
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user