# Errors

Canonical URL: https://developers.iagenify.com/sdk/concepts/errors/
Language: en

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

Status: IaGenifyError · Unauthorized · NotEnoughCredits

## The three classes {#classes}

```javascript
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;
}
```

## Branching on a failure {#branching}

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

> **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 {#details}

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.

```javascript
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;
}
```

## Codes you will meet early {#common}

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

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

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

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