Deep-review round 3 of bug hunt 2026-09.
- get_or_404 / owned_or_404 answer a structured {code: "not_found", reason: <what>}
(errors.notFound.* in HU+EN, generic fallback). The structured-404 fix of round 2 had gone into the
ad-hoc `_own_playlist` (CLAUDE.md's "Not" pattern) while the shared helpers stayed string-only, so
views, tags, pins, Plex playlists, invites and users still 404'd in English. `_own_playlist` now
goes through owned_or_404; admin's hand-rolled user lookups use get_or_404.
- A move into a list that is gone names the TARGET (reason "move_target") — "this playlist doesn't
exist any more" was read as the list on screen, which still exists.
- Every refusal in the playlists router is a {code: "playlist", reason} (playlists.errors.* HU+EN)
instead of a bare English string shown verbatim in the Hungarian UI.
- `req` / `uploadReq` mark the errors they already put on screen (the error dialog, the connection
notice, the 401 sign-out), and notifyActionError skips those. The watched toggles, the saved views
and a playlist delete toasted a fault the dialog was already showing; Playlists' two hand-written
status filters now go through the same check.
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
"""Route-layer shared helpers for the API contract (R6 S1a).
|
|
|
|
Small, dependency-light guards that several route modules had hand-rolled with subtle
|
|
drift. Keeping them here (imported by the route modules) means the "fetch a row or 404"
|
|
and "fetch a row this user owns or 404" idioms have one definition, one status code, and
|
|
one existence-leak policy.
|
|
"""
|
|
|
|
from typing import Any, TypeVar
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db import Base
|
|
from app.models import User
|
|
|
|
M = TypeVar("M", bound=Base)
|
|
|
|
|
|
def not_found(what: str = "generic") -> HTTPException:
|
|
"""The 404 for a row that is gone — structured, so the bilingual UI says it in the reader's
|
|
language (`errors.notFound.<what>`, generic fallback). This is the 404 a user actually meets: the
|
|
thing was deleted in another tab or on another device. It used to be a bare English string
|
|
("Unknown view", "Not found"), which the Hungarian UI toasted verbatim."""
|
|
return HTTPException(status_code=404, detail={"code": "not_found", "reason": what})
|
|
|
|
|
|
def get_or_404(db: Session, model: type[M], id_: Any, *, what: str = "generic") -> M:
|
|
"""Fetch a row by primary key or raise 404. Replaces the `db.get(Model, id); if None: 404`
|
|
idiom repeated across routes."""
|
|
obj = db.get(model, id_)
|
|
if obj is None:
|
|
raise not_found(what)
|
|
return obj
|
|
|
|
|
|
def owned_or_404(
|
|
db: Session,
|
|
model: type[M],
|
|
id_: Any,
|
|
user: User,
|
|
*,
|
|
owner_attr: str = "user_id",
|
|
what: str = "generic",
|
|
) -> M:
|
|
"""Fetch a row by primary key that `user` owns, or raise 404. A row owned by someone
|
|
else 404s (NOT 403) on purpose — a 403 would confirm the id exists to a stranger.
|
|
Generalizes the near-identical per-route ownership helpers (e.g. plex's
|
|
`_own_playlist_or_404`, the saved-views owner check)."""
|
|
obj = get_or_404(db, model, id_, what=what)
|
|
if getattr(obj, owner_attr) != user.id:
|
|
raise not_found(what)
|
|
return obj
|