-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTgError.ts
More file actions
34 lines (33 loc) · 902 Bytes
/
TgError.ts
File metadata and controls
34 lines (33 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import type { TgResponseParameters } from "./TgApi.ts";
/**
* Error returned from the Telegram Bot API.
*
* Contains the error code and the description.
*
* @example Keep retrying sending a message if the rate limit is exceeded.
*
* ```ts
* import { callTgApi, TgError } from "./mod.ts";
*
* const botToken = "YOUR_TOKEN";
* while (true) {
* try {
* await callTgApi({ botToken }, "sendMessage", { chat_id: 0, text: "Hello" });
* break;
* } catch (error) {
* if (error instanceof TgError && error.code === 429) {
* await new Promise((resolve) =>
* setTimeout(resolve, (error.parameters?.retry_after ?? 1) * 1000)
* );
* } else {
* throw error;
* }
* }
* }
* ```
*/
export class TgError extends Error {
constructor(message: string, public code: number, public parameters?: TgResponseParameters) {
super(message);
}
}