-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add error handling topic in docs
- Loading branch information
1 parent
e5461e3
commit a00f779
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
``` |