fix: scope telegram polling restart to telegram errors (#43799)

* fix: scope telegram polling restart to telegram errors

* fix: make telegram error tagging best-effort

* fix: scope telegram polling restart to telegram errors (#43799)
This commit is contained in:
Ayaan Zaidi
2026-03-12 13:14:17 +05:30
committed by GitHub
parent 82e3ac21ee
commit ed0ec57a7b
7 changed files with 251 additions and 21 deletions

View File

@@ -5,6 +5,8 @@ import {
readErrorName,
} from "../infra/errors.js";
const TELEGRAM_NETWORK_ORIGIN = Symbol("openclaw.telegram.network-origin");
const RECOVERABLE_ERROR_CODES = new Set([
"ECONNRESET",
"ECONNREFUSED",
@@ -101,6 +103,51 @@ function getErrorCode(err: unknown): string | undefined {
}
export type TelegramNetworkErrorContext = "polling" | "send" | "webhook" | "unknown";
export type TelegramNetworkErrorOrigin = {
method?: string | null;
url?: string | null;
};
function normalizeTelegramNetworkMethod(method?: string | null): string | null {
const trimmed = method?.trim();
if (!trimmed) {
return null;
}
return trimmed.toLowerCase();
}
export function tagTelegramNetworkError(err: unknown, origin: TelegramNetworkErrorOrigin): void {
if (!err || typeof err !== "object") {
return;
}
Object.defineProperty(err, TELEGRAM_NETWORK_ORIGIN, {
value: {
method: normalizeTelegramNetworkMethod(origin.method),
url: typeof origin.url === "string" && origin.url.trim() ? origin.url : null,
} satisfies TelegramNetworkErrorOrigin,
configurable: true,
});
}
export function getTelegramNetworkErrorOrigin(err: unknown): TelegramNetworkErrorOrigin | null {
for (const candidate of collectTelegramErrorCandidates(err)) {
if (!candidate || typeof candidate !== "object") {
continue;
}
const origin = (candidate as Record<PropertyKey, unknown>)[TELEGRAM_NETWORK_ORIGIN];
if (!origin || typeof origin !== "object") {
continue;
}
const method = "method" in origin && typeof origin.method === "string" ? origin.method : null;
const url = "url" in origin && typeof origin.url === "string" ? origin.url : null;
return { method, url };
}
return null;
}
export function isTelegramPollingNetworkError(err: unknown): boolean {
return getTelegramNetworkErrorOrigin(err)?.method === "getupdates";
}
/**
* Returns true if the error is safe to retry for a non-idempotent Telegram send operation