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

# Error Handling

> How to handle API errors

# Error Handling

The API returns standard HTTP status codes to indicate success or failure of requests.

## Error Response Format

```json theme={null}
{
  "code": "not_found",
  "message": "The requested resource was not found"
}
```

## Common Error Codes

| Status | Error Type       | Description                       |
| ------ | ---------------- | --------------------------------- |
| 400    | Bad Request      | Invalid request parameters        |
| 401    | Unauthorized     | Missing or invalid authentication |
| 403    | Forbidden        | Insufficient permissions          |
| 404    | Not Found        | Resource does not exist           |
| 422    | Validation Error | Request validation failed         |
| 429    | Rate Limited     | Too many requests                 |
| 500    | Server Error     | Internal server error             |

## SDK Error Handling

<CodeGroup>
  ```python Python theme={null}
  from meibel import MeibelClient
  from meibel.exceptions import ApiError, NotFoundError, RateLimitError

  client = MeibelClient(api_key="your-api-key")

  try:
      result = client.items.get_item(id="item-123")
  except NotFoundError as e:
      print(f"Item not found: {e.message}")
  except RateLimitError as e:
      print(f"Rate limited. Retry after {e.retry_after} seconds")
  except ApiError as e:
      print(f"API error: {e.status_code} - {e.message}")
  ```

  ```typescript TypeScript theme={null}
  import { MeibelClient, ApiError, NotFoundError, RateLimitError } from 'meibel';

  const client = new MeibelClient({ apiKey: 'your-api-key' });

  try {
    const result = await client.items.getItem('item-123');
  } catch (error) {
    if (error instanceof NotFoundError) {
      console.log('Item not found:', error.message);
    } else if (error instanceof RateLimitError) {
      console.log('Rate limited. Retry after:', error.retryAfter, 'seconds');
    } else if (error instanceof ApiError) {
      console.log('API error:', error.statusCode, error.message);
    }
  }
  ```

  ```go Go theme={null}
  import (
      "errors"
      "github.com/meibel-ai/meibel-go/v2"
  )

  client := v2.NewClient(v2.WithAPIKey("your-api-key"))

  result, err := client.Items.GetItem(ctx, "item-123")
  if err != nil {
      var notFoundErr *v2.NotFoundError
      var rateLimitErr *v2.RateLimitError
      var apiErr *v2.APIError

      switch {
      case errors.As(err, &notFoundErr):
          fmt.Println("Item not found:", notFoundErr.Message)
      case errors.As(err, &rateLimitErr):
          fmt.Println("Rate limited. Retry after:", rateLimitErr.RetryAfter)
      case errors.As(err, &apiErr):
          fmt.Println("API error:", apiErr.StatusCode, apiErr.Message)
      default:
          fmt.Println("Unknown error:", err)
      }
  }
  ```
</CodeGroup>

## Retry Strategy

For transient errors (429, 5xx), we recommend implementing exponential backoff:

1. Wait 1 second, then retry
2. If still failing, wait 2 seconds, then retry
3. If still failing, wait 4 seconds, then retry
4. Maximum of 3 retries

<Note>
  The SDKs implement automatic retry with exponential backoff for transient errors.
</Note>
