Il riprova
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
402— riprovare non crea credits.4xxin 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.timeouteunreachable— la stessa ragione: il cliente non sa se il lavoro ha avventurato.
Restriegue su costado
Branch on status e code — mai sul testo del messaggio, che è prosa e può cambiare.
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 readimport 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 readSolamente 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ì.