Files
rengine/api/app/core/db_errors.py
T
Yogesh OjhaandClaude Fable 5.1 0a69f3201c copy: state the fact, delete the explanation
Repo-wide sweep of every user-facing string: frontend components, routes,
config and stores; API details and route docs; shared definitions and
models; stage config help; report prose; MCP, toolbox, interest and
connector text; notification and worker messages.

Descriptions that explained, reassured, justified or restated their title
are deleted. Confirm dialogs name what is removed and stop. Failed actions
read "<Noun> not <verbed>", successes "<Noun> <verbed>". No reader address,
no product-as-subject, no em-dashes or parentheticals in prose.

Mirrors updated on both sides: EPSS band help, compare refusal reasons,
interest labels. Four tests asserting on old wording follow the new text.
The rules are condensed into CLAUDE.md under Writing.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SK5o8xR96cbSnAfjYSTPSe
2026-09-12 11:35:07 +05:30

43 lines
1.3 KiB
Python

"""Map a Postgres data exception to a 400."""
from fastapi import Request
from fastapi.responses import JSONResponse
from sqlalchemy.exc import DBAPIError
from starlette.status import HTTP_400_BAD_REQUEST
from shared.logging import get_logger
logger = get_logger(__name__)
_DATA_EXCEPTION = "22"
_MESSAGES = {
"22001": "A value in the request is longer than the field allows.",
"22003": "A number in the request is outside the range the field allows.",
"22021": "A value in the request contains a NUL byte.",
}
_FALLBACK = "A value in the request is not valid for its field."
def _sqlstate(exc: DBAPIError) -> str | None:
orig = exc.orig
inner = getattr(orig, "__cause__", None) or orig
state = getattr(inner, "sqlstate", None)
return state if isinstance(state, str) else None
async def data_exception_handler(request: Request, exc: DBAPIError) -> JSONResponse:
state = _sqlstate(exc)
if not state or not state.startswith(_DATA_EXCEPTION):
raise exc
logger.info(
"rejected unstorable value",
path=request.url.path,
method=request.method,
sqlstate=state,
)
return JSONResponse(
status_code=HTTP_400_BAD_REQUEST,
content={"detail": _MESSAGES.get(state, _FALLBACK)},
)