# Errors

The Mirli server reports an error with an HTTP status and a JSON body. This page
describes the status codes, the body shapes, and the errors any endpoint can
return. Each endpoint page lists the errors that endpoint returns itself.

## Status codes

The API uses these status codes:

| Status | Meaning |
|---|---|
| `200 OK` | The request succeeded and the body is the result |
| `201 Created` | A new resource was made and the body is that resource |
| `202 Accepted` | A job was queued. The body is `{ "job_id": "<JOB_ID>" }` |
| `204 No Content` | The request succeeded and there is no body |
| `206 Partial Content` | Part of an audio file, for a range request |
| `400 Bad Request` | The request cannot run as sent: an unknown model, an option the model does not support, a file that cannot be read |
| `401 Unauthorized` | The server has an access token and the request did not carry it |
| `403 Forbidden` | A request rule or a capability refused the request |
| `404 Not Found` | No route, or no resource with that id |
| `409 Conflict` | The request conflicts with the current state, such as deleting a sound that is on a timeline |
| `413 Content Too Large` | An upload is over its size limit |
| `416 Range Not Satisfiable` | A range request outside the file |
| `422 Unprocessable Content` | The body failed validation |
| `429 Too Many Requests` | The job queue is full, or a rate limit was reached |
| `499` | The client disconnected before a waiting request finished |
| `500 Internal Server Error` | A job failed, or the server hit an unexpected error |

## Error body

Most errors have a body with a `detail` string:

```json
{
  "detail": "Sound not found"
}
```

### Validation errors

A body that fails validation is `422`, and `detail` is a list with one entry per
field:

```json
{
  "detail": [
    {
      "loc": ["body", "duration_s"],
      "msg": "Too big: expected number to be <=300",
      "type": "too_big"
    }
  ]
}
```

| Field | Description |
|---|---|
| `loc` | Where the problem is: `"body"` and then the field path |
| `msg` | What is wrong |
| `type` | The validation rule that failed, such as `too_big` or `invalid_type` |

### Model errors

An error the caller can act on, such as an unknown model or a model that cannot
run, is `400` with a `detail` and a [failure object](#failure-object). This
example is an unknown model id on a server with one model:

```json
{
  "detail": "Unknown model 'sfx-v9'. Available: audiocpp-stable-audio-3-sfx",
  "failure": {
    "topic": null,
    "title": "Unknown model 'sfx-v9'. Available: audiocpp-stable-audio-3-sfx",
    "hint": null,
    "fixes": [],
    "terminal": false,
    "stage": null,
    "detail": "Unknown model 'sfx-v9'. Available: audiocpp-stable-audio-3-sfx"
  }
}
```

### Job failures

A job that fails keeps its error on the [job object](https://mirli.app/docs/api-jobs/index.md#job-object),
in `error` and `failure`. An endpoint that waits for its job reports the
failure in the response instead:

| Endpoint | Status | Body |
|---|---|---|
| `POST /api/render` | `500` | `{ detail, failure, job_id }` |
| `POST /api/transcribe` | `500` | `{ detail, job_id }` |

## Failure object

A failure says what went wrong in terms a person can act on, and keeps the raw
message. The app shows it as an error card.

| Field | Type | Description |
|---|---|---|
| `topic` | string or null | A stable name for the class of failure; see the next table. `null` when unclassified |
| `title` | string | What happened, in one line |
| `hint` | string or null | Why, what to do, and what is kept. `null` when unclassified |
| `fixes` | array | Up to two `{ label, to }` pairs: a label for the fix and the app route that offers it |
| `terminal` | boolean | `true` when a retry with nothing changed would fail the same way |
| `stage` | string or null | The job phase or step it failed in, when known |
| `detail` | string | Exactly what the runtime or server said |

The `topic` is one of these:

| Topic | Title |
|---|---|
| `gpu_oom` | The GPU ran out of memory |
| `host_memory` | The machine ran out of system memory |
| `paging_file` | Windows could not map the model into memory |
| `backend_mismatch` | The CUDA runtime cannot use this GPU driver |
| `runtime_missing` | The audio.cpp runtime is missing or cannot start |
| `disk_full` | There is not enough disk space |
| `hf_gated` | Hugging Face refused the download |
| `hf_unreachable` | The server could not reach huggingface.co |
| `download_stalled` | The download stopped receiving data |
| `download_truncated` | The download ended before the whole file arrived |
| `model_not_installed` | The model's weights are not on this machine |
| `model_file_corrupt` | The model file could not be read |
| `timeout` | audio.cpp did not finish in time |
| `no_audio` | audio.cpp ran but produced nothing |
| `queue_full` | The queue is full |

## Common errors

Any endpoint can return these errors. The endpoint pages do not repeat them.

| Status | Cause | `detail` |
|---|---|---|
| `401` | The server runs with `MIRLI_API_TOKEN` and the request has no valid token or session cookie. The response carries `WWW-Authenticate: Bearer` | `This Mirli needs its access token. ...` |
| `403` | The `Host` header names a host the server does not serve | `Forbidden host` |
| `403` | A mutating request under `/api/` has neither a non-simple `Content-Type` nor an `X-Soundbox` header | `Mutating API requests need Content-Type: application/json (or an audio upload type) or an X-Soundbox header` |
| `403` | A [capability](https://mirli.app/docs/api-server/index.md#get-capabilities) turns the feature off | `This server doesn't allow that.` |
| `404` | No route matches | `Not Found` |
| `422` | The body failed [validation](#validation-errors) | A list of field errors |
| `429` | The job queue is full | The queue's message |
| `429` | The address sent 30 wrong access tokens in ten minutes. The response carries `Retry-After` | `Too many wrong access tokens from this address. ...` |
| `500` | An unexpected server error | `Internal Server Error` |

The [request rules](https://mirli.app/docs/api/index.md#request-rules) describe the `Host`, mutating
request and access token checks.

## Streaming errors

`POST /api/speak/stream` does not queue. When it cannot start, it returns
`409` with a `reason`:

| `reason` | Cause |
|---|---|
| `unsupported` | The model cannot stream on this audio.cpp build |
| `busy` | A generation holds the GPU. The response carries `Retry-After` |

A failure after the audio has started arrives as an `error` event in the
stream. See [Stream speech](https://mirli.app/docs/api-speech/index.md#stream-speech).

## OpenAI error envelope

The [OpenAI-compatible endpoints](https://mirli.app/docs/api-openai/index.md) under `/v1/` use OpenAI's
error shape for every error, including the common errors:

```json
{
  "error": {
    "message": "No voice is called 'shimmer'.",
    "type": "invalid_request_error",
    "param": "voice",
    "code": "voice_not_found"
  }
}
```

| Field | Description |
|---|---|
| `message` | What went wrong. For a failed read, the same explanation the app shows |
| `type` | `invalid_request_error` for a 4xx other than 429, `rate_limit_error` for 429, `server_error` for 5xx |
| `param` | The request field at fault, or `null` |
| `code` | A stable name; see the next table |
| `detail` | Mirli's own field, on a failed read only: the raw text of the failure |

| `code` | Status | Cause |
|---|---|---|
| `invalid_request` | `400` | The body failed validation, or the multipart form has no `file` |
| `model_not_found` | `400` | `model` names a Mirli model of the wrong kind |
| `voice_not_found` | `400` | `voice` names no voice on this server |
| `unsupported_format` | `400` | `response_format` is not available |
| `unsupported_stream` | `400` | A transcription asked for `stream: true` |
| `invalid_audio` | `400` | The uploaded file cannot be read as audio |
| a failure topic, or `model_error` | `400` | A [model error](#model-errors), such as a model that cannot run |
| `invalid_api_key` | `401` | The access token is wrong or missing |
| `cross_site_request` | `403` | A simple request came from another site |
| `model_not_installed` | `404` | The model, or any model of that kind, is not downloaded |
| `cancelled` | `409` | The job was cancelled from the app |
| `queue_full` | `429` | The job queue is full |
| `gpu_busy` | `429` | A generation holds the GPU |
| `rate_limit_exceeded` | `429` | The address sent 30 wrong access tokens in ten minutes |
| a failure topic, or `generation_failed` | `500` | The read failed while it ran. `code` is the [failure topic](#failure-object) when there is one |
