S1: unify error responses + id parsing (responses.py) across notes.py
M9 section S1, commit 2. Add src/thoughtsync/responses.py — the app's single
JSON-error shape and the two guards that pair with it:
- json_error(message, status): the one ({"error": ...}, code) builder
- not_found(): the standard 404, by far the most common note-route error
- parse_uuid(raw): parse a path/body id, None on malformed → pair with not_found()
notes.py adopts them everywhere: ~25 hand-built `jsonify({"error":"not found"}),404`
collapse to not_found(); ~20 other error returns to json_error(...); ~13 repeated
`try: uuid.UUID(x) except: ...` blocks to parse_uuid(). Behavior-preserving — same
bodies and status codes, one definition. jsonify stays for the success responses.
Other blueprints (auth, labels, saved_filters, sync, settings_api) adopt the same
helpers in their own M9 sections.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FRgehjoz7Yv8LkUfADxACm
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
from quart import jsonify
|
||||
|
||||
# The app's single JSON error shape + the two guards that pair with it, so every
|
||||
# blueprint returns errors and parses path ids the same way instead of hand-building
|
||||
# `jsonify({"error": ...}), code` and try/except uuid blocks at ~35 call sites.
|
||||
|
||||
|
||||
def json_error(message: str, status: int):
|
||||
"""A JSON error body + status: ({"error": message}, status)."""
|
||||
return jsonify({"error": message}), status
|
||||
|
||||
|
||||
def not_found():
|
||||
"""404 with the standard body — by far the most common error in the note routes."""
|
||||
return json_error("not found", 404)
|
||||
|
||||
|
||||
def parse_uuid(raw: object) -> uuid.UUID | None:
|
||||
"""Parse a path/body UUID, returning None on anything malformed. Pair with
|
||||
not_found() for the ubiquitous 'bad id in the URL → 404' guard."""
|
||||
try:
|
||||
return uuid.UUID(str(raw))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
Reference in New Issue
Block a user