diff --git a/src/errors.ts b/src/errors.ts index 2173aab..766c404 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,8 +1,7 @@ import { TelegramErrorResponse } from "./types/telegram"; import { AxiosError, AxiosResponse, Method } from "axios"; -export const ERROR_TELEBOT_ALREADY_RUNNING = "Telebot is already running. Terminate instance for safety."; -export const ERROR_TELEBOT_MAXIMUM_RETRY = "Maximum retries exceeded. Terminate instance for safety."; +export const ERROR_TELEBOT_MAXIMUM_RETRY = "Maximum retries exceeded. Terminate instance for safety."; // TODO export type SomeKindOfError = TeleBotError | TelegramError | Error | EvalError | RangeError | ReferenceError | SyntaxError | TypeError | URIError; diff --git a/src/telebot.ts b/src/telebot.ts index fe62344..1507e71 100644 --- a/src/telebot.ts +++ b/src/telebot.ts @@ -34,13 +34,7 @@ import { User, WebhookInfo } from "./types/telegram"; -import { - ERROR_TELEBOT_ALREADY_RUNNING, - handleTelegramResponse, - normalizeError, - SomeKindOfError, - TeleBotError -} from "./errors"; +import { handleTelegramResponse, normalizeError, SomeKindOfError, TeleBotError } from "./errors"; import { Levels, LID, TeleBotLogger, TeleBotLogOptions } from "./telebot/logger"; import { TELEGRAM_UPDATE_PROCESSORS, TelegramUpdateProcessors } from "./telebot/processors"; import { ALLOWED_WEBHOOK_PORTS, craftWebhookPath, creteWebhookServer } from "./telebot/webhook"; @@ -203,7 +197,6 @@ export class TeleBot { } = this.polling; const webhook = this.webhook; - const deleteWebhook = async () => await this.deleteWebhook(); if (!this.hasFlag("isRunning")) { this.setFlag("isRunning"); @@ -230,24 +223,24 @@ export class TeleBot { } } else if (interval && interval > 0) { - await deleteWebhook(); + await this.deleteWebhook(); this.startLifeInterval(interval); } else if (interval === false) { - await deleteWebhook(); + await this.deleteWebhook(); this.startLifeCycle(); } } catch (e) { const error = normalizeError(e); this.dispatch("error", error); this.logger.error(LID.TeleBot, { error }); + this.stop(); // eslint-disable-next-line no-console console.error("==== TELEBOT GLOBAL ERROR ====", error); - this.stop(); return Promise.reject(error); } } else { this.stop(); - throw new TeleBotError(ERROR_TELEBOT_ALREADY_RUNNING); + throw new TeleBotError("Telebot is already running. Terminate instance for safety."); } } diff --git a/src/telebot/webhook.ts b/src/telebot/webhook.ts index 400949e..d70b9dc 100644 --- a/src/telebot/webhook.ts +++ b/src/telebot/webhook.ts @@ -17,35 +17,31 @@ export function craftWebhookPath(url: string, token: string): string { const listener = (bot: TeleBot, path: string) => (request: IncomingMessage, response: ServerResponse) => { if (request.url === path && request.method === "POST") { const body: string[] = []; - request.on("data", (chunk) => body.push(chunk)); request.on("end", () => { - let update: Update[]; - try { - update = convertToArray(JSON.parse(body.join())); + const update = convertToArray(JSON.parse(body.join())); + + bot.logger.debug(LID.Webhook, { meta: { update } }); + + bot.processTelegramUpdates(update).catch((e) => { + const error = normalizeError(e); + bot.logger.error(LID.Webhook, { error }); + response.writeHead(500); + return error; + }).finally(() => { + response.end(); + }); } catch (error) { bot.logger.error(LID.Webhook, { error }); response.writeHead(415); response.end(); - return error; } - - bot.logger.debug(LID.Webhook, { meta: { update } }); - - bot.processTelegramUpdates(update).catch((e) => { - const error = normalizeError(e); - bot.logger.error(LID.Webhook, { error }); - response.writeHead(500); - return error; - }).finally(() => { - response.end(); - }); }); + } else { + response.writeHead(404); + response.end(); } - - response.writeHead(404); - response.end(); }; export function creteWebhookServer(bot: TeleBot, options: WebhookServer & { url: string }): Promise {