Concepts

Errors

Three classes, exported so instanceof works. Every failure carries a status and a short code — those are the two fields to branch on.

IaGenifyErrorUnauthorizedNotEnoughCredits
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

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
#     available

Branching 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)
    raise
Never 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

codestatusMeaning
unauthorized401The session is not valid. Already retried once before you saw it.
not_enough_credits402Carries required and available.
timeout408No answer within the deadline.
unreachable503The request never arrived.
storage_not_local400That storage has no files to read — use run().
storage_is_local400A computer keeps its files on the machine; nothing is uploaded.
context_too_large400Refused before anything was charged.
unknownvariesThe response carried no code. Read status.

Each reference page lists the codes its own call can raise.