# Error codes

> Reference of agdb_server HTTP status codes, embedded DbError types and the errors of the Rust, TypeScript, PHP and Python API clients

This page lists the errors you can meet when working with `agdb`: the HTTP status codes of the [server](/docs/references/server), the `DbError` of the embedded database and the error types of the API clients. For symptoms and fixes of common problems see also the [troubleshooting](/docs/guides/troubleshooting) guide.

## Server

A failed request returns a status code and the error description as the plain text body of the response. The OpenAPI specification (see [API docs](/api-docs/openapi)) lists the codes of each endpoint.

### Custom codes (46x - 47x, 52x)

The server uses codes in the 4xx and 5xx ranges that are not defined by HTTP to tell apart the errors of `agdb` itself.

<table>
<thead>
  <tr>
    <th>
      Code
    </th>
    
    <th>
      Body
    </th>
    
    <th>
      Meaning
    </th>
    
    <th>
      Typical cause
    </th>
    
    <th>
      What to do
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      461
    </td>
    
    <td>
      <code>
        password too short (<8)
      </code>
    </td>
    
    <td>
      The password is shorter than 8
    </td>
    
    <td>
      Creating a user or changing one's own password
    </td>
    
    <td>
      Use a password of at least 8 characters
    </td>
  </tr>
  
  <tr>
    <td>
      462
    </td>
    
    <td>
      <code>
        name too short (<3)
      </code>
    </td>
    
    <td>
      The user name is shorter than 3
    </td>
    
    <td>
      Creating a user
    </td>
    
    <td>
      Use a user name of at least 3 characters
    </td>
  </tr>
  
  <tr>
    <td>
      463
    </td>
    
    <td>
      <code>
        user exists
      </code>
    </td>
    
    <td>
      The user already exists
    </td>
    
    <td>
      Creating a user with a taken name
    </td>
    
    <td>
      Choose another name
    </td>
  </tr>
  
  <tr>
    <td>
      465
    </td>
    
    <td>
      <code>
        db already exists
      </code>
    </td>
    
    <td>
      The database already exists
    </td>
    
    <td>
      Adding, copying or renaming a database to a name that is taken
    </td>
    
    <td>
      Choose another name or delete the existing database
    </td>
  </tr>
  
  <tr>
    <td>
      467
    </td>
    
    <td>
      usually <code>
        db invalid: <details>
      </code>
    </td>
    
    <td>
      The database name or file is not valid
    </td>
    
    <td>
      The name is empty, starts with <code>
        .
      </code>
      
      , contains <code>
        /
      </code>
      
      , <code>
        \
      </code>
      
      , <code>
        :
      </code>
      
       or NUL, or is <code>
        audit
      </code>
      
       or <code>
        backups
      </code>
      
      ; also a database file that cannot be opened or created
    </td>
    
    <td>
      Fix the name (also of the new name of a copy or rename), check the file
    </td>
  </tr>
  
  <tr>
    <td>
      470
    </td>
    
    <td>
      the description of the <code>
        DbError
      </code>
    </td>
    
    <td>
      A query failed in the database
    </td>
    
    <td>
      An invalid query, e.g. a missing id or alias, a type mismatch or an invalid reference to a result of an earlier query (<code>
        :0
      </code>
      
      ) in a batch
    </td>
    
    <td>
      Fix the query, see <a href="#embedded-errors">
        Embedded errors
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      471
    </td>
    
    <td>
      <code>
        storage error: <details>
      </code>
    </td>
    
    <td>
      The storage of the node failed
    </td>
    
    <td>
      A file system or database file error, an invalid snapshot, or an action the cluster leader gave up on after a fault (see <a href="#cluster">
        Cluster
      </a>
      
      )
    </td>
    
    <td>
      Check the disk and the server log; in a cluster check the data afterward
    </td>
  </tr>
  
  <tr>
    <td>
      520
    </td>
    
    <td>
      <code>
        <message>
      </code>
    </td>
    
    <td>
      The outcome of the request is unknown
    </td>
    
    <td>
      The leader lost the leadership, the node stopped, a forward failed after it may have reached the leader or the request took more than 50 s (see <a href="#cluster">
        Cluster
      </a>
      
      )
    </td>
    
    <td>
      Check the state before repeating a request that is not safe to repeat
    </td>
  </tr>
  
  <tr>
    <td>
      521
    </td>
    
    <td>
      <code>
        <message>
      </code>
    </td>
    
    <td>
      The request committed, its result is lost
    </td>
    
    <td>
      The node stopped or resynced before it applied the request, the message has its log index (see <a href="#cluster">
        Cluster
      </a>
      
      )
    </td>
    
    <td>
      The request is final, the data shows its outcome once applied
    </td>
  </tr>
</tbody>
</table>

The password check counts the length of the value in bytes. It runs when a user is created and when users change their own password (not on the admin password change). User names are checked only when a user is created.

### Standard codes

<table>
<thead>
  <tr>
    <th>
      Code
    </th>
    
    <th>
      Meaning
    </th>
    
    <th>
      Typical cause
    </th>
    
    <th>
      What to do
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      400
    </td>
    
    <td>
      Bad request
    </td>
    
    <td>
      An invalid user name (<code>
        invalid user name '<name>': ...
      </code>
      
      ) or dashboard name (<code>
        invalid dashboard name '<name>': use 1-... characters [A-Za-z0-9_.-], not starting with '.'
      </code>
      
      ), malformed JSON or an undecodable binary (<code>
        application/vnd.agdb.api.v1
      </code>
      
      ) body, an invalid <code>
        log_level
      </code>
    </td>
    
    <td>
      Fix the request
    </td>
  </tr>
  
  <tr>
    <td>
      401
    </td>
    
    <td>
      Unauthorized
    </td>
    
    <td>
      A missing, unknown or expired token (the response has no body), wrong user name or password on login (<code>
        unauthorized
      </code>
      
      ), a token of a user that is not the server admin on <code>
        /api/v1/admin/*
      </code>
      
      , a wrong <code>
        cluster_token
      </code>
      
       on the cluster endpoints
    </td>
    
    <td>
      Log in again, see <a href="/docs/guides/troubleshooting">
        troubleshooting
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      403
    </td>
    
    <td>
      Forbidden
    </td>
    
    <td>
      The user lacks the required role or the operation is not allowed, e.g. a wrong current password when changing it (<code>
        invalid credentials
      </code>
      
      ) or adding a database or dashboard to another user
    </td>
    
    <td>
      Use a user with the role needed, see <a href="#permission-denied">
        Permission denied
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      404
    </td>
    
    <td>
      Not found
    </td>
    
    <td>
      A missing user (<code>
        user not found: <name>
      </code>
      
      ), database (<code>
        db not found: <owner>/<db>
      </code>
      
      ), dashboard (<code>
        dashboard not found: <name>
      </code>
      
      ), backup (<code>
        backup not found
      </code>
      
      ), session or token. A dashboard the user has no role on is reported as not found as well
    </td>
    
    <td>
      Check the names and the owner
    </td>
  </tr>
  
  <tr>
    <td>
      409
    </td>
    
    <td>
      Conflict
    </td>
    
    <td>
      A dashboard to add or rename to already exists (<code>
        dashboard already exists: <name>
      </code>
      
      )
    </td>
    
    <td>
      Choose another name
    </td>
  </tr>
  
  <tr>
    <td>
      413
    </td>
    
    <td>
      Payload too large
    </td>
    
    <td>
      The request body is larger than <code>
        request_body_limit
      </code>
      
      , a dashboard config is larger than the maximum (<code>
        dashboard config too large (>N bytes)
      </code>
      
      ) or a request is larger than a cluster append (<code>
        the request is larger than a cluster append
      </code>
      
      )
    </td>
    
    <td>
      Send less data or raise <code>
        request_body_limit
      </code>
      
       / <code>
        cluster_max_append_size
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      415
    </td>
    
    <td>
      Unsupported media type
    </td>
    
    <td>
      A JSON body sent without <code>
        Content-Type: application/json
      </code>
      
      , e.g. curl without <code>
        -H 'Content-Type: application/json'
      </code>
    </td>
    
    <td>
      Set the header
    </td>
  </tr>
  
  <tr>
    <td>
      422
    </td>
    
    <td>
      Unprocessable entity
    </td>
    
    <td>
      JSON that does not match the expected fields
    </td>
    
    <td>
      Fix the body, see the <a href="/api-docs/openapi">
        OpenAPI
      </a>
      
       specification
    </td>
  </tr>
  
  <tr>
    <td>
      500
    </td>
    
    <td>
      Internal server error
    </td>
    
    <td>
      An unexpected failure of the server that is not a storage error, e.g. a failed internal channel or a database error that is not a query error
    </td>
    
    <td>
      Check the server log
    </td>
  </tr>
  
  <tr>
    <td>
      503
    </td>
    
    <td>
      Service unavailable
    </td>
    
    <td>
      The cluster cannot execute the request right now, see <a href="#cluster">
        Cluster
      </a>
    </td>
    
    <td>
      Retry later
    </td>
  </tr>
</tbody>
</table>

### Permission denied

A `403` caused by a role carries the body `permission denied: <reason>`. The reasons used by the server are `admin only` (the database or dashboard admin role is needed, e.g. for backup, restore, rollback, clear, convert, audit trim and managing users; a non-admin token on `/api/v1/admin/*` gets `401`), `owner only`, `write rights required`, `dashboard users only`, `cannot change role of db owner`, `cannot change role of dashboard owner`, `cannot remove owner`, `cannot add dashboard to another user` and `mutable queries not allowed, use exec_mut endpoint` (a mutable query sent to `exec`). The roles are described in the [server](/docs/references/server) reference.

### Cluster

In a [cluster](/docs/references/server) a write goes through the leader and a node that is not the leader forwards it. A `503` was not executed and is safe to retry, its body tells the reason:

<table>
<thead>
  <tr>
    <th>
      Body
    </th>
    
    <th>
      Reason
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        cluster is not ready yet
      </code>
    </td>
    
    <td>
      The node has not joined the cluster yet
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        this node is not the leader, the leader is node <n>
      </code>
      
       / <code>
        the cluster has no leader
      </code>
    </td>
    
    <td>
      No leader known (election in progress) or the leadership moved
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the leader has not heard from a majority of the cluster
      </code>
    </td>
    
    <td>
      Too many nodes are unreachable
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the cluster is busy
      </code>
    </td>
    
    <td>
      The log entries not yet executed exceed <code>
        cluster_max_pending_bytes
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the cluster node is shutting down
      </code>
    </td>
    
    <td>
      The node is stopping
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        forward to leader failed to connect: <details>
      </code>
    </td>
    
    <td>
      The node could not connect to the leader (retried before failing)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the password hash did not complete: <details>
      </code>
    </td>
    
    <td>
      Hashing a password on login or a password change failed
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        db unavailable: <owner>/<db>: <reason>
      </code>
    </td>
    
    <td>
      The files of the database could not be opened or a file operation on them is pending; <code>
        /admin/optimize
      </code>
      
       retries it
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        another backup or a snapshot install is running on this node
      </code>
      
       / <code>
        the cluster node is unavailable for backup
      </code>
    </td>
    
    <td>
      <code>
        /admin/backup
      </code>
      
       could not start
    </td>
  </tr>
</tbody>
</table>

A `520` may or may not have been executed:

<table>
<thead>
  <tr>
    <th>
      Body
    </th>
    
    <th>
      Reason
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        the leadership was lost, the request may or may not have been executed
      </code>
    </td>
    
    <td>
      The leader changed while the request was running
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the cluster node is shutting down, the request may or may not have been executed
      </code>
    </td>
    
    <td>
      The node stopped while the request was running
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the request did not finish in time, it may or may not have been executed
      </code>
    </td>
    
    <td>
      The request took more than 50 seconds
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        forward to leader failed: <details>
      </code>
    </td>
    
    <td>
      The forward failed after it may have reached the leader
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        the action faulted and its resources could not be stamped, it may still take effect when replayed
      </code>
    </td>
    
    <td>
      The leader failed to mark the data of an action that faulted
    </td>
  </tr>
</tbody>
</table>

A `521` (`the request was committed at index <n>, its result is lost on this node`) was executed: the request is final and the data shows its outcome once the node applied it.

Check the state (e.g. with a query) before repeating a `520` request that is not safe to repeat. An action the leader itself failed to run is not retried: it fails with `471` (`storage error: the action faulted, its resources are accepted as they are`) and the data it touched is kept as it is on the leader. See the cluster section of the [server](/docs/references/server) reference for details.

## Embedded errors

The embedded database reports every failure as a `DbError` with a `category` (`Collections`, `Db`, `Graph`, `Query`, `Serialization`, `Storage`), a `ty` of `DbErrorType`, a `description`, an optional `cause` and the source location. The error is printed as `[<category>:<ty>] <description> (at <location>)` followed by the cause chain. More in the [queries](/docs/references/queries#dberror) reference.

<table>
<thead>
  <tr>
    <th>
      <code>
        DbErrorType
      </code>
    </th>
    
    <th>
      Meaning
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        DbCreate
      </code>
    </td>
    
    <td>
      Failed to create a new database file or structure
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        InvalidIndex
      </code>
    </td>
    
    <td>
      An internal index was invalid or corrupted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        NotAllowed
      </code>
    </td>
    
    <td>
      The requested operation is not permitted
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        NotEnoughData
      </code>
    </td>
    
    <td>
      Not enough data was available to complete deserialization
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        NotFound
      </code>
    </td>
    
    <td>
      The requested element, alias or key was not found
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        OutOfBounds
      </code>
    </td>
    
    <td>
      An index was outside the valid range
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        Poisoned
      </code>
    </td>
    
    <td>
      A lock was poisoned by a panicked thread
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        TypeError
      </code>
    </td>
    
    <td>
      A type conversion or mismatch error
    </td>
  </tr>
</tbody>
</table>

Create your own error with one of the constructors named after the category (`collections`, `db`, `graph`, `query`, `serialization`, `storage`) and add a cause with `caused_by`:

```rs
use agdb::DbError;
use agdb::DbErrorType;

let error = DbError::db(DbErrorType::NotAllowed, "name must not be empty");
let wrapped = DbError::query(DbErrorType::NotFound, "user").caused_by(error);
```

The closure of a transaction may return any error type `E` that implements `From<DbError>`, so errors of queries convert with `?` and you can add your own:

```rs
use agdb::Db;
use agdb::DbError;
use agdb::DbId;
use agdb::QueryBuilder;

enum MyError {
    Db(DbError),
    Invalid,
}

impl From<DbError> for MyError {
    fn from(error: DbError) -> Self {
        MyError::Db(error)
    }
}

fn add_node(db: &Db, valid: bool) -> Result<DbId, MyError> {
    db.transaction_mut(|t| {
        let result = t.exec_mut(QueryBuilder::insert().nodes().count(1).query())?;

        if !valid {
            return Err(MyError::Invalid); // the transaction is rolled back
        }

        Ok(result.elements[0].id)
    })
}
```

On the server a `DbError` of a query is returned as `470` and one that originates in the storage (or a poisoned lock) as `471`.

## Clients

All API clients report a failed request with an error carrying the HTTP `status` and the `description` (the body of the response). The error is printed as `<status>: <description>` (in TypeScript with `e.to_string()`). A request that did not get a response (e.g. the connection failed) is reported with the status `500` and the description of the transport error.

<table>
<thead>
  <tr>
    <th>
      Client
    </th>
    
    <th>
      Error type
    </th>
    
    <th>
      Status and description
    </th>
    
    <th>
      Transport failure
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Rust
    </td>
    
    <td>
      <code>
        agdb_api::AgdbApiError
      </code>
      
       (a struct, <code>
        std::error::Error
      </code>
      
      )
    </td>
    
    <td>
      <code>
        status: u16
      </code>
      
      , <code>
        description: String
      </code>
    </td>
    
    <td>
      Status <code>
        500
      </code>
      
       when <code>
        reqwest
      </code>
      
       reports no status, the description is its message
    </td>
  </tr>
  
  <tr>
    <td>
      TypeScript
    </td>
    
    <td>
      <code>
        AgdbApiError
      </code>
      
       (a class)
    </td>
    
    <td>
      <code>
        status: number
      </code>
      
      , <code>
        description: string
      </code>
    </td>
    
    <td>
      Thrown by <code>
        fetch_client
      </code>
      
       with status <code>
        500
      </code>
      
       and the message
    </td>
  </tr>
  
  <tr>
    <td>
      PHP
    </td>
    
    <td>
      <code>
        AgdbApiError
      </code>
      
       (extends <code>
        \Exception
      </code>
      
      )
    </td>
    
    <td>
      <code>
        public int $status
      </code>
      
      , <code>
        public string $description
      </code>
    </td>
    
    <td>
      Thrown by <code>
        CurlClient
      </code>
      
       with status <code>
        500
      </code>
      
       and the cURL error
    </td>
  </tr>
  
  <tr>
    <td>
      Python
    </td>
    
    <td>
      <code>
        AgdbApiError
      </code>
      
       (extends <code>
        Exception
      </code>
      
      )
    </td>
    
    <td>
      <code>
        status: int
      </code>
      
      , <code>
        description: str
      </code>
    </td>
    
    <td>
      Raised by <code>
        UrllibClient
      </code>
      
       with status <code>
        500
      </code>
      
       and the message
    </td>
  </tr>
</tbody>
</table>

The Rust client uses the status `0` for errors that do not originate from an HTTP response: a failure to parse JSON, a `DbError` (converted with `From<DbError>`) or a response of unexpected size.

```rs
match client.user_login("alice", "password").await {
    Ok(_) => {}
    Err(e) if e.status == 401 => println!("wrong credentials: {e}"),
    Err(e) => println!("failed: {e}"),
}
```

```ts
try {
  await client.user_login({ username: "alice", password: "password" });
} catch (e) {
  if (e instanceof AgdbApiError && e.status === 401) {
    console.log(e.to_string());
  }
}
```

```php
try {
    $client->user_login("alice", "password");
} catch (AgdbApiError $e) {
    echo $e->status . ": " . $e->description;
}
```

```python
try:
    client.user_login("alice", "password")
except AgdbApiError as e:
    print(e.status, e.description)
```
