> ## Documentation Index
> Fetch the complete documentation index at: https://enterprise-docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 结果、错误与退出码

> 看懂 difyctl 命令输出的内容，判断一次运行是怎样结束的，并处理错误、退出码、重试与限流

## 输出去向

命令的结果一律以 JSON 输出到 stdout，错误和通知则输出到 stderr：

* 错误：在终端中显示为文本；如果把 stderr 重定向到文件或交给其他程序（例如 `2> errors.log`），则以 JSON 输出。要让终端中的错误也以 JSON 显示，加上 `--json`。
* 通知（如 `stream ended early: …`）：始终是纯文本。

## 读取运行结果

`run console_app` 命令的结果包含应用的回答，以及后续操作要用到的 ID：

| 字段 | 内容 |
| :- | :- |
| `text.answer` | 聊天助手、旧版 Agent 应用、Chatflow 应用或文本生成应用的回复。运行 Workflow 应用时为空。 |
| `outputs` | Workflow 应用的回答，键名对应输出节点的变量。Chatflow 应用会在这里再给出一次回复，并附上它返回的文件（如有）。 |
| `run_id` | Workflow 或 Chatflow 应用的运行 ID，可传给 `describe run` 和 `get run node`。 |
| `conversation_id` | 聊天助手、旧版 Agent 应用或 Chatflow 应用的会话 ID，用 `--conversation-id` 传回即可继续会话。 |

### 判断运行如何结束

在脚本中，应读取结果的 `status`。运行无论是正常结束、中途暂停还是被截断，退出码都是 0，要靠 `status` 来区分：

* `ended`：运行已结束。
* `suspended`：运行在人工介入表单处暂停。

  如果 `difyctl` 能填写这份表单，`hints` 为每个操作按钮各给出一条命令；如果表单只发给了其他收件人，`hints` 为 `[]`。填写表单或跟踪运行的方法，详见 [填写人工介入表单](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/run-apps.md#填写人工介入表单)。
* `incomplete`：运行还没结束，结果就被截断了，例如你按下了 Ctrl+C。结果中只有截断前已收到的内容。

  Workflow 或 Chatflow 应用的运行仍会在服务器上继续。把结果中的 `run_id` 传给 `difyctl describe run`，可查看它最终如何结束；传给 [`stop run`](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/run-apps#停止运行)，则可停止它。如果结果中没有 `run_id`，可在应用的 [正在进行的运行列表](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/run-apps.md#停止运行) 中找到这次运行。
* `failed`（退出码 1）：运行失败或被停止，原因见 `error.message`。

  要区分 Workflow 或 Chatflow 应用的运行是失败还是被停止，把结果中的 `run_id` 传给 `difyctl describe run`：被停止的运行，`status` 会变为 `stopped`。

运行失败时，结果和其他运行一样打印到 stdout，stderr 上没有任何输出。例如，运行 Workflow 应用时漏传了必填输入：

```json theme={null}
{
  "status": "failed",
  "text": {
    "answer": ""
  },
  "hints": [],
  "run_id": "f16c3509-ef54-4618-ae93-770724ab278d",
  "total_tokens": 0,
  "error": {
    "message": "topic is required in input form"
  }
}
```

## 解读错误信息

命令如果在产生结果之前就失败，stdout 上不会有任何输出，错误会打印到 stderr。下面是传入不存在的应用 ID 时，以 JSON 输出的错误：

```json theme={null}
{
  "error": {
    "code": "server_error",
    "message": "app not found",
    "http_status": 404,
    "method": "GET",
    "url": "https://dify.example.com/openapi/v1/apps/00000000-0000-4000-8000-000000000000",
    "server": {
      "code": "not_found",
      "message": "app not found",
      "status": 404
    }
  }
}
```

每个错误都带有 `code` 和 `message`，脚本要根据 `code` 走不同分支。根据失败原因，错误还可能带有以下字段：

* `hint`：用一句话说明下一步可以怎么做，例如 `run difyctl help get console_app`。
* `http_status`、`method`、`url` 和 `server`（服务器自身的错误）：错误由服务器返回时才会带有。遇到 `server_error` 时，根据 `http_status` 或 `server.code` 走不同分支，例如上例中的 `404` 和 `not_found`。
* `details`：某个值没有通过 `difyctl` 自身的校验（`input_invalid`）时才会带有，列出每个校验失败的字段及其标志和原因，例如 `"field": "--limit", "msg": "must be integer"`。

在终端中，如果服务器给出了自己的错误码，错误的第一行会显示这个错误码，所以上例中找不到应用的错误显示为 `not_found: app not found`。脚本分支时要用 JSON 中的错误码，这里是 `server_error`。

如果 `message` 说不清失败原因，加上 `--verbose`，错误中就会带上 `raw_response`，即服务器的完整响应。

## 退出码

每条命令退出时都会返回以下某个退出码：

| 退出码 | 含义 | 错误码 |
| :- | :- | :- |
| 0 | 成功，包括暂停（`suspended`）或被截断（`incomplete`）的运行 | |
| 1 | [运行失败](#判断运行如何结束)，或其他各行未涵盖的错误，例如服务器错误或网络错误 | `server_error`、`network_connection`、`keyring_unavailable`、`skill_missing`、`unknown` |
| 2 | `difyctl` 在发送前就拒绝了命令：标志未知、缺少必填标志，或值的类型不对 | `usage_invalid_flag`、`usage_missing_arg`、`input_invalid`、`config_invalid_key`、`config_invalid_value` |
| 4 | 未登录、登录已过期，或没有访问权限 | `not_logged_in`、`auth_expired`、`access_denied` |
| 6 | 服务器上没有这条命令，或 `difyctl` 无法获取服务器的命令列表 | `unknown_op`、`catalog_unavailable` |
| 7 | 触发了 [限流](#限流) | `rate_limited` |

退出码为 1 时，先看 stdout，运行失败的结果就输出在那里。其他错误都在 stderr 中。

具体错误的解决方法，可到 [故障排查](#故障排查) 中查找。

## 重试失败的命令

读取类命令（如 `get` 和 `describe`）可放心重试。`help` 会在每条命令的描述旁显示它的标签：`read`、`write` 或 `destructive`。

重试 `write` 或 `destructive` 命令前，先确认第一次是否已经生效，例如用对应的 `get` 或 `describe` 命令查看。运行类命令属于 `write`，重试会另起一次新的运行。

遇到退出码 7 时，任何命令都可放心重试，运行类命令也不例外：被 [限流](#限流) 拒绝的命令在服务器上什么都没做。

对于退出码 2 和 4，在解决标志写错、[登录过期](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/manage-sign-in.md#续期登录) 等问题之前，每次重试都会得到同样的退出码。

### 限流

命令以退出码 7 退出时，等待 1 分钟后再重试。

每次登录和每个 [个人访问令牌](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/personal-access-tokens.md) 各有独立的请求上限，默认为每分钟 60 次。要调整上限，可设置 `OPENAPI_RATE_LIMIT_PER_TOKEN`（Helm values 中为 `api.openapi.rateLimitPerToken`），设为 `0` 则关闭限流。

## 故障排查

| 错误信息 | 错误码 | 处理方法 |
| :- | :- | :- |
| `invalid bearer` | `auth_expired` | 登录已过期或已失效。[重新登录](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/manage-sign-in.md#续期登录)。 使用个人访问令牌时，解决方法详见 [个人访问令牌的故障排查](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/personal-access-tokens.md#故障排查)。 |
| `not logged in: …` | `not_logged_in` | 先登录。如果已经登录，将 `DIFY_CONFIG_DIR` 指向登录时所用的配置目录。 |
| `forbidden: …`，提示为 `permission denied: this account may not do this` | `access_denied` | 账户的角色不具备命令所需的权限，或账户对该应用没有 [资源访问权限](https://enterprise-docs.dify.ai/zh/3.14.x/use/workspace/roles-and-permissions#控制资源访问)。 命令所需的权限可在 [命令索引](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/reference/command-index.md) 中查到。 |
| `install plugin` 返回 `plugin_install_forbidden: …` | `access_denied` | 部署的 [**允许的插件**](https://enterprise-docs.dify.ai/zh/3.14.x/administer/plugins-management.md#允许特定的插件类型) 设置不允许安装这个集成。 |
| `license_invalid` | `access_denied` | 部署的 Enterprise 许可证未激活、已过期或已丢失，需要续期或重新激活。 |
| `workspace not found` | `server_error` | 账户不是命令所操作工作空间的成员。这个工作空间由 `DIFY_WORKSPACE_ID` 或 `--workspace-id` 指定，或者是应用所属的工作空间。 用 `difyctl get workspace` 核对 ID。如果工作空间没错，就需要把该账户加入这个工作空间。 |
| `no command or server operation named <words>` | `unknown_op` | 用 `difyctl help` 核对命令。如果拼写无误，说明服务器的 Dify 版本没有这条命令。 |
| `failed to fetch the catalog: …` | `catalog_unavailable` | 确认服务器正在运行，且登录时用的地址正确。 |
| `<method> <url> timed out after <ms>ms` | `network_connection` | 调高 [`http.timeout`](https://enterprise-docs.dify.ai/zh/3.14.x/develop/cli/reference/configuration#设置)。 |
| 所有用到登录的命令都返回 `Internal Server Error`（HTTP 503） | `server_error` | 确认 `ENABLE_OAUTH_BEARER` 已设为 `true`（Helm values 中为 `api.enableOauthBearer`）。 |
| 所有 Skill 命令都返回 `skills_disabled: …`（HTTP 503） | `server_error` | 确认 `ENABLE_SKILL` 已设为 `true`（Helm values 中为 `api.enableSkill`）。 |
| `skill_not_found: …` | `server_error` | 用 `get skill` 核对 Skill ID。 |
| `skill_version_not_found: …` | `server_error` | 用 `get skill version` 核对版本 ID。 |
| `import skill` 返回 `skill_name_conflict: …` | `server_error` | 已有其他 Skill 使用这个名称。 如果你要更新的正是那个 Skill，用 `get skill` 查到它的 ID，通过 `--skill-id` 传入。否则，在 `SKILL.md` 中给你的 Skill 改名。 |
| `import skill` 返回 `skill_name_mismatch: …` | `server_error` | `SKILL.md` 中的 `name` 与 `--skill-id` 所指 Skill 的名称不一致。 把名称改回去，或者去掉 `--skill-id`，将文件夹作为新 Skill 导入。 |
| `import skill` 返回 `draft_changed: …` | `server_error` | 你读取 `draft_hash` 之后，Skill 又有了改动，例如草稿经过编辑、Skill 已发布或已改名。 先运行 `describe skill` 记下新的哈希值，再导出当前草稿，把其中的改动合并到你的文件夹，然后用记下的哈希值重新导入。如果直接导入现在的文件夹，这些改动会丢失。 |
| `import skill` 返回 `skill_package_invalid: …` | `server_error` | 该文件夹不是有效的 Skill 包。格式要求详见 [Agent Skills](https://agentskills.io/home)。 |
| `publish skill` 或 `export skill` 返回 `skill_package_invalid: …` | `server_error` | 草稿中 `SKILL.md` 的 frontmatter 无效。在 Dify 中修正错误信息指出的字段或行。 |
| `import skill` 返回 `request_entity_too_large: …` | `server_error` | 文件夹的文件数或体积超出了服务器的 [Skill 包限制](https://enterprise-docs.dify.ai/zh/3.14.x/deploy/advanced-configuration/environment-variables.md#upload_skill_file_size_limit)。 删除 Skill 用不到的文件，或请服务器管理员调高错误信息中提到的那项限制。 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.