Spaces:
Build error

File size: 1,401 Bytes
7e8cd1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9ad0c
7e8cd1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
"""Standard error envelope for cross-service communication.

Every non-2xx response from this service uses this shape so NestJS can
distinguish error categories and decide whether to retry.
"""
from enum import Enum
from pydantic import BaseModel
from typing import Optional

from app.middleware.correlation import get_correlation_id


class ErrorCode(str, Enum):
    """Stable error codes — never rename, only add new ones."""
    VALIDATION_ERROR = "VALIDATION_ERROR"
    AUTH_INVALID_KEY = "AUTH_INVALID_KEY"
    IDEMPOTENCY_CONFLICT = "IDEMPOTENCY_CONFLICT"
    DOCUMENT_NOT_FOUND = "DOCUMENT_NOT_FOUND"
    FORBIDDEN = "FORBIDDEN"
    INGEST_FAILED = "INGEST_FAILED"
    LLM_UNAVAILABLE = "LLM_UNAVAILABLE"
    PDF_GENERATION_FAILED = "PDF_GENERATION_FAILED"
    INTERNAL_ERROR = "INTERNAL_ERROR"


class ErrorEnvelope(BaseModel):
    code: ErrorCode
    message: str
    retryable: bool = False
    correlation_id: Optional[str] = None
    details: Optional[dict] = None


def make_error(
    code: ErrorCode,
    message: str,
    *,
    retryable: bool = False,
    details: dict | None = None,
) -> dict:
    """Build an error dict suitable for JSONResponse / HTTPException detail."""
    return ErrorEnvelope(
        code=code,
        message=message,
        retryable=retryable,
        correlation_id=get_correlation_id(),
        details=details,
    ).model_dump(exclude_none=True)