异常
处理 TypeSafe API 错误、速率限制、连接失败与超时。
基础异常
typesafe_sdk.TypeSafeError
基类:Exception
SDK 失败时抛出的基础异常。
HTTP 错误
typesafe_sdk.TypeSafeAPIError
带有响应体与请求元数据的未成功 HTTP 响应。
status
instance-attribute
python
status = statusHTTP 响应状态码。
body
instance-attribute
python
body = body服务器的 JSON 错误体或纯文本响应内容;响应体为空时则为 None。
headers
instance-attribute
python
headers = headersHTTP 响应头。
endpoint
instance-attribute
python
endpoint = endpoint可用时为请求的方法与 URL,不包含凭据、查询参数或片段。
request_id
property
request_id
:
str
|
None
响应头 x-typesafe-request-id 的值,缺失时为 None。
typesafe_sdk.TypeSafeBadRequestError
请求无效(400)。
typesafe_sdk.TypeSafeAuthenticationError
身份验证失败(401)。
typesafe_sdk.TypeSafePermissionDeniedError
访问被拒绝(403)。
typesafe_sdk.TypeSafeNotFoundError
未找到资源(404)。
typesafe_sdk.TypeSafeUnprocessableEntityError
请求未通过服务器校验(422)。
typesafe_sdk.TypeSafeRateLimitError
超出速率限制(429)。
retry_after_ms
instance-attribute
python
retry_after_ms = parse_retry_after(headers)服务器要求的等待时间(毫秒);不可用时则为 None。
typesafe_sdk.TypeSafeInternalServerError
服务器处理请求失败(5xx)。
连接错误
typesafe_sdk.TypeSafeAPIConnectionError
基类:TypeSafeError, ConnectionError
请求失败且未收到 HTTP 响应。
typesafe_sdk.TypeSafeAPITimeoutError
基类:TypeSafeAPIConnectionError, TimeoutError
请求超出了配置的超时时间。
timeout
instance-attribute
python
timeout = timeout请求所使用的超时设置,以秒为单位,或为 httpx2.Timeout。
响应校验
typesafe_sdk.TypeSafeAPIResponseValidationError
HTTP 响应成功,但其响应体缺失所需数据或所需数据的结构无效。
field_path
instance-attribute
python
field_path = field_path指向出错字段的点分路径,例如 answers.tone.confidence。
args
instance-attribute
python
args = (
status,
body,
headers,
field_path,
endpoint,
)