# 错误

请求失败时，响应体包含一个 `errors` 对象。请根据 `errors.type` 进行分支处理：一个 HTTP 状态可能对应多种原因。

## 错误格式

```json
{
    "errors": {
        "type": "params_invalid",
        "message": "Either domain or company parameter must be provided.",
        "code": 422
    }
}
```

| 字段     | 说明                                                                      |
| --------- | -------------------------------------------------------------------------------- |
| `type`    | 机器可读的原因。取值列在[错误类型](#error-types)中。 |
| `message` | 人类可读的说明。措辞可能会变化；请勿据此进行匹配。           |
| `code`    | 响应的 HTTP 状态码。                                            |

`errors` 始终是单个对象，绝不会是数组。API 的响应带有 `X-Request-ID` 响应头，您也可以发送自己的 ID（[请求 ID](/going-to-production#request-ids)）；就失败的请求[联系支持团队](/support#what-to-include)时，请附上该 ID。

少数失败情况不使用此格式：

- 大于 4 MB 的请求体会返回 `413` 和 `{"error": "Request Entity Too Large"}`。
- 运行超过 180 秒的请求会由网关返回 `504`，响应体不是 JSON。
- 当未找到任何结果，或输入为一次性邮箱或网页邮箱地址时，`GET /people/find`、`GET /companies/find` 和 `GET /combined/find` 会返回状态 `200` 以及包含 `errors` 的响应体，其 `code` 为 404 或 422。对于这些端点，请检查响应体中是否有 `errors`。

## 状态码

| 状态 | 含义                                                                                            | 是否重试                                                  |
| ------ | -------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| `200`  | 成功                                                                                            | —                                                       |
| `201`  | 已创建资源。`GET /email-sources` 在找不到任何来源时也会返回 `201`，且不含数据。 | —                                                       |
| `400`  | 凭据缺失、格式错误或已过期，或请求被某个参数或套餐规则拒绝   | 否。请修正请求。                                    |
| `401`  | 凭据与任何账户都不匹配、该账户无法使用 API，或 OAuth 令牌无效   | 否                                                      |
| `402`  | 积分不足以完成该请求                                                                 | 否。请等待用量周期更新，或添加积分。 |
| `403`  | OAuth 令牌缺少某个权限范围，或套餐不包含该功能                             | 否                                                      |
| `404`  | 路由或记录不存在                                                                  | 否                                                      |
| `408`  | 邮箱验证器无法完成邮箱检查                                             | 是，稍后重试                                              |
| `409`  | 记录重复，或批量任务的状态不允许该操作                            | 否                                                      |
| `413`  | 请求体大于 4 MB                                                               | 否                                                      |
| `422`  | 某个参数未通过验证，或已达到账户上限                                       | 否。请修正请求。                                    |
| `429`  | 触发了速率限制，或邮箱计数或来源余额已用完                             | 是，对于速率限制，在 `Retry-After` 之后重试               |
| `451`  | 应所有者的要求，该地址、个人资料或电话号码不予提供                           | 否                                                      |
| `500`  | 服务器错误                                                                                       | 是，采用退避策略重试                                       |
| `504`  | 请求耗时超过 180 秒                                                           | 是，或使用[批量任务](/bulks)                         |

所有错误响应都不消耗积分。

## 错误类型

| `errors.type`                                        | 状态             | 原因                                                                                                                                                             | 建议操作                                                                                                    |
| ---------------------------------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| `params_invalid`                                     | 422, 400           | 某个参数未通过验证（422），或被套餐规则拒绝（400），例如 Free 套餐的结果数量限制。                                                      | 修正消息中指出的参数。                                                                   |
| `unknown_record`                                     | 404, 400           | 记录不存在（404），或邮箱查找器收到了一次性邮箱或网页邮箱域名（400）。                                                                | 检查 ID 或输入。                                                                                    |
| `unknown_route`                                      | 404                | URL 与任何端点都不匹配。                                                                                                                                | 在 [API 参考](/api)中检查方法和路径。                                                       |
| `not_found`                                          | 404                | 所请求的日志条目不存在。find 系列端点也会在 `200` 响应中返回此错误。                                                                 | —                                                                                                             |
| `authentication_failed`                              | 400, 401, 403      | 凭据缺失、格式错误或不正确，或者该账户无法使用 API。                                                                               | 请参阅[认证](/authentication#errors)。                                                                 |
| `api_key_expired`                                    | 400                | API 密钥已超过到期日期。`X-Tomba-Key-Expired` 响应头会给出该日期。                                                                             | [轮换密钥](/authentication#rotate-keys)。                                                                |
| `invalid_token`                                      | 401                | OAuth 访问令牌无效、已过期或已被撤销。                                                                                                           | 刷新令牌或重新授权。                                                                            |
| `insufficient_scope`                                 | 403                | OAuth 访问令牌缺少该端点所需的权限范围。                                                                                                        | 申请该权限范围；请参阅 [OAuth 2.0](/authentication#oauth-20)。                                                 |
| `forbidden`                                          | 403                | 没有管理员权限的工作区成员请求了团队或成员用量。                                                                                           | 使用 `scope=user`，或请工作区管理员操作。                                                                   |
| `quota_exceeded`                                     | 402                | 搜索或验证余额，或该成员的个人限额，低于请求的消耗。                                                           | 请参阅[积分和用量](/usage-and-quotas)。                                                                   |
| `rate_limit`                                         | 429, 422           | 429：触发了速率限制，或邮箱计数或来源余额已用完。422：账户的 API 密钥、线索、线索列表或线索属性数量已达上限。 | 对于 429，请参阅[处理 429 响应](/rate-limits#handle-429-responses)。对于 422，请删除部分项目或更换套餐。 |
| `proxy_error`                                        | 408                | 邮箱验证器的邮箱检查失败。                                                                                                                        | 稍后重试。                                                                                                  |
| `duplicate_record`                                   | 409                | 针对同一项目的报告已在等待审核。                                                                                                               | 等待审核结果。                                                                                          |
| `invalid_state`                                      | 409                | 批量任务的状态不允许该操作（例如启动一个已在运行的任务），或者任务已过期。消息会说明是哪种情况。                  | 检查任务的 `status`；请参阅[生命周期](/bulks#lifecycle)。                                                  |
| `claimed_email`, `claimed_linkedin`, `claimed_phone` | 451                | 此人已要求 Tomba 停止处理其数据，因此该地址、LinkedIn 个人资料或电话号码将永久不予提供。                                  | 从您的数据中删除该记录。                                                                             |
| `ip_blocked`                                         | 400                | Tomba 的防火墙拦截了该请求的 IP 地址。                                                                                                                 | [联系支持团队](mailto:support@tomba.io)。                                                              |
| `openai_error`, `search_failed`                      | 422                | 公司搜索无法处理该查询。                                                                                                                        | 重试，或简化查询。                                                                                 |
| `internal_error`                                     | 500                | 实时技术查询失败。                                                                                                                                  | 使用退避策略重试。                                                                                           |
| `api_error`                                          | 500, 400, 404, 422 | 意外错误，或批量任务没有所请求的结果文件（404）。                                                                                       | 对于 500，采用退避策略重试。如果问题持续存在，请附上 `X-Request-ID` 联系支持团队。                            |

## 处理错误

<CodeTabs syncKey="lang">

```javascript title="Node.js"
const res = await fetch(
    "https://api.tomba.io/v1/email-finder?domain=stripe.com&full_name=Jane%20Doe",
    {
        headers: {
            "X-Tomba-Key": process.env.TOMBA_API_KEY,
            "X-Tomba-Secret": process.env.TOMBA_SECRET_KEY,
        },
    },
);
const body = await res.json();

if (body.errors) {
    const { type, message } = body.errors;
    const requestId = res.headers.get("X-Request-ID");
    if (type === "rate_limit" && res.status === 429) {
        // Wait for Retry-After, then retry. See /rate-limits.
    } else if (res.status === 408 || res.status >= 500) {
        // Transient: retry later with backoff.
    } else {
        throw new Error(
            `${res.status} ${type}: ${message} (request ${requestId})`,
        );
    }
} else {
    console.log(body.data);
}
```

```python title="Python"
import os

import requests

response = requests.get(
    "https://api.tomba.io/v1/email-finder",
    params={"domain": "stripe.com", "full_name": "Jane Doe"},
    headers={
        "X-Tomba-Key": os.environ["TOMBA_API_KEY"],
        "X-Tomba-Secret": os.environ["TOMBA_SECRET_KEY"],
    },
    timeout=180,
)
body = response.json()

if "errors" in body:
    error = body["errors"]
    request_id = response.headers.get("X-Request-ID")
    if error["type"] == "rate_limit" and response.status_code == 429:
        pass  # Wait for Retry-After, then retry. See /rate-limits.
    elif response.status_code == 408 or response.status_code >= 500:
        pass  # Transient: retry later with backoff.
    else:
        raise RuntimeError(
            f"{response.status_code} {error['type']}: {error['message']} "
            f"(request {request_id})"
        )
else:
    print(body["data"])
```

</CodeTabs>
