Spaces:
Running on Zero
Running on Zero
File size: 1,330 Bytes
8604d66 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | """Typed errors that the API layer converts into structured JSON responses."""
from __future__ import annotations
from typing import Any, Optional
class PCBError(Exception):
"""An error with a stable machine-readable code and an HTTP status."""
status_code = 400
def __init__(self, code: str, message: str, *, status_code: Optional[int] = None,
details: Any = None) -> None:
super().__init__(message)
self.code = code
self.message = message
self.details = details
if status_code is not None:
self.status_code = status_code
def to_dict(self) -> dict:
body = {"code": self.code, "message": self.message}
if self.details is not None:
body["details"] = self.details
return {"error": body}
class NotFoundError(PCBError):
status_code = 404
class ConflictError(PCBError):
status_code = 409
class RequirementsError(PCBError):
status_code = 422
class LLMError(PCBError):
"""Raised by LLM providers. `retryable` marks transient failures."""
status_code = 502
def __init__(self, code: str, message: str, *, retryable: bool = False, **kw: Any) -> None:
super().__init__(code, message, **kw)
self.retryable = retryable
|