← Back to PRs

#7558: fix: Handle Grammy/Telegram network errors to prevent gateway crashes

by kaigritun open 2026-02-03 00:13 View on GitHub →
Fixes #7553 ## Problem Gateway crashes 3-6 times per day from unhandled Grammy (Telegram) network errors: - `GrammyError: Call to 'sendMessage' failed! (502: Bad Gateway)` - Connection timeouts and other transient failures These errors weren't being caught by the existing transient network error handler. ## Solution Extended `isTransientNetworkError()` to detect Grammy-specific errors: - GrammyError and HttpError names - HTTP 502, 503, 504 status codes - Connection and timeout messages These are now logged as warnings instead of crashing the gateway. ## Changes - Added Grammy error detection in `src/infra/unhandled-rejections.ts` - Added comprehensive tests for all Grammy error cases - Follows existing pattern for network error handling ## Testing Added tests for: - GrammyError with 502, 503, 504 - Connection errors - Timeout errors - Non-network GrammyErrors (still crash as expected) ## Impact Prevents 3-6 crashes per day for users running Telegram bot integrations. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR extends the unhandled rejection transient network classifier to treat certain Telegram/Grammy failures (e.g., 502/503/504 responses and connection/timeout-like messages for `GrammyError`/`HttpError`) as non-fatal, so the gateway logs a warning instead of exiting. It adds unit tests covering those new cases alongside existing transient-network behaviors, fitting into the existing `installUnhandledRejectionHandler()` flow that suppresses/terminates based on error classification. <h3>Confidence Score: 4/5</h3> - This PR looks safe to merge and should reduce crashes, with only minor risk of misclassification due to message heuristics. - Changes are localized to `isTransientNetworkError()` and backed by targeted unit tests. The main remaining risk is reliance on case-sensitive / loose substring and regex matching against error messages, which could miss some transient errors or (less likely) classify unrelated errors as transient. - src/infra/unhandled-rejections.ts (message matching heuristics) <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs