Spaces:
Running
Running
File size: 8,159 Bytes
a55ea57 2f1e7ff a55ea57 5583f46 6663ad4 a55ea57 2f1e7ff a55ea57 6663ad4 5583f46 a55ea57 | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | """Structured, agent-readable HTTP error responses."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from uuid import uuid4
from fastapi import FastAPI, Request
from fastapi.encoders import jsonable_encoder
from src.services.redaction import redact
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, JsonValue
from starlette.exceptions import HTTPException as StarletteHTTPException
from src.lib.embeddings.profile import EmbeddingProfileError
from src.services.brain_identity import BrainNotFoundError, BrainAlreadyExistsError, InvalidBrainIdError
class ErrorInfo(BaseModel):
"""Stable machine-readable information attached to every API error."""
code: str = Field(description="Stable error code for programmatic handling.")
message: str = Field(description="Short human-readable error summary.")
resolution: str = Field(description="Concrete guidance for resolving the error.")
request_id: str = Field(description="Identifier shared with the X-Request-ID header.")
class ErrorResponse(BaseModel):
"""Additive error envelope that preserves FastAPI's legacy detail field."""
detail: JsonValue = Field(description="Original FastAPI error detail value.")
error: ErrorInfo
_STATUS_DEFAULTS: dict[int, tuple[str, str, str]] = {
400: (
"INVALID_REQUEST",
"The request is invalid.",
"Correct the request parameters and retry.",
),
401: (
"AUTH_INVALID",
"Authentication failed.",
"Send a valid BrainPAT or Bearer token for the selected brain.",
),
403: (
"FORBIDDEN",
"The operation is not permitted.",
"Use credentials with permission for this operation.",
),
404: (
"RESOURCE_NOT_FOUND",
"The requested resource was not found.",
"Check the path, identifier, and selected brain before retrying.",
),
405: (
"METHOD_NOT_ALLOWED",
"The HTTP method is not allowed for this resource.",
"Use one of the methods documented in the OpenAPI specification.",
),
406: (
"BRAIN_NOT_FOUND",
"The selected brain could not be resolved.",
"Create the brain or provide the correct X-Brain-ID value.",
),
422: (
"VALIDATION_ERROR",
"Request validation failed.",
"Correct the fields listed in detail and retry.",
),
429: (
"RATE_LIMITED",
"The request rate limit was exceeded.",
"Wait before retrying and use bounded exponential backoff.",
),
500: (
"INTERNAL_ERROR",
"The service could not complete the request.",
"Retry only when safe; contact support with the request ID if it persists.",
),
501: (
"not_implemented",
"The operation is not implemented.",
"Use a supported operation or consult the release notes for availability.",
),
503: (
"SERVICE_UNAVAILABLE",
"A required service is temporarily unavailable.",
"Retry with bounded exponential backoff after the dependency recovers.",
),
}
def _request_id(request: Request) -> str:
return (
request.headers.get("X-Request-ID")
or request.headers.get("X-Trace-ID")
or str(uuid4())
)
def error_response(
request: Request,
*,
status_code: int,
detail: Any,
code: str | None = None,
message: str | None = None,
resolution: str | None = None,
extra: dict[str, Any] | None = None,
headers: Mapping[str, str] | None = None,
) -> JSONResponse:
"""Create the common JSON envelope while retaining the legacy detail value."""
default_code, default_message, default_resolution = _STATUS_DEFAULTS.get(
status_code,
_STATUS_DEFAULTS[500],
)
request_id = _request_id(request)
content: dict[str, Any] = {
"detail": detail,
"error": {
"code": code or default_code,
"message": message or default_message,
"resolution": resolution or default_resolution,
"request_id": request_id,
},
}
if extra:
content.update(extra)
response_headers = dict(headers or {})
response_headers["X-Request-ID"] = request_id
return JSONResponse(
status_code=status_code,
content=content,
headers=response_headers,
)
def install_error_handlers(app: FastAPI) -> None:
"""Install handlers for framework, validation, and unexpected exceptions."""
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
return error_response(
request,
status_code=exc.status_code,
detail=exc.detail,
headers=exc.headers,
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request,
exc: RequestValidationError,
):
return error_response(
request,
status_code=422,
# Pydantic puts exception objects in ctx.error; input may contain
# credentials and is unnecessary for actionable field diagnostics.
detail=redact(jsonable_encoder(
[{k: v for k, v in error.items() if k != "input"} for error in exc.errors()],
custom_encoder={ValueError: str, TypeError: str, Exception: str},
)),
)
@app.exception_handler(BrainNotFoundError)
async def missing_brain_handler(request: Request, _exc: BrainNotFoundError):
return error_response(
request, status_code=404, detail="Brain not found", code="BRAIN_NOT_FOUND",
resolution="Create the brain explicitly or select a registered brain.",
)
@app.exception_handler(BrainAlreadyExistsError)
async def duplicate_brain_handler(request: Request, _exc: BrainAlreadyExistsError):
return error_response(
request, status_code=409, detail="Brain already exists", code="BRAIN_ALREADY_EXISTS",
message="A brain with this identifier already exists.",
resolution="Select the existing brain or create a different identifier.",
)
@app.exception_handler(InvalidBrainIdError)
async def invalid_brain_handler(request: Request, exc: InvalidBrainIdError):
return error_response(request, status_code=400, detail=str(exc), code=exc.code)
@app.exception_handler(EmbeddingProfileError)
async def embedding_profile_exception_handler(request: Request, exc: EmbeddingProfileError):
return error_response(
request,
status_code=503,
detail=str(exc),
code="EMBEDDING_PROFILE_MISMATCH",
message="The configured embedding profile does not match this index.",
resolution="Use a new empty brain/index or explicitly re-index its source texts with the configured model.",
)
@app.exception_handler(Exception)
async def unexpected_exception_handler(request: Request, _exc: Exception):
return error_response(
request,
status_code=500,
detail="Internal server error",
)
COMMON_ERROR_RESPONSES = {
400: {"model": ErrorResponse, "description": "Invalid request"},
401: {"model": ErrorResponse, "description": "Authentication failed"},
403: {"model": ErrorResponse, "description": "Operation forbidden"},
404: {"model": ErrorResponse, "description": "Resource not found"},
405: {"model": ErrorResponse, "description": "Method not allowed"},
406: {"model": ErrorResponse, "description": "Brain not found"},
422: {"model": ErrorResponse, "description": "Validation failed"},
429: {"model": ErrorResponse, "description": "Rate limit exceeded"},
500: {"model": ErrorResponse, "description": "Internal service error"},
501: {"model": ErrorResponse, "description": "Operation not implemented"},
503: {"model": ErrorResponse, "description": "Dependency unavailable"},
}
|