From 17a307419b14e0d0a8440d09e340531ca84e92b6 Mon Sep 17 00:00:00 2001 From: Sreelal TS Date: Fri, 15 Mar 2024 20:02:13 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=AC=20Added=20`TeleverseExceptionType`?= =?UTF-8?q?=20to=20classify=20exceptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/abstracts/chat_boost_source.dart | 1 + .../models/abstracts/chat_member.dart | 2 + .../abstracts/input_message_content.dart | 7 ++- .../models/abstracts/message_origin.dart | 7 ++- .../models/abstracts/reaction_type.dart | 2 + lib/src/telegram/models/file.dart | 8 ++- lib/src/telegram/models/update.dart | 2 + lib/src/televerse/bot.dart | 2 + lib/src/televerse/context/context.dart | 2 + lib/src/televerse/links/user.dart | 2 + lib/src/televerse/models/chat_id.dart | 1 + lib/src/televerse/models/input_file.dart | 6 +- .../televerse/models/televerse_exception.dart | 54 +++++++++++++++--- lib/src/televerse/raw_api.dart | 56 +++++++++++++++---- lib/src/types/chat_boost_source_type.dart | 1 + lib/src/types/message_origin_type.dart | 2 + lib/src/types/parse_mode.dart | 6 +- lib/src/types/reaction_type_emoji_type.dart | 1 + lib/src/types/sticker_type.dart | 6 +- 19 files changed, 143 insertions(+), 25 deletions(-) diff --git a/lib/src/telegram/models/abstracts/chat_boost_source.dart b/lib/src/telegram/models/abstracts/chat_boost_source.dart index 8cd61acd..e46d3db1 100644 --- a/lib/src/telegram/models/abstracts/chat_boost_source.dart +++ b/lib/src/telegram/models/abstracts/chat_boost_source.dart @@ -28,6 +28,7 @@ abstract class ChatBoostSource { "Invalid value '${json['type']}' for ChatBoostSourceType.", description: 'The given value does not match any ChatBoostSourceType.', + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/telegram/models/abstracts/chat_member.dart b/lib/src/telegram/models/abstracts/chat_member.dart index 80450795..909d599b 100644 --- a/lib/src/telegram/models/abstracts/chat_member.dart +++ b/lib/src/telegram/models/abstracts/chat_member.dart @@ -37,6 +37,8 @@ abstract class ChatMember { default: throw TeleverseException( 'Unknown ChatMember status: ${json['status']}', + description: 'The given status does not match any ChatMemberStatus.', + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/telegram/models/abstracts/input_message_content.dart b/lib/src/telegram/models/abstracts/input_message_content.dart index 9ac5d18e..a39ce0e7 100644 --- a/lib/src/telegram/models/abstracts/input_message_content.dart +++ b/lib/src/telegram/models/abstracts/input_message_content.dart @@ -35,7 +35,12 @@ abstract class InputMessageContent { } else if (isInVoice) { return InputInvoiceMessageContent.fromJson(json); } else { - throw TeleverseException('Unknown InputMessageContent type'); + throw TeleverseException( + 'Unknown InputMessageContent type', + description: + 'The given JSON object does not match any InputMessageContent type.', + type: TeleverseExceptionType.invalidParameter, + ); } } } diff --git a/lib/src/telegram/models/abstracts/message_origin.dart b/lib/src/telegram/models/abstracts/message_origin.dart index d6ff78fd..f731307a 100644 --- a/lib/src/telegram/models/abstracts/message_origin.dart +++ b/lib/src/telegram/models/abstracts/message_origin.dart @@ -31,7 +31,12 @@ abstract class MessageOrigin { case 'channel': return MessageOriginChannel.fromJson(json); default: - throw TeleverseException("Unknown message origin type"); + throw TeleverseException( + "Unknown message origin type", + description: + "The given JSON object does not match any MessageOrigin type.", + type: TeleverseExceptionType.invalidParameter, + ); } } diff --git a/lib/src/telegram/models/abstracts/reaction_type.dart b/lib/src/telegram/models/abstracts/reaction_type.dart index 32f4f167..63b1b01d 100644 --- a/lib/src/telegram/models/abstracts/reaction_type.dart +++ b/lib/src/telegram/models/abstracts/reaction_type.dart @@ -26,6 +26,8 @@ abstract class ReactionType { default: throw TeleverseException( 'Invalid reaction type: ${json['type']}', + description: 'The given type does not match any ReactionType.', + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/telegram/models/file.dart b/lib/src/telegram/models/file.dart index 48e6942f..5e2f7a5e 100644 --- a/lib/src/telegram/models/file.dart +++ b/lib/src/telegram/models/file.dart @@ -73,10 +73,16 @@ class File { getDownloadURI(token), options: Options(responseType: ResponseType.bytes), ); + dio.close(); if (r.statusCode == 200) { return r.data as Uint8List; } else { - throw TeleverseException("Couldn't fetch the file data."); + throw TeleverseException( + "Couldn't fetch the file data.", + description: + "The request to fetch the file data failed with status code ${r.statusCode}.", + type: TeleverseExceptionType.requestFailed, + ); } } catch (err) { return null; diff --git a/lib/src/telegram/models/update.dart b/lib/src/telegram/models/update.dart index dced7893..cf58480c 100644 --- a/lib/src/telegram/models/update.dart +++ b/lib/src/telegram/models/update.dart @@ -213,6 +213,8 @@ class Update { } else { throw TeleverseException( "The update type is unknown", + description: "The given update does not match any known update type.", + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/televerse/bot.dart b/lib/src/televerse/bot.dart index 1d35b7a1..3039988b 100644 --- a/lib/src/televerse/bot.dart +++ b/lib/src/televerse/bot.dart @@ -54,6 +54,7 @@ class Bot { throw TeleverseException( "Bot instance not found. ", description: "Create a Bot instance with a token first.", + type: TeleverseExceptionType.botNotInitialized, ); } } @@ -194,6 +195,7 @@ class Bot { description: "This happens when the initial getMe request is not completed. You can call `bot.getMe` method to set this property.", stackTrace: stack, + type: TeleverseExceptionType.requestFailed, ); } } diff --git a/lib/src/televerse/context/context.dart b/lib/src/televerse/context/context.dart index 5dedfd13..5913f352 100644 --- a/lib/src/televerse/context/context.dart +++ b/lib/src/televerse/context/context.dart @@ -46,6 +46,7 @@ class Context { if (chat == null) { throw TeleverseException( "The update type is ${update.type}, which does not have a chat.", + type: TeleverseExceptionType.updateTypeDoesNotHaveChat, ); } return ChatID(chat!.id); @@ -210,6 +211,7 @@ class Context { throw TeleverseException( "The context does not contain necessary information to call the method `$method`.", description: description, + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/televerse/links/user.dart b/lib/src/televerse/links/user.dart index b83d4c5c..e3fff971 100644 --- a/lib/src/televerse/links/user.dart +++ b/lib/src/televerse/links/user.dart @@ -23,5 +23,7 @@ class UserLink extends Deeplink { @override String get https => throw TeleverseException( "https://t.me/ links are not supported for user links", + description: "User links are not supported for t.me links.", + type: TeleverseExceptionType.unsupported, ); } diff --git a/lib/src/televerse/models/chat_id.dart b/lib/src/televerse/models/chat_id.dart index 293d2332..9757610c 100644 --- a/lib/src/televerse/models/chat_id.dart +++ b/lib/src/televerse/models/chat_id.dart @@ -28,6 +28,7 @@ abstract class ID { throw TeleverseException( "The passed value is not a valid chat id. The value must be an integer or a string.", description: "The passed value is of type ${value.runtimeType}.", + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/televerse/models/input_file.dart b/lib/src/televerse/models/input_file.dart index 439212c8..fd3a25d4 100644 --- a/lib/src/televerse/models/input_file.dart +++ b/lib/src/televerse/models/input_file.dart @@ -84,7 +84,10 @@ class InputFile { } else if (bytes != null) { return InputFileType.bytes; } else { - throw TeleverseException('InputFile must have a value'); + throw TeleverseException( + 'InputFile must have a value', + type: TeleverseExceptionType.invalidParameter, + ); } } @@ -107,6 +110,7 @@ class InputFile { } else { throw TeleverseException( 'InputFile must be created with either [InputFile.fromBytes] or [InputFile.fromFile]', + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/televerse/models/televerse_exception.dart b/lib/src/televerse/models/televerse_exception.dart index 5035a910..b95ab272 100644 --- a/lib/src/televerse/models/televerse_exception.dart +++ b/lib/src/televerse/models/televerse_exception.dart @@ -1,7 +1,48 @@ part of 'models.dart'; +/// Enum to classify different types of Televerse exceptions. +enum TeleverseExceptionType { + /// File does not exist. + fileDoesNotExist, + + /// Sessions are not enabled. + sessionsNotEnabled, + + /// Session ID is not set. + sessionIdNotSet, + + /// Thrown when a API request is failed + requestFailed, + + /// Thrown when the timeout exception occurs. + timeoutException, + + /// Parameter Exception + /// + /// This type of exception is thrown when a request is made with invalid parameters. + invalidParameter, + + /// Bot is not initialized. + /// + /// This type of exception is thrown when a method is called before even creating the bot instance. + botNotInitialized, + + /// Update type doesn't have a chat. + /// + /// This type of exception is thrown when the update type doesn't have a chat. + updateTypeDoesNotHaveChat, + + /// Unsupported feature. + /// + /// This type of exception is thrown when a feature is not supported. + unsupported, +} + /// TeleverseException is thrown when an error occurs in the library. class TeleverseException implements Exception { + /// The type of the exception. + final TeleverseExceptionType type; + /// The error message. final String message; @@ -16,6 +57,7 @@ class TeleverseException implements Exception { this.message, { this.description, StackTrace? stackTrace, + required this.type, }) : stackTrace = stackTrace ?? StackTrace.current; /// Returns a string representation of the exception. @@ -30,6 +72,7 @@ class TeleverseException implements Exception { "File does not exist", description: "The file $path does not exist. Please check the path and try again.", + type: TeleverseExceptionType.fileDoesNotExist, ); } @@ -38,6 +81,7 @@ class TeleverseException implements Exception { "Sessions aren't enabled for the bot ", description: "To use sessions, enable them using `Televerse.initSessions()` method.", + type: TeleverseExceptionType.sessionsNotEnabled, ); /// Exception thrown when the session is tried to be saved without providing path or id. @@ -45,6 +89,7 @@ class TeleverseException implements Exception { "Session ID not set", description: "To save/load the session, you must provide a path or set the session ID.", + type: TeleverseExceptionType.sessionIdNotSet, ); /// Exception thrown when the `getMe` request is failed when setting up a bot command. @@ -54,13 +99,7 @@ class TeleverseException implements Exception { description: "The request to getMe failed. Please check your internet connection and try again. \n\nError: $err", stackTrace: stack, - ); - - /// Exception thrown when bytes are not provided in case of uploading a file. - static TeleverseException bytesNotProvided() => TeleverseException( - "Bytes not provided", - description: - "You must provide the bytes of the file to upload. Use the `InputFile.fromBytes` constructor to create an InputFile.", + type: TeleverseExceptionType.requestFailed, ); /// Exception thrown when the timeout exception occurs. @@ -74,6 +113,7 @@ class TeleverseException implements Exception { " 2. Check your internet connection.\n" " 3. Attach a error handler using `Bot.onError` to handle the timeout exception.", stackTrace: st, + type: TeleverseExceptionType.timeoutException, ); } } diff --git a/lib/src/televerse/raw_api.dart b/lib/src/televerse/raw_api.dart index 8e95a179..0b001d49 100644 --- a/lib/src/televerse/raw_api.dart +++ b/lib/src/televerse/raw_api.dart @@ -748,12 +748,14 @@ class RawAPI { throw TeleverseException( "Invalid Parameter in [sendMediaGroup]", description: "The maximum number of items in a media group is 10.", + type: TeleverseExceptionType.invalidParameter, ); } if (media.length < 2) { throw TeleverseException( "Invalid Parameter in [sendMediaGroup]", description: "The minimum number of items in a media group is 2.", + type: TeleverseExceptionType.invalidParameter, ); } bool containsInvalidType = media.any((m) { @@ -765,6 +767,7 @@ class RawAPI { "Invalid Parameter in [sendMediaGroup]", description: "Audio and Animation files can't be sent in a media group.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1050,6 +1053,7 @@ class RawAPI { "The number of options must be between 2 and 10", description: "You provided ${options.length} options. Please provide between 2 and 10 options.", + type: TeleverseExceptionType.invalidParameter, ); } if (closeDate != null && openPeriod != null) { @@ -1057,6 +1061,7 @@ class RawAPI { "You can't provide both a close date and an open period", description: "You provided both a close date and an open period. Please provide only one of them.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1065,6 +1070,7 @@ class RawAPI { "The close date must be in the future", description: "The close date you provided is in the past. Please provide a date in the future.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1073,6 +1079,7 @@ class RawAPI { "The open period must be at least 5 seconds", description: "The open period you provided is less than 5 seconds. Please provide a period of at least 5 seconds.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1081,6 +1088,7 @@ class RawAPI { "The open period must be at most 600 seconds", description: "The open period you provided is more than 600 seconds. Please provide a period of at most 600 seconds.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1089,6 +1097,7 @@ class RawAPI { "You must provide a correct option ID for a quiz", description: "You provided a quiz poll type but did not provide a correct option ID. Please provide a correct option ID.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1418,16 +1427,18 @@ class RawAPI { }) async { if (expireDate != null && expireDate.isBefore(DateTime.now())) { throw TeleverseException( - "Invalid Argument [expireDate]", + "Invalid Parameter [expireDate]", description: "Expire date must be in the future", + type: TeleverseExceptionType.invalidParameter, ); } if (createsJoinRequest == true && memberLimit != null) { throw TeleverseException( - "Invalid Arguments", + "Invalid Parameter", description: - "You can't set a member limit and allow join requests at the same time", + "You can't set a member limit and allow join requests at the same time.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1436,6 +1447,7 @@ class RawAPI { "Invalid Argument [memberLimit]", description: "The maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1464,24 +1476,27 @@ class RawAPI { }) async { if (expireDate != null && expireDate.isBefore(DateTime.now())) { throw TeleverseException( - "Invalid Argument [expireDate]", + "Invalid Parameter [expireDate]", description: "Expire date must be in the future", + type: TeleverseExceptionType.invalidParameter, ); } if (createsJoinRequest == true && memberLimit != null) { throw TeleverseException( - "Invalid Arguments", + "Invalid Parameter", description: "You can't set a member limit and allow join requests at the same time", + type: TeleverseExceptionType.invalidParameter, ); } if (memberLimit != null && (memberLimit < 1 || memberLimit > 99999)) { throw TeleverseException( - "Invalid Argument [memberLimit]", + "Invalid Parameter [memberLimit]", description: "The maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1597,8 +1612,9 @@ class RawAPI { ) async { if (title.length > 128) { throw TeleverseException( - "Invalid Argument [title]", + "Invalid Parameter [title]", description: "Chat title can't be longer than 128 characters", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1620,8 +1636,9 @@ class RawAPI { ) async { if (description != null && description.length > 255) { throw TeleverseException( - "Invalid Argument [description]", + "Invalid Parameter [description]", description: "Chat description can't be longer than 255 characters", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1815,7 +1832,11 @@ class RawAPI { String? iconCustomEmojiId, }) async { if (name.isEmpty || name.length > 128) { - throw TeleverseException("Name length must be between 1 and 64"); + throw TeleverseException( + "Invalid parameter [name]", + description: "Name length must be between 1 and 64", + type: TeleverseExceptionType.invalidParameter, + ); } Map params = { @@ -1842,7 +1863,9 @@ class RawAPI { }) async { if (name != null && (name.isEmpty || name.length > 128)) { throw TeleverseException( - "Name length must be between 1 and 128", + "Invalid parameter [name]", + description: "Name length must be between 1 and 128", + type: TeleverseExceptionType.invalidParameter, ); } @@ -1929,7 +1952,11 @@ class RawAPI { String name, ) async { if (name.isEmpty || name.length > 128) { - throw TeleverseException("name length must be between 1 and 64"); + throw TeleverseException( + "Invalid Parameter [name]", + description: "Length of name must be between 1 and 64", + type: TeleverseExceptionType.invalidParameter, + ); } Map params = { @@ -2531,9 +2558,10 @@ class RawAPI { ); } else { throw TeleverseException( - "uploadStickerFile", + "Invalid parameter [sticker]", description: "Only upload file. Use [InputFile.fromFile] to upload file.", + type: TeleverseExceptionType.invalidParameter, ); } @@ -2828,6 +2856,7 @@ class RawAPI { throw TeleverseException( "Invalid Parameter [shippingOptions]", description: "shippingOptions is required if ok is True", + type: TeleverseExceptionType.invalidParameter, ); } @@ -2835,6 +2864,7 @@ class RawAPI { throw TeleverseException( "Invalid Parameter [errorMessage]", description: "errorMessage is required if ok is False", + type: TeleverseExceptionType.invalidParameter, ); } @@ -2868,6 +2898,7 @@ class RawAPI { throw TeleverseException( "Invalid Parameter [errorMessage]", description: "errorMessage is required if ok is False", + type: TeleverseExceptionType.invalidParameter, ); } @@ -3006,6 +3037,7 @@ class RawAPI { "Invalid Parameter", description: "chatId, messageId, and inlineMessageId cannot all be null", + type: TeleverseExceptionType.invalidParameter, ); } diff --git a/lib/src/types/chat_boost_source_type.dart b/lib/src/types/chat_boost_source_type.dart index 19461e19..c58ae026 100644 --- a/lib/src/types/chat_boost_source_type.dart +++ b/lib/src/types/chat_boost_source_type.dart @@ -32,6 +32,7 @@ enum ChatBoostSourceType { "Invalid value '$json' for ChatBoostSourceType.", description: 'The given value does not match any ChatBoostSourceType.', + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/types/message_origin_type.dart b/lib/src/types/message_origin_type.dart index 8ca3389f..151bf063 100644 --- a/lib/src/types/message_origin_type.dart +++ b/lib/src/types/message_origin_type.dart @@ -34,6 +34,8 @@ enum MessageOriginType { default: throw TeleverseException( 'Invalid MessageOriginType value.', + description: 'The given value does not match any MessageOriginType.', + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/types/parse_mode.dart b/lib/src/types/parse_mode.dart index c22a7863..e8777e8a 100644 --- a/lib/src/types/parse_mode.dart +++ b/lib/src/types/parse_mode.dart @@ -27,7 +27,11 @@ enum ParseMode { case 'MarkdownV2': return markdownV2; default: - throw TeleverseException('Invalid ParseMode value: $value'); + throw TeleverseException( + 'Invalid ParseMode value: $value', + description: 'The given value does not match any ParseMode.', + type: TeleverseExceptionType.invalidParameter, + ); } } } diff --git a/lib/src/types/reaction_type_emoji_type.dart b/lib/src/types/reaction_type_emoji_type.dart index faac3f82..945b3ae0 100644 --- a/lib/src/types/reaction_type_emoji_type.dart +++ b/lib/src/types/reaction_type_emoji_type.dart @@ -27,6 +27,7 @@ enum ReactionTypeType { "Unknown value: $value", description: "ReactionType.type got an unknown value. For now ReactionTypeType only supports emoji and custom_emoji.\n\nIf you're sure this is a valid value, please let us know by creating an issue at https://github.com/HeySreelal/televerse/issues", + type: TeleverseExceptionType.invalidParameter, ); } } diff --git a/lib/src/types/sticker_type.dart b/lib/src/types/sticker_type.dart index 180abac1..03af19ea 100644 --- a/lib/src/types/sticker_type.dart +++ b/lib/src/types/sticker_type.dart @@ -32,7 +32,11 @@ enum StickerType { case "custom_emoji": return StickerType.customEmoji; default: - throw TeleverseException("Unknown StickerType: $type"); + throw TeleverseException( + "Unknown StickerType: $type", + description: "The given value does not match any StickerType.", + type: TeleverseExceptionType.invalidParameter, + ); } }