mirror of
https://github.com/yogeshojha/rengine.git
synced 2026-10-19 14:51:31 +02:00
The comment rule in CLAUDE.md is one line maximum, never narration: no multi-line blocks, no module-header explainers, no sentences that explain or justify the code to the reader. The tree had drifted a long way from it, and most of the drift is mine. Removed: 232 lines of multi-line comment blocks 203 single-line comments that argued instead of stating 154 comments joined by so/because/otherwise/rather-than 143 narrating comments in the frontend 112 test docstrings (the test name already says what it asserts) 25 test module docstrings and 25 decorative section dividers Collapsed: 324 multi-line docstrings to one factual line 271 one-line docstrings that had a justifying clause bolted on Kept: functional directives, section dividers, short field labels, alembic revision headers, and the eight FastAPI route docstrings that render in /docs. Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs. 284 python tests and 34 frontend tests pass, ruff and ruff-format clean across ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""Turn a value the database refuses into a 400 rather than an unhandled 500."""
|
|
|
|
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, which cannot be stored.",
|
|
}
|
|
_FALLBACK = "A value in the request is not valid for the field it was sent to."
|
|
|
|
|
|
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)},
|
|
)
|