Skip to content

Commit

Permalink
feat: add warnings for errors 401 and 409 (#574)
Browse files Browse the repository at this point in the history
* feat: add warnings for errors 401 and 409

* fix: dedupe errors during polling

* fix: remove testing code

revoked, don't bother
  • Loading branch information
KnorpelSenf committed May 9, 2024
1 parent f1950b3 commit ee542d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,8 @@ a known bot info object.",
let sleepSeconds = 3;
if (error instanceof GrammyError) {
debugErr(error.message);
if (error.error_code === 401) {
debugErr(
"Make sure you are using the bot token you obtained from @BotFather (https://t.me/BotFather).",
);
throw error;
} else if (error.error_code === 409) {
debugErr(
"Consider revoking the bot token if you believe that no other instance is running.",
);
// rethrow upon unauthorized or conflict
if (error.error_code === 401 || error.error_code === 409) {
throw error;
} else if (error.error_code === 429) {
debugErr("Bot API server is closing.");
Expand Down
14 changes: 14 additions & 0 deletions src/core/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { type ApiError, type ResponseParameters } from "../types.ts";
import { debug as d } from "../platform.deno.ts";
const debug = d("grammy:warn");

/**
* This class represents errors that are thrown by grammY because the Telegram
Expand Down Expand Up @@ -40,6 +42,18 @@ export function toGrammyError(
method: string,
payload: Record<string, unknown>,
) {
switch (err.error_code) {
case 401:
debug(
"Error 401 means that your bot token is wrong, talk to https://t.me/BotFather to check it.",
);
break;
case 409:
debug(
"Error 409 means that you are running your bot several times on long polling. Consider revoking the bot token if you believe that no other instance is running.",
);
break;
}
return new GrammyError(
`Call to '${method}' failed!`,
err,
Expand Down

0 comments on commit ee542d6

Please sign in to comment.