> ## Documentation Index
> Fetch the complete documentation index at: https://n-a-818b4087.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Errors and retries

> Catch API, network, and timeout failures with typed exceptions.

All Telegram failures are raised as exceptions instead of returning `False` or silently ignoring the problem.

```python theme={null}
from TelegramAPI import TelegramAPI, TelegramAPIError

telegram = TelegramAPI('YOUR_BOT_TOKEN')

try:
    telegram.send_message(chat_id = 'YOUR_CHAT_ID', text = 'Hello!')
except TelegramAPIError as error:
    print(f'Method: {error.method}')
    print(f'Error code: {error.error_code}')
    print(f'Description: {error.description}')
    print(f'Parameters: {error.parameters}')
```

## Exception hierarchy

```text theme={null}
Exception
└── TelegramAPIError
    └── TelegramNetworkError
        └── TelegramTimeoutError
```

| Exception | Raised when |
| - | - |
| `TelegramAPIError` | Telegram returns an error, an invalid JSON response, or an invalid result |
| `TelegramNetworkError` | A request to Telegram fails at the network level |
| `TelegramTimeoutError` | A request or download times out |

`TelegramTimeoutError` is a subclass of `TelegramNetworkError`, which is a subclass of `TelegramAPIError`. Catching `TelegramAPIError` catches all three.

## Error fields

| Attribute | Description |
| - | - |
| `method` | Telegram method that failed |
| `error_code` | Telegram error code, or the HTTP status when Telegram does not provide one |
| `description` | Human-readable error description |
| `parameters` | Telegram error parameters, such as `retry_after` |
| `response` | The raw error payload when one is available |

The bot token is redacted from exception descriptions and network error text.

## Automatic retries

Rate-limited requests (`429`) are retried automatically up to `max_flood_retries` times, respecting Telegram's `retry_after` value. The wait is capped at 60 seconds per attempt.

Upload streams are rewound before a retry. If an upload uses a non-seekable stream, the request is not retried with empty data; the error is raised instead.

Downloads also retry `429` and `5xx` responses up to `max_flood_retries` times.

## Permanent errors

Permanent errors such as an invalid token or a webhook conflict are raised immediately. They are not retried.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.