BotError<C>
Thrown when middleware throws an error. Wraps the original error and provides access to the context.Properties
error: The original error that was thrown by your middlewarectx: The context object being processed when the error occurredmessage: A descriptive error messagename: Always"BotError"stack: Stack trace from the original error (if available)
When it occurs
BotError is thrown whenever an error occurs during middleware execution:
Handling BotError
Usebot.catch() to install an error handler:
Accessing context from errors
Thectx property lets you access update information when an error occurs:
Example: Retry on error
GrammyError
Thrown when the Telegram Bot API returns an error response.Properties
error_code: Telegram’s error code (e.g., 400, 404, 429)description: Human-readable error description from Telegramparameters: Additional parameters (e.g.,retry_afterfor rate limits)method: The API method that was called (e.g.,"sendMessage")payload: The parameters passed to the API methodok: Alwaysfalse
Common Error Codes
| Code | Description |
|---|---|
| 400 | Bad Request - invalid parameters |
| 401 | Unauthorized - invalid bot token |
| 403 | Forbidden - no permission to perform action |
| 404 | Not Found - resource doesn’t exist |
| 409 | Conflict - bot is already running elsewhere |
| 429 | Too Many Requests - rate limited |
| 500 | Internal Server Error - Telegram server issue |
When it occurs
GrammyError is thrown when Telegram’s API returns an error:
Example: Handling specific errors
Example: Handling message deletion
Example: Handling bot blocking
ResponseParameters
Some errors include additional parameters:HttpError
Thrown when the HTTP request to Telegram’s servers fails.Properties
error: The underlying network errormessage: Descriptive error messagename: Always"HttpError"
When it occurs
HttpError is thrown when:
- Network connection fails
- DNS resolution fails
- Request times out
- Connection is aborted
- API transformer function throws
Example: Retry on network failure
Error Handling Patterns
Global Error Handler
Handle all errors in one place:Local Error Handling
Handle errors in specific middleware:Error Boundaries
Create protected middleware sections:Ignore Specific Errors
Silently handle expected errors:Custom Error Classes
Create application-specific errors:Error Logging
Log errors with context:Best Practices
-
Always set an error handler
-
Handle specific errors explicitly
-
Don’t leak sensitive information
-
Implement retry logic for transient errors
-
Fail gracefully