Skip to content

Commit

Permalink
chore: add error handling topic in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Feb 17, 2024
1 parent e5461e3 commit a00f779
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default defineConfig({
},
{ text: "Formatting", link: "/formatting/index" },
{ text: "Types", link: "/types/index" },
{ text: "Error Handling", link: "/error-handling/index" },
],
},
],
Expand Down
56 changes: 56 additions & 0 deletions docs/error-handling/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Error Handling

It happens that errors occur in middleware and we need to handle them.
That's what the `onError` method was created for.

```ts twoslash
import { Bot } from "gramio";

const bot = new Bot("");
// ---cut---

bot.updates.on("message", () => {
bot.api.sendMessage({
chat_id: "@not_found",
text: "Chat not exists....",
});
});

bot.updates.onError(({ context, kind, error }) => {
if (context.is("message")) return context.send(`${kind}: ${error.message}`);
});
```

## Errors

### Telegram

This error is the result of a failed request to the Telegram Bot API

```ts twoslash
import { Bot } from "gramio";

const bot = new Bot("");
// ---cut---
bot.updates.onError(({ context, kind, error }) => {
if (kind === "TELEGRAM" && error.method === "sendMessage") {
error.params; // is sendMessage params
}
});
```

### Unknown

This error is any unknown error, whether it's your class or just an Error.

```ts twoslash
import { Bot } from "gramio";

const bot = new Bot("");
// ---cut---
bot.updates.onError(({ context, kind, error }) => {
if (kind === "UNKNOWN") {
console.log(error.message);
}
});
```

0 comments on commit a00f779

Please sign in to comment.