fix(telegram): handle network errors gracefully

- Add bot.catch() to prevent unhandled rejections from middleware
- Add isRecoverableNetworkError() to retry on transient failures
- Add maxRetryTime and exponential backoff to grammY runner
- Global unhandled rejection handler now logs recoverable errors
  instead of crashing (fetch failures, timeouts, connection resets)

Fixes crash loop when Telegram API is temporarily unreachable.
This commit is contained in:
techboss
2026-01-26 15:25:27 -07:00
committed by Gustavo Madeira Santana
parent a8ad242f88
commit e43f4c0628
4 changed files with 84 additions and 2 deletions

View File

@@ -1,5 +1,17 @@
import { setDefaultAutoSelectFamily } from "net";
import { resolveFetch } from "../infra/fetch.js";
// Workaround for Node.js 22 "Happy Eyeballs" (autoSelectFamily) bug
// that causes intermittent ETIMEDOUT errors when connecting to Telegram's
// dual-stack servers. Disabling autoSelectFamily forces sequential IPv4/IPv6
// attempts which works reliably.
// See: https://github.com/nodejs/node/issues/54359
try {
setDefaultAutoSelectFamily(false);
} catch {
// Ignore if not available (older Node versions)
}
// Prefer wrapped fetch when available to normalize AbortSignal across runtimes.
export function resolveTelegramFetch(proxyFetch?: typeof fetch): typeof fetch | undefined {
if (proxyFetch) return resolveFetch(proxyFetch);