# Errors

Source: https://docs.sociaro.com/errors/

Use the HTTP status and the error fields documented for the requested endpoint. [Jobs and Files errors](https://docs.sociaro.com/errors/#v2-errors) have a common envelope. Native v1 generation endpoints can return a provider-specific error.

| Status | Meaning | Action |
| --- | --- | --- |
| `400` | Invalid field, value, request body or media URL. | Correct the input named in the message. |
| `401` | Missing, invalid, expired or revoked key. | Check the bearer key. |
| `403` | Access denied. Some features require organisation activation. | Check the model, resource ownership and feature access. |
| `404` | Unknown route, model, job or file, or a resource outside the key's scope. | Check the path, ID and owning account. |
| `405` | Unsupported HTTP method on this route. | Check the endpoint and method. |
| `408` | File upload timed out. | Upload again. |
| `409` | Request conflicts with existing state. | For a job, check the idempotency key and request body. For a balance lookup, contact your account manager if no spending limit is configured. |
| `413` | Request exceeds the route's size limit. | Reduce the body. For estimates, replace large inline media with accessible URLs. |
| `415` | Unsupported or mismatched media type. | Check the file bytes and `Content-Type` against the [Files API](https://docs.sociaro.com/files/index.md). |
| `422` | Source media could not be prepared. | Re-encode or replace the file named in the message. |
| `429` | Insufficient balance, rate limit, concurrency limit or provider capacity. | Read the error type and code. Follow `Retry-After` when present. |
| `500` | Unexpected service error. | Follow the endpoint's retry rules. |
| `502`, `503`, `504` | Temporary gateway, provider or timeout error. | Retry reads. For a v2 submit, replay with the same idempotency key. For v1, check the route's retry rules before resubmitting. A file upload answered `503 files_api_overloaded` comes with `Retry-After`: send it again after that many seconds. |

A request answered with `Upstream error. Try again later or contact your account manager.` can be retried later; if it keeps coming back, contact your account manager, or hello@sociaro.com if your organisation has none.

## /v2 errors

Every error answer of every `/v2` route (jobs, files, models, the estimate, the lookup, the OpenAPI document and any
path beneath them), the gateway's own `400`, `413`, `429`, `502` and `504` included, has the same `error` object (the one
exception: a request the gateway cannot parse at all, such as a malformed request line or header line, gets a `400`
that is a plain page, not JSON):

```json
{"error": {"type": "invalid_input", "code": "invalid_input", "message": "model (string) is required", "provider_message": null},
 "job_created": false}
```

- Branch on `error.type` and log `error.code`. The `message` field describes the problem. Submit errors have `provider_message: null`. A failed job can include a provider message or a moderation type. See [failed jobs](https://docs.sociaro.com/jobs/failures/#failed-jobs).
- Only errors from `POST /v2/jobs` include top-level `job_created`. A route-level `404` or `405` can also include `detail`. A path outside the supported v2 routes is not in this envelope. An edge service may return an HTML error page.
- A `429` may include `scope`, `retry_after` or the `Retry-After` header.

| `error.type` | `error.code` |
| --- | --- |
| `invalid_input` | `invalid_input`, `byo_storage_error`, `storage_changed`, `unsupported_input`, `invalid_idempotency_key`, `idempotency_key_required`, `invalid_callback_url`, `idempotency_conflict`, `cancel_not_supported`, `file_too_large`, `unsupported_media_type`, `content_type_mismatch`, `upload_timeout`, `estimate_unavailable`, `method_not_allowed`, `request_too_large` |
| `unauthorized` | `invalid_key` |
| `forbidden` | `forbidden`, `storage_disabled` |
| `not_found` | `model_not_found`, `job_not_found`, `file_not_found`, `not_found`, `route_not_found` |
| `insufficient_balance` | `insufficient_budget` |
| `rate_limited` | `rate_limit_exceeded`, `rate_limited` |
| `concurrency_limited` | `too_many_inflight_jobs`, `too_many_uploads` |
| `upstream_error` | `gateway_unavailable`, `files_api_overloaded` |
| `internal_error` | `unavailable`, `internal_error`, `upstream_error` |

**Handling HTTP 429**

- `insufficient_balance`: check the organisation's balance and top up. Unclassified budget responses also use this type.
- `rate_limited`: wait for the specified retry interval.
- `concurrency_limited`: reduce simultaneous jobs or uploads and retry after `Retry-After`.

**Estimate errors.** `400 estimate_unavailable` means the gateway could not read the media measurement needed for pricing. An estimate can succeed with an exhausted balance. A body larger than 2,097,152 bytes returns `413 request_too_large`. See [estimate responses](https://docs.sociaro.com/jobs/models/#other-answers).

**Terminal job errors.** The `error` on a failed job is distinct from a submit error. For example, `content_policy_violation` can appear after the provider reviews the input or generated output. See the [terminal error codes](https://docs.sociaro.com/jobs/failures/#the-codes).

**`job_created`.**

- `false`: this attempt created nothing. Release the user's credits when no earlier attempt with the same
  `Idempotency-Key` ended ambiguously (`null`, `502`, `503`, `504`, or no answer); see `null` below. On `409 idempotency_conflict` it is `false` for
  this request, and the key already belongs to an earlier job with another body.
- `null`: we cannot tell. This happens in three cases: a `503` after the job may have been stored, or after a timeout inside the submit's transaction (a wait on an earlier attempt with the same key), a `502` or `504`
  from the gateway, and a `500` `internal_error`, an unexpected internal error that may have come after the job was
  stored. An answer that is not JSON, or no answer at all, means the same. Hold the credits and send the same request
  again with the same `Idempotency-Key`: it returns the existing job or creates exactly one. You can also look the key up
  with `GET /v2/jobs?idempotency_key=` ([Jobs API](https://docs.sociaro.com/jobs/failures/#look-up-a-job-by-its-key)). A `404` from a lookup sent at least
  180 seconds after your last attempt with that key ended (an answer arrived, the connection closed, or you stopped waiting and closed it)
  is final: no job exists, none of those attempts will create one, and you can release the credits. This holds for requests sent to the public API host, on the condition that a request reaches our server within 90 seconds after your attempt with that key ended; sending the same request again with the same Idempotency-Key is always safe and settles the outcome without relying on timing (it returns the existing job or creates exactly one). A `404` from a lookup
  sent before that is not proof, whenever it arrives, because an earlier attempt may still create the job; a `503` from the lookup is not an answer about the job either, so ask again. A refusal of a later attempt (a `401`, `403`, `429` or `503`, for example) or its
  `job_created: false` settles only that attempt: keep the credits held until the answer is the job (`200` or `202`) or
  that `404`. A job that does appear is charged once.
- A success answer (`202`, or `200` for a repeated key) carries the job and no `job_created`.

## v1 error handling

For v1 responses, read `error.message` when available and fall back to the HTTP status if the body is empty, HTML or in a different format.

Example balance error:

```json
{"error": {"message": "budget exceeded for this organisation: its spend has reached the balance it was topped up to. Top up in the console to continue."}}
```

v1 error formats depend on the endpoint. The native task and image routes that otherwise return the generator's own error answer a request they cannot take with `503`, `error.code` `Gateway.UpstreamUnavailable` and `Upstream error. Try again later or contact your account manager.` or `temporarily unavailable, retry later`; a task or an image job that ended that way polls as `failed` with that `error`. [Generations API](https://docs.sociaro.com/generations-api/index.md) documents submission, polling and retry rules. For example, a failed ModelArk task can report an input-preparation error on poll, while [reference preparation](https://docs.sociaro.com/reference-images/index.md) may reject a request before submission. For jobs without an idempotency key, verify the outcome of a timed-out submit before sending another request.

## Charges on failure

A refused request incurs no charge. A job whose price cannot be determined is withheld. For terminal failures, see [failed job billing](https://docs.sociaro.com/jobs/failures/#what-a-failed-job-costs).
