FastAPI exception classes and handlers producing OpenAI‑compatible error responses.
# Source: https://github.com/520mmxx/grok2api-merged | License: MIT | Copyright 2026 Chenyme
# Provided via justclaw.it.com; keep this notice with the code.
"""
全局异常处理 - OpenAI 兼容错误格式
"""
from typing import Any, Optional
from enum import Enum
from fastapi import Request, HTTPException
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from app.core.logger import logger
# ============= 错误类型 =============
class ErrorType(str, Enum):
"""OpenAI 错误类型"""
INVALID_REQUEST = "invalid_request_error"
AUTHENTICATION = "authentication_error"
PERMISSION = "permission_error"
NOT_FOUND = "not_found_error"
RATE_LIMIT = "rate_limit_error"
SERVER = "server_error"
SERVICE_UNAVAILABLE = "service_unavailable_error"
# ==========
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key