The three classes
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;
}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
# availableBranching on a failure
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
}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)
raiseNever match on the message
status and code are stable. The message is prose: it is written for a person, it can be rewritten, and it is translated in places. A condition built on it will break quietly.
details carries what you need to decide
Some refusals are not failures so much as a question. When a refusal has structured data behind it, it arrives on details, so your interface can offer the real choice instead of showing a dead end.
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;
}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)Codes you will meet early
| code | status | Meaning |
|---|---|---|
| unauthorized | 401 | The session is not valid. Already retried once before you saw it. |
| not_enough_credits | 402 | Carries required and available. |
| timeout | 408 | No answer within the deadline. |
| unreachable | 503 | The request never arrived. |
| storage_not_local | 400 | That storage has no files to read — use run(). |
| storage_is_local | 400 | A computer keeps its files on the machine; nothing is uploaded. |
| context_too_large | 400 | Refused before anything was charged. |
| unknown | varies | The response carried no code. Read status. |
Each reference page lists the codes its own call can raise.