From c8bb8ba320c9ac208abe041ce84738b65e85a4fb Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Thu, 16 Jan 2025 11:34:44 +0300 Subject: [PATCH 1/4] catch errors in webhook --- src/bot.ts | 14 ++------------ test/convenience/webhook.test.ts | 24 ++++++++++++++++++++++-- test/deps.test.ts | 1 + 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 30ddd824..68ed34df 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -316,17 +316,7 @@ export class Bot< // handle updates sequentially (!) for (const update of updates) { this.lastTriedUpdateId = update.update_id; - try { - await this.handleUpdate(update); - } catch (err) { - // should always be true - if (err instanceof BotError) { - await this.errorHandler(err); - } else { - console.error("FATAL: grammY unable to handle:", err); - throw err; - } - } + await this.handleUpdate(update); } } @@ -367,7 +357,7 @@ a known bot info object.", await run(this.middleware(), ctx); } catch (err) { debugErr(`Error in middleware for update ${update.update_id}`); - throw new BotError(err, ctx); + await this.errorHandler(new BotError(err, ctx)); } } diff --git a/test/convenience/webhook.test.ts b/test/convenience/webhook.test.ts index de703399..a79e152b 100644 --- a/test/convenience/webhook.test.ts +++ b/test/convenience/webhook.test.ts @@ -11,9 +11,9 @@ import type bodyParser from "npm:@types/koa-bodyparser"; import type Koa from "npm:@types/koa"; import type { FastifyInstance } from "npm:fastify"; import type { NextApiRequest, NextApiResponse } from "npm:next"; -import { Bot, webhookCallback } from "../../src/mod.ts"; +import { Bot, BotError, webhookCallback } from "../../src/mod.ts"; import type { UserFromGetMe } from "../../src/types.ts"; -import { describe, it } from "../deps.test.ts"; +import { assertIsError, describe, it } from "../deps.test.ts"; describe("webhook", () => { const bot = new Bot("dummy", { me: {} as unknown as UserFromGetMe }); @@ -138,4 +138,24 @@ describe("webhook", () => { return handler(req); }); }); + + it("bot should catch errors in webhoook handler", async () => { + bot.catch((err) => { + assertIsError(err, BotError, "Test Error"); + }); + bot.on("message", () => { + throw new Error("Test Error"); + }); + + const handler = webhookCallback(bot, "std/http"); + const fake_req = new Request("https://fake-api.com", { + method: "POST", + body: JSON.stringify({ + update_id: 9696, + message: {}, + }), + }); + + await handler(fake_req); + }); }); diff --git a/test/deps.test.ts b/test/deps.test.ts index 3460db1f..216b3590 100644 --- a/test/deps.test.ts +++ b/test/deps.test.ts @@ -3,6 +3,7 @@ export { assertEquals, assertFalse, assertInstanceOf, + assertIsError, assertNotStrictEquals, assertObjectMatch, assertRejects, From b03590d3fd107db7d033e02581c9af38c763f17f Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Thu, 16 Jan 2025 19:10:37 +0300 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: KnorpelSenf --- test/convenience/webhook.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/convenience/webhook.test.ts b/test/convenience/webhook.test.ts index a79e152b..c3baaa01 100644 --- a/test/convenience/webhook.test.ts +++ b/test/convenience/webhook.test.ts @@ -148,7 +148,7 @@ describe("webhook", () => { }); const handler = webhookCallback(bot, "std/http"); - const fake_req = new Request("https://fake-api.com", { + const fakeReq = new Request("https://fake-api.com", { method: "POST", body: JSON.stringify({ update_id: 9696, @@ -156,6 +156,6 @@ describe("webhook", () => { }), }); - await handler(fake_req); + await handler(fakeReq); }); }); From 1f31a315c9e0c426a36c40287fce2d8a786af588 Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Thu, 16 Jan 2025 19:16:07 +0300 Subject: [PATCH 3/4] check handler actually been called --- test/convenience/webhook.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/convenience/webhook.test.ts b/test/convenience/webhook.test.ts index c3baaa01..6a39a0b9 100644 --- a/test/convenience/webhook.test.ts +++ b/test/convenience/webhook.test.ts @@ -13,7 +13,7 @@ import type { FastifyInstance } from "npm:fastify"; import type { NextApiRequest, NextApiResponse } from "npm:next"; import { Bot, BotError, webhookCallback } from "../../src/mod.ts"; import type { UserFromGetMe } from "../../src/types.ts"; -import { assertIsError, describe, it } from "../deps.test.ts"; +import { assertEquals, assertIsError, describe, it } from "../deps.test.ts"; describe("webhook", () => { const bot = new Bot("dummy", { me: {} as unknown as UserFromGetMe }); @@ -140,8 +140,11 @@ describe("webhook", () => { }); it("bot should catch errors in webhoook handler", async () => { + let called = false; + bot.catch((err) => { assertIsError(err, BotError, "Test Error"); + called = true; }); bot.on("message", () => { throw new Error("Test Error"); @@ -157,5 +160,7 @@ describe("webhook", () => { }); await handler(fakeReq); + + assertEquals(called, true); }); }); From 6d624c94ec73be7d31953f97831705703aac5b5d Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Thu, 16 Jan 2025 17:28:41 +0100 Subject: [PATCH 4/4] test: fix nitpicks --- test/convenience/webhook.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/convenience/webhook.test.ts b/test/convenience/webhook.test.ts index 6a39a0b9..7a093a86 100644 --- a/test/convenience/webhook.test.ts +++ b/test/convenience/webhook.test.ts @@ -13,7 +13,7 @@ import type { FastifyInstance } from "npm:fastify"; import type { NextApiRequest, NextApiResponse } from "npm:next"; import { Bot, BotError, webhookCallback } from "../../src/mod.ts"; import type { UserFromGetMe } from "../../src/types.ts"; -import { assertEquals, assertIsError, describe, it } from "../deps.test.ts"; +import { assert, assertIsError, describe, it } from "../deps.test.ts"; describe("webhook", () => { const bot = new Bot("dummy", { me: {} as unknown as UserFromGetMe }); @@ -161,6 +161,6 @@ describe("webhook", () => { await handler(fakeReq); - assertEquals(called, true); + assert(called); }); });