🛡️TypeSafe 中文文档
原文档 ↗

异常

处理 TypeSafe API 错误、速率限制、连接失败与超时。

基础异常

typesafe_sdk.TypeSafeError

基类:Exception

SDK 失败时抛出的基础异常。

HTTP 错误

typesafe_sdk.TypeSafeAPIError

基类:TypeSafeError

带有响应体与请求元数据的未成功 HTTP 响应。

status

instance-attribute

python
status = status

HTTP 响应状态码。

body

instance-attribute

python
body = body

服务器的 JSON 错误体或纯文本响应内容;响应体为空时则为 None。

headers

instance-attribute

python
headers = headers

HTTP 响应头。

endpoint

instance-attribute

python
endpoint = endpoint

可用时为请求的方法与 URL,不包含凭据、查询参数或片段。

request_id

property


  
    request_id
  

  
    :
  

   

  
    
      str
    
  

   

  
    |
  

   

  
    None
  

  

响应头 x-typesafe-request-id 的值,缺失时为 None。

typesafe_sdk.TypeSafeBadRequestError

基类:TypeSafeAPIError

请求无效(400)。

typesafe_sdk.TypeSafeAuthenticationError

基类:TypeSafeAPIError

身份验证失败(401)。

typesafe_sdk.TypeSafePermissionDeniedError

基类:TypeSafeAPIError

访问被拒绝(403)。

typesafe_sdk.TypeSafeNotFoundError

基类:TypeSafeAPIError

未找到资源(404)。

typesafe_sdk.TypeSafeUnprocessableEntityError

基类:TypeSafeAPIError

请求未通过服务器校验(422)。

typesafe_sdk.TypeSafeRateLimitError

基类:TypeSafeAPIError

超出速率限制(429)。

retry_after_ms

instance-attribute

python
retry_after_ms = parse_retry_after(headers)

服务器要求的等待时间(毫秒);不可用时则为 None。

typesafe_sdk.TypeSafeInternalServerError

基类:TypeSafeAPIError

服务器处理请求失败(5xx)。

连接错误

typesafe_sdk.TypeSafeAPIConnectionError

基类:TypeSafeError, ConnectionError

请求失败且未收到 HTTP 响应。

typesafe_sdk.TypeSafeAPITimeoutError

基类:TypeSafeAPIConnectionError, TimeoutError

请求超出了配置的超时时间。

timeout

instance-attribute

python
timeout = timeout

请求所使用的超时设置,以秒为单位,或为 httpx2.Timeout。

响应校验

typesafe_sdk.TypeSafeAPIResponseValidationError

基类:TypeSafeAPIError

HTTP 响应成功,但其响应体缺失所需数据或所需数据的结构无效。

field_path

instance-attribute

python
field_path = field_path

指向出错字段的点分路径,例如 answers.tone.confidence。

args

instance-attribute

python
args = (
    status,
    body,
    headers,
    field_path,
    endpoint,
)
本站为 docs.typesafe.ai 的中文翻译,仅供学习参考;内容版权归原作者所有。