# 错误

Canonical URL: https://developers.iagenify.com/zh/sdk/concepts/errors/
Language: zh

每一个失败都带有一个状态和一个短代码——这两个字段是分行的。

Status: IaGenifyError · Unauthorized · NotEnoughCredits

## 这三个班级 {#classes}

```javascript
import { IaGenifyError, Unauthorized, NotEnoughCredits } from "iagenify";

class IaGenifyError extends Error {
  status;      // HTTP status
  code;        // short, stable identifier
  details;     // present when the refusal carries structured data
}

class Unauthorized extends IaGenifyError {}      // 401, code "unauthorized"
class NotEnoughCredits extends IaGenifyError {   // 402, code "not_enough_credits"
  required;
  available;
}
```

## 失败时的分行 {#branching}

```javascript
try {
  await assistant.generate({ inputs });
} catch (err) {
  if (err instanceof NotEnoughCredits) {
    return topUp(err.required - err.available);
  }
  if (err instanceof Unauthorized) {
    return reauthenticate();
  }
  if (err instanceof IaGenifyError) {
    console.error(err.status, err.code, err.message);
    throw err;
  }
  throw err;    // not from the SDK
}
```

> **永远不匹配信息**
>
> 状态和代码是稳定的。 信息是发文: 信息是给一个人写的, 它可以改写, 并且翻译到其他地方。 以它为基础的条件会悄悄地打破 。

## 包含您需要决定的内容的细节 {#details}

有些拒绝不是失败,而是一个问题。 当拒绝背后有结构化数据时,它就会出现。 `details`,这样你的界面就可以提供真正的选择 而不是显示一个死胡同。

```javascript
try {
  await domain.mail.enable();
} catch (err) {
  if (err.code === "mail_would_replace_mx") {
    // err.details.current — where mail goes today
    // err.details.hint    — the suggested way out
    return offerChoice(err.details);
  }
  throw err;
}
```

## 早早会遇到密码 {#common}

| 代码代码代码 | 状态状态状态状态状态状态状态 | 含义 |
| --- | --- | --- |
| 未经授权 | 401 | 此会话无效。 在您看到它之前已经重审过一次 。 |
| not_enough_credits | 402 | 需要和可用的载货。 |
| 超时超时 | 408 | 截止日期前没有答复。 |
| 无法到达 | 503 | 请求从未到来。 |
| storage_not_local | 400 | 该存储没有文件可读—— 使用运行 () 。 |
| storage_is_local | 400 | 计算机将其文件保存在机器上;没有上传任何文件。 |
| context_too_large | 400 | 在指控之前就被拒了 |
| 未知 | 变化变化 | 回复没有代码,读取状态 |

每个参考页都列出其自用电话可以使用的代码。

```python
from iagenify import IaGenifyError, Unauthorized, NotEnoughCredits

# class IaGenifyError(Exception):
#     status    # HTTP status
#     code      # short, stable identifier
#     details   # present when the refusal carries structured data
#
# class Unauthorized(IaGenifyError): ...       # 401, code "unauthorized"
# class NotEnoughCredits(IaGenifyError):       # 402, code "not_enough_credits"
#     required
#     available
```

```python
try:
    assistant.generate(inputs=inputs)
except NotEnoughCredits as error:
    top_up(error.required - error.available)
except Unauthorized:
    reauthenticate()
except IaGenifyError as error:
    print(error.status, error.code, error)
    raise
```

```python
try:
    domain.mail.enable()
except IaGenifyError as error:
    if error.code != "mail_would_replace_mx":
        raise
    # error.details["current"] — where mail goes today
    # error.details["hint"]    — the suggested way out
    offer_choice(error.details)
```
