pdf-quiz-generator/backend/app/api/errors.py
Daniel e72cdd6716 feat: a versioned API, refresh tokens, and an end-to-end stack that found four bugs
**The API.** Every route now lives under `/api/v1`, with `/api/...` rewritten
onto it — one route, two spellings, so they cannot drift and the OpenAPI
document describes each endpoint once. Errors carry an `error` object with a
stable code, one human sentence and, for a validation failure, the fields that
were wrong; `detail` is untouched so nothing that reads it breaks. The whole
surface — 320 routes, their parameters and their status codes — is checked in
as `backend/tests/api-contract.json`, and a test fails on any difference,
naming the routes that moved. `docs/api.md` is the contract in prose.

**Refresh tokens**, so an app can stay signed in without keeping a password.
Rows rather than signatures: listable, withdrawable, stored as hashes, rotated
on every use. A spent token coming back ends the whole session, because a theft
and a replay look identical from the server and the safe reading is the unsafe
one. A browser is not given one — it has nowhere to put it and a person to ask.

**An end-to-end stack**: `docker-compose.test.yml` with its own Postgres and
Redis, `e2e/seed.py` for the smallest world the tests name, and Playwright with
five projects — desktop, iPhone, Pixel, iPad and a browserless API project.
Devices because every bug reported this week was a phone bug found by a person
looking at a screenshot; a desktop-only suite would have passed through all of
them. Forty tests, five clean runs.

It found four things in its first hour:

- **A fresh deploy could not start.** `create_all()` ran before
  `CREATE EXTENSION vector`, so any database that had never had pgvector
  installed died on the first table with a vector column. Invisible here
  because this one has had the extension for a year.
- **A figure in a published article was a 404 for everyone but an admin.**
  Media in the library is nobody's to read by default, and nothing made an
  exception for a drawing an article actually shows — so every illustration
  added this week was an empty box for every real user.
- **Every rate limit was one bucket for the whole site.** The backend saw
  nginx's address for every request, so ten bad passwords from anybody locked
  out everybody, and no log line could say who. nginx now takes the real
  address from the proxy and overwrites the header on the way in; uvicorn runs
  with --proxy-headers.
- **The reading page's breakpoints disagreed** — 1150px in the component,
  820px in the stylesheet. Between them the menu button claimed the contents
  drawer and then toggled a class on a rail that was still in the layout: the
  contents did not open and the site menu did not either. The button was dead
  on every tablet.

And two smaller ones: the login limiter counted successful sign-ins, so eleven
people behind one hospital NAT locked each other out — it is cleared by a
correct password now; and `/uploads/{path}` served GET and HEAD from one route
with one operation id, which makes every OpenAPI client generator refuse the
document.

The first admin's password is generated and printed once at first start when
`DEFAULT_ADMIN_PASSWORD` is blank, rather than the account not existing:
`docker compose logs backend | grep -A3 "FIRST ADMIN"`.

CI (`.forgejo/workflows/tests.yml`) runs the backend suite, the contract, the
frontend suite and the build on every push to dev, main or master, and the
end-to-end stack on those branches and on pull requests into them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TqXevQJhxFrM7jJg82cgZN
2026-09-13 01:23:38 +02:00

99 lines
4.4 KiB
Python

"""One shape for every failure the API reports.
FastAPI's default is two shapes: `{"detail": "Quiz not found"}` for a raised
HTTPException and `{"detail": [ {...}, {...} ]}` for a validation failure. A
browser can squint at both — this repository's frontend has a helper that does
exactly that — but a client written against the API has to guess which it got,
and a `detail` that is sometimes a sentence and sometimes a list of objects is
not a contract anybody can code against.
So every error carries an `error` object as well: a stable machine-readable
code, one human sentence, and — for a validation failure — which fields were
wrong and why. `detail` is left exactly as it was, because removing it would
break every call site in the web app for no benefit to anyone.
"""
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
#: Status code to code word. A client switches on the word, not the number:
#: the number says what happened to the request, the word says what happened.
CODES = {
status.HTTP_400_BAD_REQUEST: "bad_request",
status.HTTP_401_UNAUTHORIZED: "unauthenticated",
status.HTTP_402_PAYMENT_REQUIRED: "payment_required",
status.HTTP_403_FORBIDDEN: "forbidden",
status.HTTP_404_NOT_FOUND: "not_found",
status.HTTP_405_METHOD_NOT_ALLOWED: "method_not_allowed",
status.HTTP_409_CONFLICT: "conflict",
status.HTTP_413_REQUEST_ENTITY_TOO_LARGE: "too_large",
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE: "unsupported_media_type",
status.HTTP_422_UNPROCESSABLE_ENTITY: "invalid_request",
status.HTTP_429_TOO_MANY_REQUESTS: "rate_limited",
status.HTTP_500_INTERNAL_SERVER_ERROR: "server_error",
status.HTTP_502_BAD_GATEWAY: "upstream_failed",
status.HTTP_503_SERVICE_UNAVAILABLE: "unavailable",
}
def code_for(status_code: int) -> str:
if status_code in CODES:
return CODES[status_code]
return "client_error" if status_code < 500 else "server_error"
def _sentence(detail) -> str:
"""One line a person could be shown, whatever the detail turned out to be."""
if isinstance(detail, str):
return detail
if isinstance(detail, list) and detail:
first = detail[0]
if isinstance(first, dict) and first.get("msg"):
where = ".".join(str(part) for part in first.get("loc", []) if part != "body")
return f"{where}: {first['msg']}" if where else str(first["msg"])
return "Request failed"
def _fields(errors) -> list[dict]:
"""Which inputs were wrong, in the caller's own terms."""
out = []
for error in errors or []:
location = [str(part) for part in error.get("loc", [])]
# "body" is where it came from, not what was wrong with it.
name = ".".join(part for part in location[1:] or location)
out.append({"field": name, "message": error.get("msg", "Invalid value"),
"type": error.get("type", "invalid")})
return out
def envelope(status_code: int, message: str, *, code: str | None = None,
fields: list[dict] | None = None, detail=None) -> dict:
body = {"detail": detail if detail is not None else message,
"error": {"code": code or code_for(status_code), "message": message}}
if fields:
body["error"]["fields"] = fields
return body
def install(app: FastAPI) -> None:
@app.exception_handler(StarletteHTTPException)
async def http_error(request: Request, exc: StarletteHTTPException):
return JSONResponse(
status_code=exc.status_code,
content=envelope(exc.status_code, _sentence(exc.detail), detail=exc.detail),
headers=getattr(exc, "headers", None),
)
@app.exception_handler(RequestValidationError)
async def validation_error(request: Request, exc: RequestValidationError):
errors = exc.errors()
# Pydantic puts an unserialisable exception object in `ctx` for some
# error types; the default handler drops it and so must this one, or
# reporting the error becomes its own 500.
clean = [{k: v for k, v in error.items() if k != "ctx"} for error in errors]
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=envelope(422, _sentence(clean), code="invalid_request",
fields=_fields(clean), detail=clean),
)