跳转至

errors

feishu.gateway.errors

GatewayRequestError

Bases: Exception

可映射为 HTTP 响应的网关请求校验错误。

源代码位于: feishu/gateway/errors.py
Python
class GatewayRequestError(Exception):
    r"""可映射为 HTTP 响应的网关请求校验错误。"""

    def __init__(self, message: str, status_code: int = 400) -> None:
        super().__init__(message, status_code)
        self.message = message
        self.status_code = status_code

error_response

Python
error_response(message: str, *, status_code: int) -> JSONResponse

返回紧凑的 JSON 错误响应。

源代码位于: feishu/gateway/errors.py
Python
def error_response(message: str, *, status_code: int) -> JSONResponse:
    r"""返回紧凑的 JSON 错误响应。"""
    return JSONResponse({"msg": message}, status_code=status_code)

read_json_object async

Python
read_json_object(request: Request) -> dict[str, Any]

读取并校验请求体必须是 JSON 对象。

源代码位于: feishu/gateway/errors.py
Python
async def read_json_object(request: Request) -> dict[str, Any]:
    r"""读取并校验请求体必须是 JSON 对象。"""
    try:
        payload = await request.json()
    except ValueError as exc:
        raise GatewayRequestError("invalid json") from exc
    if not isinstance(payload, dict):
        raise GatewayRequestError("request body must be a JSON object")
    return payload

feishu_error_response

Python
feishu_error_response(error: FeishuError) -> JSONResponse

把 SDK 错误映射为网关 HTTP 响应,同时避免泄露凭据。

源代码位于: feishu/gateway/errors.py
Python
def feishu_error_response(error: FeishuError) -> JSONResponse:
    r"""把 SDK 错误映射为网关 HTTP 响应,同时避免泄露凭据。"""
    status_code = 502
    if isinstance(error, FeishuPermissionError):
        status_code = 403
    elif isinstance(error, FeishuAuthError):
        status_code = 502
    elif isinstance(error, FeishuRateLimitError):
        status_code = 429
    elif isinstance(error, FeishuServerError):
        status_code = 502
    elif isinstance(error, FeishuApiError):
        status_code = 400
    payload: dict[str, Any] = {"msg": error.message, "code": error.code}
    if error.log_id:
        payload["log_id"] = error.log_id
    return JSONResponse(payload, status_code=status_code)