# 重试

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

客户只重复了一件事, 一次: 401, 其他一切都交给你, 因为只有你知道重复是否安全。

Status: one retry · 401 only

## 重试 {#the-one}

在一个 `401` 客户端放下缓存 JWT,保留刷新标记,并重放一次请求。一个飞行中途过期的会话,或签名更改,在未达到您的代码的情况下被吸收。

> **只有 JWT 被丢弃**
>
> 刷新符物幸存下来, 所以客户端从存储的会话而不是钥匙中 建立起来, 也恢复了。

## 未被再考的, {#not-retried}

- `402` 重新尝试不会产生信用。
- `4xx` 总的来说,要求是错误的;重复要求就是错误的。
- `5xx` 重复发送、部署或写作是您的决定,而不是客户的决定。
- `timeout` 和 `unreachable` - 同样的理由:客户无法知道工作是否发生。

## 重新在身边 {#your-own}

分会 `status` 和 `code` - 信息里没有,这是传言,可以改变

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

async function withRetry(call, { tries = 3 } = {}) {
  for (let attempt = 1; ; attempt++) {
    try {
      return await call();
    } catch (err) {
      const transient = err instanceof IaGenifyError
        && (err.status >= 500 || err.code === "timeout" || err.code === "unreachable");

      if (!transient || err instanceof NotEnoughCredits || attempt === tries) throw err;

      await new Promise((r) => setTimeout(r, 2 ** attempt * 250));
    }
  }
}

const prices = await withRetry(() => api.catalogue());   // safe: a read
```

> **仅打空打空可以安全重复的打包电话**
>
> 总是读。发送、部署、购买或写作:只有您能够判断第一次尝试是否着陆。

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

def with_retry(call, *, tries=3):
    for attempt in range(1, tries + 1):
        try:
            return call()
        except IaGenifyError as error:
            transient = error.status >= 500 or error.code in ("timeout", "unreachable")
            if not transient or isinstance(error, NotEnoughCredits) or attempt == tries:
                raise
            time.sleep(2 ** attempt * 0.25)

prices = with_retry(api.catalogue)   # safe: a read
```
