@tgsnake/skema 1.12.0 → 2.0.0-browser.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +125 -16
- package/dist/mod.d.ts +10 -0
- package/dist/mod.js +10 -0
- package/dist/src/Logger.d.ts +20 -0
- package/dist/src/Logger.js +23 -0
- package/dist/src/deps.d.ts +29 -0
- package/dist/src/deps.js +34 -0
- package/dist/src/errors/Base.d.ts +53 -0
- package/dist/src/errors/Base.js +114 -0
- package/dist/src/errors/Client.d.ts +69 -0
- package/dist/src/errors/Client.js +69 -0
- package/dist/src/errors/File.d.ts +89 -0
- package/dist/src/errors/File.js +99 -0
- package/dist/src/errors/RpcError.d.ts +115 -0
- package/dist/src/errors/RpcError.js +226 -0
- package/dist/src/errors/SecretChat.d.ts +78 -0
- package/dist/src/errors/SecretChat.js +88 -0
- package/dist/src/errors/WebSocket.d.ts +70 -0
- package/dist/src/errors/WebSocket.js +80 -0
- package/dist/{errors → src/errors}/exceptions/All.d.ts +14 -0
- package/dist/{errors → src/errors}/exceptions/All.js +15 -5
- package/dist/{errors → src/errors}/exceptions/BadRequest400.d.ts +15 -1
- package/dist/src/errors/exceptions/BadRequest400.js +2683 -0
- package/dist/{errors → src/errors}/exceptions/Flood420.d.ts +15 -1
- package/dist/{errors → src/errors}/exceptions/Flood420.js +25 -24
- package/dist/{errors → src/errors}/exceptions/Forbidden403.d.ts +15 -1
- package/dist/src/errors/exceptions/Forbidden403.js +243 -0
- package/dist/{errors → src/errors}/exceptions/InternalServerError500.d.ts +15 -1
- package/dist/src/errors/exceptions/InternalServerError500.js +255 -0
- package/dist/{errors → src/errors}/exceptions/NotAcceptable406.d.ts +15 -1
- package/dist/{errors → src/errors}/exceptions/NotAcceptable406.js +51 -76
- package/dist/src/errors/exceptions/NotFound404.d.ts +27 -0
- package/dist/src/errors/exceptions/NotFound404.js +27 -0
- package/dist/src/errors/exceptions/SeeOther303.d.ts +39 -0
- package/dist/src/errors/exceptions/SeeOther303.js +39 -0
- package/dist/src/errors/exceptions/ServiceUnavailable503.d.ts +31 -0
- package/dist/src/errors/exceptions/ServiceUnavailable503.js +31 -0
- package/dist/{errors → src/errors}/exceptions/Unauthorized401.d.ts +15 -1
- package/dist/src/errors/exceptions/Unauthorized401.js +55 -0
- package/dist/src/errors/exceptions/index.d.ts +24 -0
- package/dist/src/errors/exceptions/index.js +24 -0
- package/dist/src/errors/index.d.ts +142 -0
- package/dist/src/errors/index.js +178 -0
- package/dist/src/helpers.d.ts +102 -0
- package/dist/src/helpers.js +176 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.js +12 -0
- package/dist/{raw → src/raw}/All.d.ts +47 -5
- package/dist/{raw → src/raw}/All.js +48 -9
- package/dist/{raw → src/raw}/Raw.d.ts +17546 -9
- package/dist/{raw → src/raw}/Raw.js +44899 -21694
- package/dist/src/raw/UpdateSecretChat.d.ts +186 -0
- package/dist/src/raw/UpdateSecretChat.js +215 -0
- package/dist/src/raw/core/GzipPacked.d.ts +63 -0
- package/dist/src/raw/core/GzipPacked.js +76 -0
- package/dist/src/raw/core/Message.d.ts +68 -0
- package/dist/src/raw/core/Message.js +105 -0
- package/dist/src/raw/core/MsgContainer.d.ts +56 -0
- package/dist/src/raw/core/MsgContainer.js +77 -0
- package/dist/src/raw/core/TLObject.d.ts +108 -0
- package/dist/src/raw/core/TLObject.js +209 -0
- package/dist/src/raw/core/index.d.ts +19 -0
- package/dist/src/raw/core/index.js +19 -0
- package/dist/src/raw/core/primitive/Bool.d.ts +104 -0
- package/dist/src/raw/core/primitive/Bool.js +120 -0
- package/dist/src/raw/core/primitive/Bytes.d.ts +45 -0
- package/dist/src/raw/core/primitive/Bytes.js +76 -0
- package/dist/src/raw/core/primitive/Double.d.ts +37 -0
- package/dist/src/raw/core/primitive/Double.js +51 -0
- package/dist/src/raw/core/primitive/Float.d.ts +37 -0
- package/dist/src/raw/core/primitive/Float.js +51 -0
- package/dist/src/raw/core/primitive/Int.d.ts +144 -0
- package/dist/src/raw/core/primitive/Int.js +237 -0
- package/dist/src/raw/core/primitive/String.d.ts +37 -0
- package/dist/src/raw/core/primitive/String.js +42 -0
- package/dist/src/raw/core/primitive/Vector.d.ts +56 -0
- package/dist/src/raw/core/primitive/Vector.js +93 -0
- package/dist/src/raw/core/primitive/index.d.ts +20 -0
- package/dist/src/raw/core/primitive/index.js +20 -0
- package/dist/src/raw/index.d.ts +17 -0
- package/dist/src/raw/index.js +17 -0
- package/package.json +54 -50
- package/dist/Logger.d.ts +0 -3
- package/dist/Logger.js +0 -9
- package/dist/deps.d.ts +0 -8
- package/dist/deps.js +0 -19
- package/dist/errors/Base.d.ts +0 -9
- package/dist/errors/Base.js +0 -50
- package/dist/errors/Client.d.ts +0 -21
- package/dist/errors/Client.js +0 -29
- package/dist/errors/File.d.ts +0 -13
- package/dist/errors/File.js +0 -30
- package/dist/errors/RpcError.d.ts +0 -22
- package/dist/errors/RpcError.js +0 -120
- package/dist/errors/SecretChat.d.ts +0 -13
- package/dist/errors/SecretChat.js +0 -30
- package/dist/errors/WebSocket.d.ts +0 -13
- package/dist/errors/WebSocket.js +0 -30
- package/dist/errors/exceptions/BadRequest400.js +0 -3352
- package/dist/errors/exceptions/Forbidden403.js +0 -290
- package/dist/errors/exceptions/InternalServerError500.js +0 -305
- package/dist/errors/exceptions/NotFound404.d.ts +0 -13
- package/dist/errors/exceptions/NotFound404.js +0 -19
- package/dist/errors/exceptions/SeeOther303.d.ts +0 -25
- package/dist/errors/exceptions/SeeOther303.js +0 -34
- package/dist/errors/exceptions/ServiceUnavailable503.d.ts +0 -17
- package/dist/errors/exceptions/ServiceUnavailable503.js +0 -24
- package/dist/errors/exceptions/Unauthorized401.js +0 -54
- package/dist/errors/exceptions/index.d.ts +0 -10
- package/dist/errors/exceptions/index.js +0 -48
- package/dist/errors/index.d.ts +0 -31
- package/dist/errors/index.js +0 -111
- package/dist/helpers.d.ts +0 -6
- package/dist/helpers.js +0 -85
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -34
- package/dist/raw/UpdateSecretChat.d.ts +0 -43
- package/dist/raw/UpdateSecretChat.js +0 -91
- package/dist/raw/core/GzipPacked.d.ts +0 -9
- package/dist/raw/core/GzipPacked.js +0 -59
- package/dist/raw/core/Message.d.ts +0 -12
- package/dist/raw/core/Message.js +0 -80
- package/dist/raw/core/MsgContainer.d.ts +0 -10
- package/dist/raw/core/MsgContainer.js +0 -68
- package/dist/raw/core/TLObject.d.ts +0 -18
- package/dist/raw/core/TLObject.js +0 -108
- package/dist/raw/core/index.d.ts +0 -6
- package/dist/raw/core/index.js +0 -46
- package/dist/raw/core/primitive/Bool.d.ts +0 -19
- package/dist/raw/core/primitive/Bool.js +0 -41
- package/dist/raw/core/primitive/Bytes.d.ts +0 -6
- package/dist/raw/core/primitive/Bytes.js +0 -41
- package/dist/raw/core/primitive/Double.d.ts +0 -6
- package/dist/raw/core/primitive/Double.js +0 -24
- package/dist/raw/core/primitive/Float.d.ts +0 -6
- package/dist/raw/core/primitive/Float.js +0 -24
- package/dist/raw/core/primitive/Int.d.ts +0 -22
- package/dist/raw/core/primitive/Int.js +0 -122
- package/dist/raw/core/primitive/String.d.ts +0 -6
- package/dist/raw/core/primitive/String.js +0 -15
- package/dist/raw/core/primitive/Vector.d.ts +0 -8
- package/dist/raw/core/primitive/Vector.js +0 -49
- package/dist/raw/core/primitive/index.d.ts +0 -7
- package/dist/raw/core/primitive/index.js +0 -22
- package/dist/raw/index.d.ts +0 -4
- package/dist/raw/index.js +0 -17
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tgsnake - Telegram MTProto library for javascript or typescript.
|
|
3
|
+
* Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
|
|
4
|
+
*
|
|
5
|
+
* THIS FILE IS PART OF TGSNAKE
|
|
6
|
+
*
|
|
7
|
+
* tgsnake is a free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT License as published.
|
|
9
|
+
*/
|
|
10
|
+
import { BaseError } from './Base.js';
|
|
11
|
+
/**
|
|
12
|
+
* Base error class for all file and stream operations.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* Extends {@link BaseError} to provide additional context specifically for handling,
|
|
16
|
+
* transferring, and validating local or remote files.
|
|
17
|
+
*
|
|
18
|
+
* @extends BaseError
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* throw new FileError('File not found', 'The specified file does not exist.');
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class FileError extends BaseError {
|
|
25
|
+
/**
|
|
26
|
+
* Constructs a new FileError instance.
|
|
27
|
+
*
|
|
28
|
+
* @param message - A brief message describing the error.
|
|
29
|
+
* @param description - An optional detailed description or troubleshooting guide.
|
|
30
|
+
*/
|
|
31
|
+
constructor(message: string, description?: string);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Error thrown when an attempt is made to upload or process a zero-byte file.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* Telegram MTProto uploads require files to have a non-zero byte size.
|
|
38
|
+
*
|
|
39
|
+
* @extends FileError
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* if (file.size === 0) {
|
|
43
|
+
* throw new FileUploadZero();
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare class FileUploadZero extends FileError {
|
|
48
|
+
/**
|
|
49
|
+
* Constructs a new FileUploadZero instance with predefined error messages.
|
|
50
|
+
*/
|
|
51
|
+
constructor();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Error thrown when a file exceeds the maximum allowed file upload size.
|
|
55
|
+
*
|
|
56
|
+
* @extends FileError
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* if (file.size > LIMIT) {
|
|
60
|
+
* throw new FileUploadBigger(LIMIT, file.size);
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare class FileUploadBigger extends FileError {
|
|
65
|
+
/**
|
|
66
|
+
* Constructs a new FileUploadBigger instance.
|
|
67
|
+
*
|
|
68
|
+
* @param limit - The maximum allowed file size limit in bytes.
|
|
69
|
+
* @param size - The actual size of the provided file in bytes.
|
|
70
|
+
*/
|
|
71
|
+
constructor(limit: number, size: number);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Error thrown when the provided argument is not a valid readable stream or buffer.
|
|
75
|
+
*
|
|
76
|
+
* @extends FileError
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* if (!isReadableStream(arg)) {
|
|
80
|
+
* throw new FileIsNotReadable();
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare class FileIsNotReadable extends FileError {
|
|
85
|
+
/**
|
|
86
|
+
* Constructs a new FileIsNotReadable instance with predefined error messages.
|
|
87
|
+
*/
|
|
88
|
+
constructor();
|
|
89
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tgsnake - Telegram MTProto library for javascript or typescript.
|
|
3
|
+
* Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
|
|
4
|
+
*
|
|
5
|
+
* THIS FILE IS PART OF TGSNAKE
|
|
6
|
+
*
|
|
7
|
+
* tgsnake is a free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT License as published.
|
|
9
|
+
*/
|
|
10
|
+
import { BaseError } from './Base.js';
|
|
11
|
+
/**
|
|
12
|
+
* Base error class for all file and stream operations.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* Extends {@link BaseError} to provide additional context specifically for handling,
|
|
16
|
+
* transferring, and validating local or remote files.
|
|
17
|
+
*
|
|
18
|
+
* @extends BaseError
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* throw new FileError('File not found', 'The specified file does not exist.');
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class FileError extends BaseError {
|
|
25
|
+
/**
|
|
26
|
+
* Constructs a new FileError instance.
|
|
27
|
+
*
|
|
28
|
+
* @param message - A brief message describing the error.
|
|
29
|
+
* @param description - An optional detailed description or troubleshooting guide.
|
|
30
|
+
*/
|
|
31
|
+
constructor(message, description) {
|
|
32
|
+
super();
|
|
33
|
+
this.message = message;
|
|
34
|
+
this.description = description;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Error thrown when an attempt is made to upload or process a zero-byte file.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* Telegram MTProto uploads require files to have a non-zero byte size.
|
|
42
|
+
*
|
|
43
|
+
* @extends FileError
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* if (file.size === 0) {
|
|
47
|
+
* throw new FileUploadZero();
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export class FileUploadZero extends FileError {
|
|
52
|
+
/**
|
|
53
|
+
* Constructs a new FileUploadZero instance with predefined error messages.
|
|
54
|
+
*/
|
|
55
|
+
constructor() {
|
|
56
|
+
super("Can't upload file when it zero bytes.", 'Provided file has zero bytes (0 B) file size.');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Error thrown when a file exceeds the maximum allowed file upload size.
|
|
61
|
+
*
|
|
62
|
+
* @extends FileError
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* if (file.size > LIMIT) {
|
|
66
|
+
* throw new FileUploadBigger(LIMIT, file.size);
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export class FileUploadBigger extends FileError {
|
|
71
|
+
/**
|
|
72
|
+
* Constructs a new FileUploadBigger instance.
|
|
73
|
+
*
|
|
74
|
+
* @param limit - The maximum allowed file size limit in bytes.
|
|
75
|
+
* @param size - The actual size of the provided file in bytes.
|
|
76
|
+
*/
|
|
77
|
+
constructor(limit, size) {
|
|
78
|
+
super(`File greater than ${limit} B.`, `The provided file has ${size} B file size, it greater than ${limit} B`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Error thrown when the provided argument is not a valid readable stream or buffer.
|
|
83
|
+
*
|
|
84
|
+
* @extends FileError
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* if (!isReadableStream(arg)) {
|
|
88
|
+
* throw new FileIsNotReadable();
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export class FileIsNotReadable extends FileError {
|
|
93
|
+
/**
|
|
94
|
+
* Constructs a new FileIsNotReadable instance with predefined error messages.
|
|
95
|
+
*/
|
|
96
|
+
constructor() {
|
|
97
|
+
super('FILE_IS_NOT_READABLE', 'The argument provided is not a Readable stream.');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tgsnake - Telegram MTProto library for javascript or typescript.
|
|
3
|
+
* Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
|
|
4
|
+
*
|
|
5
|
+
* THIS FILE IS PART OF TGSNAKE
|
|
6
|
+
*
|
|
7
|
+
* tgsnake is a free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT License as published.
|
|
9
|
+
*/
|
|
10
|
+
import { Raw, TLObject } from '../raw/index.js';
|
|
11
|
+
/**
|
|
12
|
+
* Base error class for all Remote Procedure Call (RPC) errors returned by the Telegram API.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* Every failed API request returns an RPC error consisting of an error code (e.g. 400, 401, 403, 420, etc.)
|
|
16
|
+
* and an error identifier (e.g. `PHONE_NUMBER_INVALID`). This class dynamically maps, formats,
|
|
17
|
+
* and formats exceptions into descriptive structures that inherit from `Error`.
|
|
18
|
+
*
|
|
19
|
+
* @extends Error
|
|
20
|
+
*/
|
|
21
|
+
export declare class RPCError extends Error {
|
|
22
|
+
/**
|
|
23
|
+
* The Telegram-specific alphanumeric error code identifier. E.g. `FLOOD_WAIT_X`.
|
|
24
|
+
*/
|
|
25
|
+
id: string;
|
|
26
|
+
/**
|
|
27
|
+
* The HTTP/MTProto status code returned. E.g. 400 or 420.
|
|
28
|
+
*/
|
|
29
|
+
code: number;
|
|
30
|
+
/**
|
|
31
|
+
* The formatted, human-readable error message.
|
|
32
|
+
*/
|
|
33
|
+
message: string;
|
|
34
|
+
/**
|
|
35
|
+
* The class name representing this error instance.
|
|
36
|
+
*/
|
|
37
|
+
name: string;
|
|
38
|
+
/**
|
|
39
|
+
* The optional value parsed from the Telegram error string (such as the number of seconds to wait).
|
|
40
|
+
*/
|
|
41
|
+
value?: number | string | Raw.RpcError;
|
|
42
|
+
/**
|
|
43
|
+
* Internal sign flag. True if the Telegram error code was negative.
|
|
44
|
+
*
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
_isSigned?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Internal flag indicating whether the error code or string was unrecognized.
|
|
50
|
+
*
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
_isUnknown?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The name of the specific RPC method that triggered this error.
|
|
56
|
+
*
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
_rpcName?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Constructs a new RPCError instance.
|
|
62
|
+
*
|
|
63
|
+
* @param value - The parsed variable value from the Telegram error string, if any.
|
|
64
|
+
* @param rpcName - The name of the causing RPC method.
|
|
65
|
+
* @param isUnknown - True if the error is unrecognized.
|
|
66
|
+
* @param isSigned - True if the error code is signed.
|
|
67
|
+
*/
|
|
68
|
+
constructor(value?: number | string | Raw.RpcError, rpcName?: string, isUnknown?: boolean, isSigned?: boolean);
|
|
69
|
+
/**
|
|
70
|
+
* Formats the final exception message by embedding variables and the causing RPC method name.
|
|
71
|
+
*
|
|
72
|
+
* @protected
|
|
73
|
+
*/
|
|
74
|
+
protected _format(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Analyzes a raw Telegram `RpcError` object and raises a matching, highly-specialized RPC sub-exception.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* This static factory parses the Telegram response error string, extracts parameterized values (such as
|
|
80
|
+
* the cooldown duration in `FLOOD_WAIT_60`), matches the error code to the compiled exception dictionary,
|
|
81
|
+
* constructs the correct class dynamically, and throws the finalized instance.
|
|
82
|
+
*
|
|
83
|
+
* @param rpcError - The raw `RpcError` returned from the API deserializer.
|
|
84
|
+
* @param rpcType - The causing TL request object context.
|
|
85
|
+
* @throws {RPCError} The parsed, specialized subclass of `RPCError`.
|
|
86
|
+
*/
|
|
87
|
+
static raise(rpcError: Raw.RpcError, rpcType: TLObject): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Serializes the RPC error object to a clean JSON-compatible representation.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* Correctly stringifies extremely large fields (e.g. bigints) that are normally not
|
|
93
|
+
* serializable in standard `JSON.stringify` calls.
|
|
94
|
+
*
|
|
95
|
+
* @returns A plain object containing error properties and the stack trace.
|
|
96
|
+
*/
|
|
97
|
+
toJSON(): {
|
|
98
|
+
[key: string]: unknown;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Formats the error into a structured string.
|
|
102
|
+
*
|
|
103
|
+
* @returns A string starting with the constructor name followed by a pretty JSON payload.
|
|
104
|
+
*/
|
|
105
|
+
toString(): string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Fallback error class used when the Telegram API returns an unrecognized error code or message.
|
|
109
|
+
*
|
|
110
|
+
* @extends RPCError
|
|
111
|
+
*/
|
|
112
|
+
export declare class UnknownError extends RPCError {
|
|
113
|
+
code: number;
|
|
114
|
+
name: string;
|
|
115
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tgsnake - Telegram MTProto library for javascript or typescript.
|
|
3
|
+
* Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
|
|
4
|
+
*
|
|
5
|
+
* THIS FILE IS PART OF TGSNAKE
|
|
6
|
+
*
|
|
7
|
+
* tgsnake is a free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT License as published.
|
|
9
|
+
*/
|
|
10
|
+
import { Logger } from '../Logger.js';
|
|
11
|
+
import { Exceptions } from './exceptions/All.js';
|
|
12
|
+
import { inspect } from '../deps.js';
|
|
13
|
+
import { Exceptions as AllExceptions } from './index.js';
|
|
14
|
+
function getModule(name) {
|
|
15
|
+
const [namespace, mod] = name.split('.');
|
|
16
|
+
if (AllExceptions[namespace] &&
|
|
17
|
+
AllExceptions[namespace][mod]) {
|
|
18
|
+
return AllExceptions[namespace][mod];
|
|
19
|
+
}
|
|
20
|
+
return UnknownError;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Base error class for all Remote Procedure Call (RPC) errors returned by the Telegram API.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Every failed API request returns an RPC error consisting of an error code (e.g. 400, 401, 403, 420, etc.)
|
|
27
|
+
* and an error identifier (e.g. `PHONE_NUMBER_INVALID`). This class dynamically maps, formats,
|
|
28
|
+
* and formats exceptions into descriptive structures that inherit from `Error`.
|
|
29
|
+
*
|
|
30
|
+
* @extends Error
|
|
31
|
+
*/
|
|
32
|
+
export class RPCError extends Error {
|
|
33
|
+
/**
|
|
34
|
+
* The Telegram-specific alphanumeric error code identifier. E.g. `FLOOD_WAIT_X`.
|
|
35
|
+
*/
|
|
36
|
+
id;
|
|
37
|
+
/**
|
|
38
|
+
* The HTTP/MTProto status code returned. E.g. 400 or 420.
|
|
39
|
+
*/
|
|
40
|
+
code;
|
|
41
|
+
/**
|
|
42
|
+
* The formatted, human-readable error message.
|
|
43
|
+
*/
|
|
44
|
+
message;
|
|
45
|
+
/**
|
|
46
|
+
* The class name representing this error instance.
|
|
47
|
+
*/
|
|
48
|
+
name;
|
|
49
|
+
/**
|
|
50
|
+
* The optional value parsed from the Telegram error string (such as the number of seconds to wait).
|
|
51
|
+
*/
|
|
52
|
+
value;
|
|
53
|
+
/**
|
|
54
|
+
* Internal sign flag. True if the Telegram error code was negative.
|
|
55
|
+
*
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
_isSigned;
|
|
59
|
+
/**
|
|
60
|
+
* Internal flag indicating whether the error code or string was unrecognized.
|
|
61
|
+
*
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
_isUnknown;
|
|
65
|
+
/**
|
|
66
|
+
* The name of the specific RPC method that triggered this error.
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
_rpcName;
|
|
71
|
+
/**
|
|
72
|
+
* Constructs a new RPCError instance.
|
|
73
|
+
*
|
|
74
|
+
* @param value - The parsed variable value from the Telegram error string, if any.
|
|
75
|
+
* @param rpcName - The name of the causing RPC method.
|
|
76
|
+
* @param isUnknown - True if the error is unrecognized.
|
|
77
|
+
* @param isSigned - True if the error code is signed.
|
|
78
|
+
*/
|
|
79
|
+
constructor(value, rpcName, isUnknown, isSigned) {
|
|
80
|
+
super();
|
|
81
|
+
Logger.debug(`[8] Creating new instance RPCError(${rpcName ?? this.name})`);
|
|
82
|
+
this._isSigned = isSigned;
|
|
83
|
+
this._isUnknown = isUnknown;
|
|
84
|
+
this._rpcName = rpcName;
|
|
85
|
+
if (!Number.isNaN(value)) {
|
|
86
|
+
this.value = Number(value);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.value = value;
|
|
90
|
+
}
|
|
91
|
+
if (isUnknown) {
|
|
92
|
+
Logger.debug(`[9] UnknownError : ${this.name}`);
|
|
93
|
+
// TODO: write UnknownError.txt file
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Formats the final exception message by embedding variables and the causing RPC method name.
|
|
98
|
+
*
|
|
99
|
+
* @protected
|
|
100
|
+
*/
|
|
101
|
+
_format() {
|
|
102
|
+
this.message = `Telegram Says: [${this._isSigned ? '-' : ''}${this.code} ${this.id || this.name}] - ${(this.message || '').replace(/\{value\}/g, String(this.value))} ${this._rpcName ? `(caused by ${this._rpcName})` : ''}`;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Analyzes a raw Telegram `RpcError` object and raises a matching, highly-specialized RPC sub-exception.
|
|
106
|
+
*
|
|
107
|
+
* @remarks
|
|
108
|
+
* This static factory parses the Telegram response error string, extracts parameterized values (such as
|
|
109
|
+
* the cooldown duration in `FLOOD_WAIT_60`), matches the error code to the compiled exception dictionary,
|
|
110
|
+
* constructs the correct class dynamically, and throws the finalized instance.
|
|
111
|
+
*
|
|
112
|
+
* @param rpcError - The raw `RpcError` returned from the API deserializer.
|
|
113
|
+
* @param rpcType - The causing TL request object context.
|
|
114
|
+
* @throws {RPCError} The parsed, specialized subclass of `RPCError`.
|
|
115
|
+
*/
|
|
116
|
+
static async raise(rpcError, rpcType) {
|
|
117
|
+
let code = rpcError.errorCode;
|
|
118
|
+
const message = rpcError.errorMessage;
|
|
119
|
+
const isSigned = code < 0;
|
|
120
|
+
const name = rpcType.className;
|
|
121
|
+
if (isSigned)
|
|
122
|
+
code = -code;
|
|
123
|
+
if (!(code in Exceptions)) {
|
|
124
|
+
throw new UnknownError(`[${code} ${message}]`, name, true, isSigned);
|
|
125
|
+
}
|
|
126
|
+
let id = message.replace(/\_\d+/gm, '_X');
|
|
127
|
+
const regexMatch = message.match(/\_(\d+)/gm);
|
|
128
|
+
let value = '';
|
|
129
|
+
if (regexMatch) {
|
|
130
|
+
value = regexMatch[0].replace(/\_/g, '');
|
|
131
|
+
}
|
|
132
|
+
if (!(id in Exceptions[code])) {
|
|
133
|
+
// try to replace the last word with asterisk (*)
|
|
134
|
+
// example: FILE_REFERENCE_*
|
|
135
|
+
id = id.split('_').splice(-1, 1, '*').join('_');
|
|
136
|
+
if (!(id in Exceptions[code])) {
|
|
137
|
+
const modules = getModule(Exceptions[code]['_']);
|
|
138
|
+
const _module = new modules(value, name, true, isSigned);
|
|
139
|
+
_module.message = `[${code} ${message}]`;
|
|
140
|
+
_module.id = message.replace(/\_\d+/gm, '_X');
|
|
141
|
+
_module._format();
|
|
142
|
+
throw _module;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const modules = await getModule(Exceptions[code][id]);
|
|
146
|
+
const _module = new modules(value, name, false, isSigned);
|
|
147
|
+
_module._format();
|
|
148
|
+
throw _module;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Custom inspect handler for Node.js console output (`util.inspect`).
|
|
152
|
+
* Omits private properties (prefixed with `_`) to keep debug prints clean.
|
|
153
|
+
*
|
|
154
|
+
* @ignore
|
|
155
|
+
* @returns An object containing enumerable public properties to display.
|
|
156
|
+
*/
|
|
157
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
158
|
+
const toPrint = {
|
|
159
|
+
_: this.constructor.name,
|
|
160
|
+
};
|
|
161
|
+
for (const key in this) {
|
|
162
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
163
|
+
const value = this[key];
|
|
164
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
165
|
+
toPrint[key] = value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
Object.setPrototypeOf(toPrint, {
|
|
170
|
+
stack: this.stack,
|
|
171
|
+
});
|
|
172
|
+
return toPrint;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Custom inspect handler for Deno runtime terminal output (`Deno.inspect`).
|
|
176
|
+
* Wraps the Node.js inspect output and enables terminal colors.
|
|
177
|
+
*
|
|
178
|
+
* @ignore
|
|
179
|
+
* @returns The colorized, formatted representation of the error.
|
|
180
|
+
*/
|
|
181
|
+
[Symbol.for('Deno.customInspect')]() {
|
|
182
|
+
// @ts-ignore: deno compatibility
|
|
183
|
+
return String(inspect(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Serializes the RPC error object to a clean JSON-compatible representation.
|
|
187
|
+
*
|
|
188
|
+
* @remarks
|
|
189
|
+
* Correctly stringifies extremely large fields (e.g. bigints) that are normally not
|
|
190
|
+
* serializable in standard `JSON.stringify` calls.
|
|
191
|
+
*
|
|
192
|
+
* @returns A plain object containing error properties and the stack trace.
|
|
193
|
+
*/
|
|
194
|
+
toJSON() {
|
|
195
|
+
const toPrint = {
|
|
196
|
+
_: this.constructor.name,
|
|
197
|
+
stack: this.stack,
|
|
198
|
+
};
|
|
199
|
+
for (const key in this) {
|
|
200
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
201
|
+
const value = this[key];
|
|
202
|
+
if (!key.startsWith('_')) {
|
|
203
|
+
toPrint[key] = typeof value === 'bigint' ? String(value) : value;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return toPrint;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Formats the error into a structured string.
|
|
211
|
+
*
|
|
212
|
+
* @returns A string starting with the constructor name followed by a pretty JSON payload.
|
|
213
|
+
*/
|
|
214
|
+
toString() {
|
|
215
|
+
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Fallback error class used when the Telegram API returns an unrecognized error code or message.
|
|
220
|
+
*
|
|
221
|
+
* @extends RPCError
|
|
222
|
+
*/
|
|
223
|
+
export class UnknownError extends RPCError {
|
|
224
|
+
code = 520;
|
|
225
|
+
name = 'Unknown Error';
|
|
226
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tgsnake - Telegram MTProto library for javascript or typescript.
|
|
3
|
+
* Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
|
|
4
|
+
*
|
|
5
|
+
* THIS FILE IS PART OF TGSNAKE
|
|
6
|
+
*
|
|
7
|
+
* tgsnake is a free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT License as published.
|
|
9
|
+
*/
|
|
10
|
+
import { BaseError } from './Base.js';
|
|
11
|
+
/**
|
|
12
|
+
* Base custom error class for all secret chat end-to-end encryption and session management issues.
|
|
13
|
+
*
|
|
14
|
+
* @extends BaseError
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* throw new SecretChatError('Failed to decrypt message', 'The provided key is invalid.');
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class SecretChatError extends BaseError {
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a new SecretChatError instance.
|
|
23
|
+
*
|
|
24
|
+
* @param message - A brief message describing the error.
|
|
25
|
+
* @param description - An optional detailed description or troubleshooting context.
|
|
26
|
+
*/
|
|
27
|
+
constructor(message: string, description?: string);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Error thrown when a security key fingerprint mismatch is detected during end-to-end secret chat encryption.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* This suggests that the encryption key exchanged does not match, indicating the session is not secure
|
|
34
|
+
* or has been tampered with. For security reasons, the secret chat must be closed immediately.
|
|
35
|
+
*
|
|
36
|
+
* @extends SecretChatError
|
|
37
|
+
*/
|
|
38
|
+
export declare class FingerprintMismatch extends SecretChatError {
|
|
39
|
+
/**
|
|
40
|
+
* Constructs a new FingerprintMismatch instance with predefined error messages.
|
|
41
|
+
*/
|
|
42
|
+
constructor();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Error thrown when a secret chat session ID cannot be found in the client's local database or session file.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* Ensure that the `chatId` is valid, correct, and that the secret chat negotiation has already taken place
|
|
49
|
+
* and was stored successfully.
|
|
50
|
+
*
|
|
51
|
+
* @extends SecretChatError
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* throw new ChatNotFound(12345);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare class ChatNotFound extends SecretChatError {
|
|
58
|
+
/**
|
|
59
|
+
* Constructs a new ChatNotFound instance.
|
|
60
|
+
*
|
|
61
|
+
* @param chatId - The target secret chat ID that was not found.
|
|
62
|
+
*/
|
|
63
|
+
constructor(chatId: number);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Error thrown when an attempt is made to accept an end-to-end secret chat request that has already been accepted.
|
|
67
|
+
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* Secret chats can only transition from pending to accepted once.
|
|
70
|
+
*
|
|
71
|
+
* @extends SecretChatError
|
|
72
|
+
*/
|
|
73
|
+
export declare class AlreadyAccepted extends SecretChatError {
|
|
74
|
+
/**
|
|
75
|
+
* Constructs a new AlreadyAccepted instance.
|
|
76
|
+
*/
|
|
77
|
+
constructor();
|
|
78
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tgsnake - Telegram MTProto library for javascript or typescript.
|
|
3
|
+
* Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
|
|
4
|
+
*
|
|
5
|
+
* THIS FILE IS PART OF TGSNAKE
|
|
6
|
+
*
|
|
7
|
+
* tgsnake is a free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the MIT License as published.
|
|
9
|
+
*/
|
|
10
|
+
import { BaseError } from './Base.js';
|
|
11
|
+
/**
|
|
12
|
+
* Base custom error class for all secret chat end-to-end encryption and session management issues.
|
|
13
|
+
*
|
|
14
|
+
* @extends BaseError
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* throw new SecretChatError('Failed to decrypt message', 'The provided key is invalid.');
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class SecretChatError extends BaseError {
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a new SecretChatError instance.
|
|
23
|
+
*
|
|
24
|
+
* @param message - A brief message describing the error.
|
|
25
|
+
* @param description - An optional detailed description or troubleshooting context.
|
|
26
|
+
*/
|
|
27
|
+
constructor(message, description) {
|
|
28
|
+
super();
|
|
29
|
+
this.message = message;
|
|
30
|
+
this.description = description;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Error thrown when a security key fingerprint mismatch is detected during end-to-end secret chat encryption.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* This suggests that the encryption key exchanged does not match, indicating the session is not secure
|
|
38
|
+
* or has been tampered with. For security reasons, the secret chat must be closed immediately.
|
|
39
|
+
*
|
|
40
|
+
* @extends SecretChatError
|
|
41
|
+
*/
|
|
42
|
+
export class FingerprintMismatch extends SecretChatError {
|
|
43
|
+
/**
|
|
44
|
+
* Constructs a new FingerprintMismatch instance with predefined error messages.
|
|
45
|
+
*/
|
|
46
|
+
constructor() {
|
|
47
|
+
super('Fingerprint key mismatch', 'Given fingerprint key from message is mismatch. So the message is not secure and the secret chat should be closed.');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Error thrown when a secret chat session ID cannot be found in the client's local database or session file.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* Ensure that the `chatId` is valid, correct, and that the secret chat negotiation has already taken place
|
|
55
|
+
* and was stored successfully.
|
|
56
|
+
*
|
|
57
|
+
* @extends SecretChatError
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* throw new ChatNotFound(12345);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export class ChatNotFound extends SecretChatError {
|
|
64
|
+
/**
|
|
65
|
+
* Constructs a new ChatNotFound instance.
|
|
66
|
+
*
|
|
67
|
+
* @param chatId - The target secret chat ID that was not found.
|
|
68
|
+
*/
|
|
69
|
+
constructor(chatId) {
|
|
70
|
+
super('Secret chat not found', `Provided chatId (${chatId}) is not found in session. Make sure the chatId is correct and already saved in session.`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Error thrown when an attempt is made to accept an end-to-end secret chat request that has already been accepted.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* Secret chats can only transition from pending to accepted once.
|
|
78
|
+
*
|
|
79
|
+
* @extends SecretChatError
|
|
80
|
+
*/
|
|
81
|
+
export class AlreadyAccepted extends SecretChatError {
|
|
82
|
+
/**
|
|
83
|
+
* Constructs a new AlreadyAccepted instance.
|
|
84
|
+
*/
|
|
85
|
+
constructor() {
|
|
86
|
+
super('Secret chat already accepted');
|
|
87
|
+
}
|
|
88
|
+
}
|