From de000905b3e5a6213175a45b9317bae211889fdb Mon Sep 17 00:00:00 2001 From: Raykov-MS Date: Thu, 14 May 2026 16:04:39 +0300 Subject: [PATCH] Add Constants --- api/src/core/CONSTANTS.py | 31 ++++++++++++++++++++++++++++++ api/src/core/exception_handlers.py | 31 ++++++++++-------------------- 2 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 api/src/core/CONSTANTS.py diff --git a/api/src/core/CONSTANTS.py b/api/src/core/CONSTANTS.py new file mode 100644 index 0000000..3cbaac0 --- /dev/null +++ b/api/src/core/CONSTANTS.py @@ -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:", +) diff --git a/api/src/core/exception_handlers.py b/api/src/core/exception_handlers.py index 4559291..b1eaa97 100644 --- a/api/src/core/exception_handlers.py +++ b/api/src/core/exception_handlers.py @@ -9,21 +9,14 @@ from src.core.errors import ( UsernameConflictException, ValidationException, ) - - -VALIDATION_PREFIXES = ( - "computed_field:", - "normative_field:", - "unknown_column:", - "key_field:", - "structural_field:", - "bad_column_format:", - "bad_booking_year:", - "bad_booking_quarter:", - "unsupported_scope:", +from src.core.CONSTANTS import ( + VALIDATION_PREFIXES, + PG_404_ERRORS, + PG_409_ERRORS, + PG_400_ERRORS, + PG_403_ERRORS, ) - def _error_payload(code: str | int, message: str, field: str | None = None) -> dict: """Формирует структурированный payload ошибки по спеке.""" 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): code, field = _parse_prefixed_validation_error(message) return status.HTTP_422_UNPROCESSABLE_ENTITY, code, message, field - if low.startswith("role_not_allowed:") or low.startswith("window_closed:"): - return status.HTTP_403_FORBIDDEN, "access_denied", message, None - if "не существует" in low or "не принадлежит" in low or "not found" in low: + if any(error in low for error in PG_404_ERRORS): 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 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 - if "foreign key" in low: - return status.HTTP_409_CONFLICT, "conflict", message, None - if "violates check constraint" in low: + if any(error in low for error in PG_400_ERRORS): return status.HTTP_400_BAD_REQUEST, "db_error", message, None return status.HTTP_400_BAD_REQUEST, "db_error", message, None