> ## Documentation Index
> Fetch the complete documentation index at: https://docs.drime.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Error Handling

> Understanding and handling API errors

## HTTP Status Codes

The Drime Cloud API uses standard HTTP codes:

| Code  | Meaning              |
| ----- | -------------------- |
| `200` | Success              |
| `201` | Created successfully |
| `400` | Bad request          |
| `401` | Unauthenticated      |
| `403` | Access denied        |
| `404` | Resource not found   |
| `422` | Validation error     |
| `429` | Too many requests    |
| `500` | Server error         |

## Error Formats

### Authentication Error (401)

```json theme={null}
{
  "message": "Unauthenticated."
}
```

<Tip>
  Verify that your token is valid and sent in the `Authorization: Bearer TOKEN` header
</Tip>

### Permission Error (403)

```json theme={null}
{
  "message": "This action is unauthorized."
}
```

### Validation Error (422)

```json theme={null}
{
  "status": "error",
  "message": "The given data was invalid.",
  "errors": {
    "email": ["The email field is required."],
    "password": ["The password must be at least 8 characters."]
  }
}
```

### Resource Not Found (404)

```json theme={null}
{
  "message": "No query results for model [App\\FileEntry] 123456"
}
```

## Best Practices

<AccordionGroup>
  <Accordion title="Always check the status" icon="check">
    ```javascript theme={null}
    const response = await fetch(url, options);

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Request failed');
    }
    ```
  </Accordion>

  <Accordion title="Handle validation errors" icon="triangle-exclamation">
    ```javascript theme={null}
    if (response.status === 422) {
      const { errors } = await response.json();
      // Display errors per field
      Object.entries(errors).forEach(([field, messages]) => {
        console.error(`${field}: ${messages.join(', ')}`);
      });
    }
    ```
  </Accordion>

  <Accordion title="Refresh token if expired" icon="rotate">
    ```javascript theme={null}
    if (response.status === 401) {
      // Token expired, reconnect the user
      await refreshToken();
      // Retry the request
    }
    ```
  </Accordion>
</AccordionGroup>

## Rate Limiting

The API limits request rates to ensure stability:

<Warning>
  If you receive a **429** error, wait a few seconds before retrying.
</Warning>

```javascript theme={null}
async function fetchWithRetry(url, options, retries = 3) {
  for (let i = 0; i < retries; i++) {
    const response = await fetch(url, options);
    
    if (response.status === 429) {
      const waitTime = Math.pow(2, i) * 1000; // Exponential backoff
      await new Promise(r => setTimeout(r, waitTime));
      continue;
    }
    
    return response;
  }
  throw new Error('Max retries exceeded');
}
```
