Skip to content

Commit

Permalink
🎬Merge #215: Exception types
Browse files Browse the repository at this point in the history
Added `TeleverseExceptionType` to classify exceptions
  • Loading branch information
HeySreelal authored Mar 15, 2024
2 parents 8ac850f + 17a3074 commit f699193
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/src/telegram/models/abstracts/chat_boost_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/telegram/models/abstracts/chat_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/src/telegram/models/abstracts/input_message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
}
7 changes: 6 additions & 1 deletion lib/src/telegram/models/abstracts/message_origin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/telegram/models/abstracts/reaction_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
Expand Down
8 changes: 7 additions & 1 deletion lib/src/telegram/models/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/telegram/models/update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/televerse/bot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Bot<TeleverseSession extends Session> {
throw TeleverseException(
"Bot instance not found. ",
description: "Create a Bot instance with a token first.",
type: TeleverseExceptionType.botNotInitialized,
);
}
}
Expand Down Expand Up @@ -194,6 +195,7 @@ class Bot<TeleverseSession extends Session> {
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,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/televerse/context/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Context<TeleverseSession extends Session> {
if (chat == null) {
throw TeleverseException(
"The update type is ${update.type}, which does not have a chat.",
type: TeleverseExceptionType.updateTypeDoesNotHaveChat,
);
}
return ChatID(chat!.id);
Expand Down Expand Up @@ -210,6 +211,7 @@ class Context<TeleverseSession extends Session> {
throw TeleverseException(
"The context does not contain necessary information to call the method `$method`.",
description: description,
type: TeleverseExceptionType.invalidParameter,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/televerse/links/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
1 change: 1 addition & 0 deletions lib/src/televerse/models/chat_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/televerse/models/input_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}

Expand All @@ -107,6 +110,7 @@ class InputFile {
} else {
throw TeleverseException(
'InputFile must be created with either [InputFile.fromBytes] or [InputFile.fromFile]',
type: TeleverseExceptionType.invalidParameter,
);
}
}
Expand Down
54 changes: 47 additions & 7 deletions lib/src/televerse/models/televerse_exception.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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.
Expand All @@ -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,
);
}

Expand All @@ -38,13 +81,15 @@ 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.
static TeleverseException sessionIdNotSet = TeleverseException(
"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.
Expand All @@ -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.
Expand All @@ -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,
);
}
}
Loading

0 comments on commit f699193

Please sign in to comment.