diff --git a/lib/telegram.mjs b/lib/telegram.mjs index 26075d8..0b07481 100644 --- a/lib/telegram.mjs +++ b/lib/telegram.mjs @@ -2,7 +2,6 @@ import { RetryableError, wrapRetryable } from "./retryable.mjs"; const request = wrapRetryable(async function request(telegramToken, method, chat_id, body) { let response; - let json; try { response = await fetch(`https://api.telegram.org/bot${telegramToken}/${method}`, { method: "POST", @@ -12,9 +11,18 @@ const request = wrapRetryable(async function request(telegramToken, method, chat ...body }) }); - json = await JSON.parse(response); } catch (error) { - throw new RetryableError(`Telegram general error`, true, error); + throw new RetryableError(`Telegram fetch error`, true, error); + } + const text = await response.text(); + if (response.status === 502) { + throw new RetryableError(`Telegram error 502 (${text})`, true); + } + let json; + try { + json = JSON.parse(text); + } catch (error) { + throw new RetryableError(`Telegram JSON parse error (${text})`, true, error); } if (!json.ok) { let canRetryOrRetryAfter = response.status === 502; @@ -22,7 +30,7 @@ const request = wrapRetryable(async function request(telegramToken, method, chat const { retry_after } = json.parameters ?? { retry_after: 10 }; canRetryOrRetryAfter = retry_after; } - throw new RetryableError(`Telegram error ${response.status} (${JSON.stringify(json)})`, canRetryOrRetryAfter); + throw new RetryableError(`Telegram error ${response.status} (${text})`, canRetryOrRetryAfter); } return json.result; }); diff --git a/package.json b/package.json index c75e755..cc7d750 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tokentx-notifier", - "version": "1.4.3", + "version": "1.4.4", "description": "A telegram bot that sends a message when new ERC20 transactions are detected", "keywords": ["telegram", "crypto", "ethereum", "erc20", "bot"], "homepage": "https://github.com/varanauskas/tokentx-notifier",