#12870: fix: recover from telegram fetch errors (issue #12835)
channel: telegram
stale
Cluster:
Network Error Handling Improvements
# Fix: Gateway crashes on Telegram fetch errors (Issue #12835)
## Description
This PR fixes a bug where the gateway would crash when checking for Telegram updates if the network connection failed with a native `fetch failed` error (e.g., `TypeError: fetch failed`).
Previously, the unhandled rejection handler in `src/telegram/monitor.ts` only suppressed errors that were BOTH `GrammyHttpError` AND recoverable network errors. Native fetch errors are not wrapped in `GrammyHttpError`, so they were allowed to bubble up and crash the process.
This change relaxes the check to suppress **any** error that matches `isRecoverableTelegramNetworkError`, regardless of its type wrapper.
## Related Issue
Fixes #12835
## Changes
- Modified `src/telegram/monitor.ts` to remove the `isGrammyHttpError` check in the unhandled rejection handler.
- Removed the unused `isGrammyHttpError` helper function.
## Verification
- Created a reproduction script simulating a `TypeError: fetch failed` with `ConnectTimeoutError` cause.
- Verified that the new logic correctly identifies and suppresses this error.
- Ran `pnpm check` and `pnpm test` to ensure no regressions.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Changes tighten gateway resilience to Telegram polling/network failures by broadening the unhandled-rejection suppression logic in `src/telegram/monitor.ts` to rely solely on `isRecoverableTelegramNetworkError` (instead of requiring a grammY `HttpError` wrapper), and removing the now-unused `isGrammyHttpError` helper.
Adds a new Vitest reproduction covering a native `TypeError("fetch failed")` with a timeout-like `cause` chain to ensure the classifier recognizes this scenario.
<h3>Confidence Score: 3/5</h3>
- This PR is close to merge-ready but currently fails due to an incorrect/unused import in the new test and a misleading comment in the telegram monitor.
- Core logic change is small and aligns with the stated bug (native fetch errors not wrapped in HttpError), but the added test imports a non-exported symbol from `src/telegram/monitor.ts`, which should cause the test suite to fail. There’s also a stale comment suggesting `HttpError` gating that no longer exists.
- test/reproduce_issue_12835.ts, src/telegram/monitor.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#7558: fix: Handle Grammy/Telegram network errors to prevent gateway crashes
by kaigritun · 2026-02-03
86.5%
#7141: fix(telegram): unify network error detection to prevent poll crashes
by hclsys · 2026-02-02
85.8%
#7247: fix(telegram): abort stale getUpdates connections after long-poll t...
by JanderV · 2026-02-02
82.1%
#6463: fix(telegram): improve timeout handling and prevent channel exits
by ai-fanatic · 2026-02-01
82.0%
#11101: fix: handle AbortError and WebSocket 1006 in unhandled rejection ha...
by Nipurn123 · 2026-02-07
81.2%
#17243: fix(telegram): catch getFile network failures to prevent gateway cr...
by robbyczgw-cla · 2026-02-15
79.7%
#12936: fix(telegram): omit message_thread_id for private DM chats
by omair445 · 2026-02-09
79.3%
#13881: fix: Address Greptile feedback - test isolation and channel resolution
by trevorgordon981 · 2026-02-11
79.1%
#6447: fix(telegram): auto-restart polling when grammY runner exits silently
by AugmentAdvertise · 2026-02-01
79.1%
#8166: fix(telegram): lifecycle fixes for duplicate messages and auto-reco...
by cheenu1092-oss · 2026-02-03
78.8%