# Novas tentativas

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

Il client tenta una cosa, una sola: una 401. Tutto il resto è dadas a te, perché solo tu sa se repeti o non è sicuro.

Status: one retry · 401 only

## Il riprova {#the-one}

Su un `401` il client larghese la cached JWT, mantene la gnâm rafraîtchîe, e reproduce la richiesta una volta. Una sessione che ha expirat mid-vol, o un cambio di segna, è absorbit sin atingere il vostro codice.

> **Solo il JWT è cadut**
>
> Il token de revigorare sopravvive, così un client costruito da una sessione stoked piuttosto que una chiave recupera tao.

## Qua non è rejuzgat {#not-retried}

- `402` — riprovare non crea credits.
- `4xx` in general — la richiesta è errata; repetir la mantiene errata.
- `5xx` — la chiamata può avere un effetto. Repetire un envio, un deployment o un write è una decisione tua, non del cliente.
- `timeout` e `unreachable` — la stessa ragione: il cliente non sa se il lavoro ha avventurato.

## Restriegue su costado {#your-own}

Branch on `status` e `code` — mai sul testo del messaggio, che è prosa e può cambiare.

```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
```

> **Solamente s'impletjano le chiamate che sono sicure da ripetere**
>
> Legge, sempre. Un envio, un deploy, un compra o un scriva: solo se si sape se il primo tentato aterrì.

```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
```
