Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Improve error tracing in TelegramError for debugging purposes #2058

@nyancodeid

Description

@nyancodeid

Is your feature request related to a problem? Please describe.
When using the telegraf package, errors such as TelegramError (e.g., 400: Bad Request: BUTTON_USER_PRIVACY_RESTRICTED) lack stack traces pointing to the application code that triggered the Telegram API request. This forces developers to guess which part of their codebase caused the error, leading to:

  • Time-consuming manual debugging to isolate the problematic API call.
  • Inability to quickly correlate errors with specific workflows (e.g., button creation, message edits).

Example error log:

TelegramError: 400: Bad Request: BUTTON_USER_PRIVACY_RESTRICTED
    at new TelegramError (/redacted_path/node_modules/telegraf/lib/core/network/error.js:6:9)
    at /redacted_path/node_modules/telegraf/lib/core/network/client.js:315:19

The stack trace above lacks references to the application code where the failing Telegram API method (e.g., sendMessage, editButton, etc.) was called.

Describe the solution you'd like
Enhance the TelegramError class to preserve the original stack trace from the point where the Telegram API method was invoked in userland code. This would allow developers to see:

  • The exact location in their application code that triggered the error.
  • Contextual details (e.g., method parameters or partial payload) if feasible.

Example ideal error:

TelegramError: 400: Bad Request: BUTTON_USER_PRIVACY_RESTRICTED
    at AppController.sendUserInformation (/app/src/modules/bot/users.ts:127:14)  # <-- User code trace
    at new TelegramError (/node_modules/telegraf/lib/core/network/error.js:6:9)

Describe alternatives you've considered

  1. Manual error wrapping:
    Wrapping all Telegram API calls in try/catch blocks and appending custom context.
    Drawback: Adds boilerplate and is error-prone.

  2. Debug mode:
    Using DEBUG=telegraf:* to trace internal library behavior.
    Drawback: Overwhelms logs and still lacks direct user code correlation.

  3. Third-party error tracking:
    Tools like Sentry or Winston for post-hoc analysis.
    Drawback: Does not solve the root issue of missing stack traces.

Additional context
Critical use case: Errors like BUTTON_USER_PRIVACY_RESTRICTED often occur in complex bot workflows with dynamic buttons, making tracing without context nearly impossible.

telegraf: 4.16.3
bun: v1.1.43
OS: Linux Mint 21.2 x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions