Heuristic:4thfever Cultivation world simulator Unified Response Contract
| Knowledge Sources | |
|---|---|
| Domains | API_Design, Backend |
| Last Updated | 2026-10-22 16:00 GMT |
Overview
All public API responses must use `ok_response()` for success and `raise_public_error()` for errors, ensuring a uniform `{ok, data}` / `{code, message, details}` contract.
Description
The public API contract enforces a consistent response shape across all query and command endpoints. Success responses wrap data in `{"ok": True, "data": ...}`. Error responses raise an `HTTPException` with `detail={"code": ..., "message": ..., "details": ...}`. This pattern eliminates ad-hoc response construction and makes error handling predictable for external agents consuming the API.
Usage
Use this heuristic whenever adding a new public API endpoint or modifying an existing one. Never return raw dicts or bare HTTP exceptions — always route through these helpers.
The Insight (Rule of Thumb)
- Action: Use `ok_response(data)` for all successful query/command responses.
- Action: Use `raise_public_error(status_code=..., code=..., message=..., details=...)` for all error cases.
- Value: Success shape: `{"ok": True, "data": <payload>}`. Error shape: `{"code": <str>, "message": <str>, "details": <dict>}`.
- Trade-off: Slightly more verbose than bare returns, but guarantees consistent client-side error parsing.
Reasoning
External agents (such as Claw integrations) need a predictable contract to parse responses and handle errors. Without a unified pattern, each endpoint might return different error shapes, forcing clients to special-case every route. The `ok_response` / `raise_public_error` pair ensures the contract is enforced at the helper level — you cannot accidentally return an unstructured error.
Code Evidence
Unified contract from `src/server/services/public_api_contract.py:8-26`:
def ok_response(data: Any) -> dict[str, Any]:
return {"ok": True, "data": data}
def raise_public_error(
*,
status_code: int,
code: str,
message: str,
details: dict[str, Any] | None = None,
) -> None:
raise HTTPException(
status_code=status_code,
detail={
"code": code,
"message": message,
"details": details or {},
},
)
Usage in query router `src/server/api/public_v1/query.py:43`:
return ok_response(query_service.get_runtime_status())
Usage in game queries `src/server/services/game_queries.py:231`:
raise_public_error(
status_code=404, code="not_found",
message="Avatar not found", details={"avatar_id": avatar_id}
)