Error handling & rate limiting

Introduction#

How both servers fail and how they slow callers down. Shared error JSON, rate-limit headers, 429 handling, retries, and what happens when a dependency is unhealthy.

Project structure#

The error handling and rate limiting concerns are distributed across:

  • Servers: Telegram Bot Server and Webhook/REST API Server
  • Clients: Telegram API client with built-in retry/backoff and rate-limit handling
  • Services: Notification routing and channel-specific send operations
  • Core: Logging and configuration utilities

Core components#

  • Standardized error response format with code, human-readable message, timestamp, and request ID.
  • Rate limiting policies:
  • Bot Commands: 30 per minute per user
  • REST API: 100 per minute per IP
  • Webhook: unlimited (trusted Telegram servers)
  • Rate limit headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset
  • 429 responses with retry-after guidance
  • Retry/backoff and exponential backoff for Telegram API calls
  • Circuit breaker-like behavior via graceful degradation and fallbacks

Architecture overview#

The error handling and rate limiting architecture is layered:

  • Servers define endpoints and raise HTTP exceptions with standardized error bodies.
  • Services encapsulate cross-channel notification routing and handle per-channel failures gracefully.
  • Clients implement retry/backoff and translate 429 responses into controlled delays.
  • Logging and configuration utilities centralize logging and environment-driven behavior.

Detailed component analysis#

Standardized error response format#

  • All errors follow a consistent JSON shape with code, message, timestamp, and request ID.
  • The API documentation defines the standard error envelope and common error codes mapped to HTTP status codes.

Rate limiting policies and headers#

  • Bot Commands: 30 per minute per user
  • REST API: 100 per minute per IP
  • Webhook: unlimited (trusted Telegram servers)
  • Rate limit headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset
  • 429 responses include a retry-after hint

Telegram bot server error handling#

  • Command handlers perform basic input validation and delegate to services.
  • Exceptions in stats calculation are caught and surfaced to the user.
  • Logging is used for operational visibility.

Webhook/REST API server error handling#

  • Endpoints raise HTTPException with standardized details for misconfiguration or processing failures.
  • Health endpoints return structured responses.
  • Stats endpoints return aggregated data or raise 501/503 when services are not configured.

Telegram client retry and backoff#

  • The Telegram client implements retries with exponential backoff on transient failures.
  • On 429 responses, it reads Retry-After header and waits before retrying.
  • Non-transient failures are logged and retried according to backoff.

Notification service failure handling#

  • Broadcast loops through channels and logs errors per channel without failing the whole operation.
  • Unsolicited notices send path marks as sent only on full success per channel.

Logging and configuration#

  • Centralized logging configuration with environment-driven log levels and daemon mode.
  • Safe printing utilities suppress output in daemon mode and log instead.

Dependency analysis#

  • The Webhook server depends on NotificationService for routing and on TelegramClient for Telegram delivery.
  • The Telegram Bot Server depends on services for DB operations and stats calculation.
  • Logging and configuration utilities are used across servers and services.

Performance considerations#

  • Exponential backoff reduces thundering herds under rate limits.
  • Circuit breaker-like behavior: per-channel failure isolation prevents cascading failures.
  • Graceful degradation: continue processing other items when encountering errors.

[No sources needed since this section provides general guidance]

Troubleshooting guide#

Common scenarios and resolutions:

  • Rate limit exceeded (429): Respect Retry-After and back off. Consider batching or reducing request frequency.
  • Unauthorized or forbidden: Verify authentication tokens and permissions.
  • Service unavailable: Check database connectivity and external service health.
  • Internal errors: Review logs and stack traces for root cause.

Error code reference (HTTP status mappings and guidance):

  • INVALID_REQUEST (400): Fix malformed requests.
  • UNAUTHORIZED (401): Provide valid credentials.
  • FORBIDDEN (403): Insufficient permissions.
  • NOT_FOUND (404): Resource does not exist.
  • CONFLICT (409): Resource conflict; retry with corrected state.
  • RATE_LIMITED (429): Back off and retry later.
  • INTERNAL_ERROR (500): Investigate server-side issues.
  • SERVICE_UNAVAILABLE (503): Dependency failure; retry after recovery.

Conclusion#

Shared error JSON on both servers. Telegram client retries and backs off on 429. Webhook routes should return the same shape so clients only learn one failure format.