Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to catch errors with bot.catch on webhook #741

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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<C>(err, ctx);
await this.errorHandler(new BotError<C>(err, ctx));
}
}

Expand Down
29 changes: 27 additions & 2 deletions test/convenience/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { assertEquals, assertIsError, describe, it } from "../deps.test.ts";

describe("webhook", () => {
const bot = new Bot("dummy", { me: {} as unknown as UserFromGetMe });
Expand Down Expand Up @@ -138,4 +138,29 @@ describe("webhook", () => {
return handler(req);
});
});

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");
});

const handler = webhookCallback(bot, "std/http");
const fakeReq = new Request("https://fake-api.com", {
method: "POST",
body: JSON.stringify({
update_id: 9696,
message: {},
}),
});

await handler(fakeReq);

assertEquals(called, true);
});
});
1 change: 1 addition & 0 deletions test/deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
assertEquals,
assertFalse,
assertInstanceOf,
assertIsError,
assertNotStrictEquals,
assertObjectMatch,
assertRejects,
Expand Down