Files
microfish/backend/app/utils/api_errors.py
Kunthawat Greethong 8b84378fe1 feat: SaaS foundation for CrowdSight
Elevate MiroFish/CrowdSight from single-container dev to a SaaS foundation:

- Local memory backend (Zep-compatible): memory services/models, local graph
  builder + updater, AgentActivity seam, import-boundary isolation; Zep stays
  default, local is opt-in behind MEMORY_BACKEND. Semantic parity not yet proven.
- Durable product persistence: projects/simulations/reports schema (migration
  0007) + tenant/owner-scoped ProductRepository + dual-write + scoped_project
  read-first + ArtifactStore abstraction; durable JobQueue + worker.py.
- SaaS hardening: durable RateLimiter (wired to login), UsageService (LLM
  accounting), redacted AuditService, idempotency, CORS allowlist, safe API
  errors, single-use PasswordResetService + endpoints (covers invite-pending).
- Exactly 3 roles (super_admin/admin/user) with tenant authz policy.
- Admin UI: GET/POST/PATCH /api/admin/users + GET/PUT /api/admin/settings
  (super-admin only, encrypted/masked); AdminView.vue + SettingsView.vue with
  admin/super-admin route guards, th/en i18n.
- Production deploy topology: multi-stage Dockerfile (frontend build + gunicorn
  wsgi + nginx SPA-proxy + supervisord worker), backend/wsgi.py, gunicorn dep.

Backend 197 passed; frontend 10 tests + build green. ruff unavailable (gap).
No commit of credentials; secrets handled via env/.env.example.
Deferred: Zep semantic A/B parity, object storage cutover, mobile QA, EasyPanel
container build of deploy topology.
2026-08-31 13:05:21 +07:00

44 lines
1.2 KiB
Python

"""Structured, localized API error contracts.
The exception text is intentionally never serialized. Route handlers can raise
``ApiError`` with a stable code and translation key; Flask integration resolves
the message through the current locale at the response boundary.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Callable, Mapping
Translator = Callable[..., str]
@dataclass
class ApiError(Exception):
"""A safe application error that can cross the HTTP boundary."""
code: str
status_code: int
message_key: str
params: Mapping[str, object] = field(default_factory=dict)
def __post_init__(self):
Exception.__init__(self, self.code)
def to_payload(self, translate: Translator) -> dict[str, object]:
return {
"success": False,
"error_code": self.code,
"message": translate(self.message_key, **dict(self.params)),
}
def internal_error_payload(translate: Translator) -> dict[str, object]:
"""Return a generic internal error without accepting exception details."""
return ApiError(
code="internal_error",
status_code=500,
message_key="api.internalError",
).to_payload(translate)