# Errors

> Every Nullgate API and SDK error code with its HTTP status, what it means, whether you are charged, what the SDK retries and what to do next.

The SDK raises `NullgateError` with a `code`. Give `request_id` to support.

```python
from nullgate import NullgateError

try:
    sensor = page.sensor(script=script, cookies=http.cookies)
except NullgateError as error:
    print(error.code, error.status, error.request_id, error.message)
```

```typescript
import { NullgateError } from "@nullgate/sdk";

try {
  const sensor = await page.sensor({ script, cookies });
} catch (error) {
  if (!(error instanceof NullgateError)) throw error;
  console.log(error.code, error.status, error.requestId, error.message);
}
```

Raw HTTP non-2xx responses contain a JSON error:

```json
{
  "code": "invalid_request",
  "message": "Invalid field: user_agent",
  "request_id": "correlation-id"
}
```

## API codes

| Code                   | HTTP               | Meaning                                                                                              | What to do                                                                                |
| ---------------------- | ------------------ | ---------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `invalid_request`      | 400, 405, 413, 415, 422 | A field is missing, malformed, too large or does not match the page. HTTP 415 means unsupported request compression. | Fix the field or compression. Do not repeat the same request |
| `invalid_key`          | 401                | The key is missing, wrong, expired or revoked, or the account is disabled                            | Check `NULLGATE_API_KEY`. Create a new key in the bot                                     |
| `insufficient_balance` | 402                | The balance does not cover the operation                                                             | Top up in the bot, then repeat                                                            |
| `forbidden`            | 403                | The operation is not permitted                                                                       | Do not repeat. Write to support with `request_id`                                         |
| `not_found`            | 404                | No such path                                                                                         | Check the URL against the [HTTP API](https://nullgate.club/en/http)                                            |
| `session_busy`         | 409                | Another request of the same session is still running                                                 | Wait for that request, then repeat. Send the requests of one session one by one           |
| `script_required`      | 428                | The sensor script is not retained for this document, or its digest does not match. `message` is `Send the script for this document.` | Send the full script. The SDK retries once for you |
| `rate_limited`         | 429                | Too many requests                                                                                    | Wait the number of seconds in the `Retry-After` header, then repeat                       |
| `internal`             | 500                | Nullgate failed                                                                                      | Repeat once. If it fails again, write to support with `request_id`                        |
| `unsupported`          | 501                | The ACIC challenge profile is not supported | Use a supported challenge profile. This error is not charged |
| `upstream`             | 502                | A step outside Nullgate failed. For ThreatMetrix: your `send` callback did not return a valid answer | Check your `send` callback, proxy and timeouts                                            |
| `unavailable`          | 503                | Nullgate cannot serve the request now. Sustained generation overload includes `Retry-After: 1` | Wait `Retry-After` seconds if present, otherwise a few seconds, then repeat |
| `maintenance`          | 503                | Nullgate restarts. The answer has a `Retry-After` header                                             | The SDK repeats the request for you. Over raw HTTP, wait `Retry-After` seconds and repeat |

### Charges and session state

- A failed request is not charged.
- HTTP 413 `invalid_request`: continuation over 256 KiB before compression (state unchanged), Akamai body over 8 MiB, or sensor, pixel or SBSD script over 2 MiB in UTF-8.
- HTTP 428 `script_required` leaves state unchanged.
- HTTP 503 `unavailable` from overload leaves state unchanged. The SDK does not retry it.
- ThreatMetrix profile-start capacity errors use WebSocket `error` with `code: "unavailable"`, not HTTP 503.

## What the SDK repeats

Only these errors trigger SDK retries:

- **`maintenance`:** after `Retry-After`, up to two more attempts within `timeout` (default 30 seconds).
- **`script_required`** (Akamai sensor): one retry with the full script you supplied, then the SDK raises the error if it recurs.

## SDK codes

SDK-side errors before or after an API call have no `status`.

| Code                   | Meaning                                                                              | What to do                                          |
| ---------------------- | ------------------------------------------------------------------------------------ | --------------------------------------------------- |
| `protection_not_found` | The page HTML has no script of the kind you asked for                                | Check the HTML, or pass `script_url`                |
| `ambiguous_protection` | Several scripts match                                                                | Pass one of the URLs from `message` as `script_url` |
| `invalid_page`         | The HTML cannot be used as a page                                                    | Pass the full HTML that the target returned         |
| `invalid_url`          | A URL is not an absolute `http` or `https` URL                                       | Pass the final URL of the page                      |
| `invalid_inputs`       | `inputs`, `form` or `timing` do not match the page, or contain a raw password | Read `message`. For passwords, use `prepare_inputs()` / `prepareInputs()` |
| `invalid_actions`      | `actions` contain raw typed values                                                   | Remove the raw values                               |
| `invalid_cookies`      | `cookies` is not a mapping or a cookie jar                                           | Pass a dictionary or the jar of your client         |
| `transport_error`      | The SDK could not reach Nullgate or lost the connection                              | Check your network, then repeat                     |
| `timeout`              | TypeScript. The call took longer than `timeout`                                      | Repeat, or raise `timeout`                          |
| `protocol_error`       | Nullgate returned an answer the SDK cannot read. Python also uses `invalid_response` | Update the SDK. If it still fails, write to support |
| `closed`               | Python. The client is already closed                                                 | Create a new `Nullgate()`                           |
