From 1e640399b85974dd8380ececdecd048c973e14db Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Tue, 22 Oct 2024 01:32:20 +0200 Subject: [PATCH] Rework catching JellyfinConfigNotFoundException --- .editorconfig | 9 +++++++++ .gitignore | 1 + bin/running_on_dart.dart | 21 ++++++++++++++++----- lib/src/commands/jellyfin.dart | 7 ++++++- lib/src/modules/jellyfin.dart | 6 +++--- 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..47860bf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*.dart] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.gitignore b/.gitignore index beffca7..31a0809 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ doc/api/ # IDE configuration folders .vscode/ .idea/ +*.iml # Build files *.exe diff --git a/bin/running_on_dart.dart b/bin/running_on_dart.dart index 1a54f48..cfd5818 100644 --- a/bin/running_on_dart.dart +++ b/bin/running_on_dart.dart @@ -4,6 +4,7 @@ import 'package:nyxx_extensions/nyxx_extensions.dart'; import 'package:running_on_dart/running_on_dart.dart'; import 'package:running_on_dart/src/commands/reminder.dart'; import 'package:running_on_dart/src/commands/tag.dart'; +import 'package:running_on_dart/src/models/jellyfin_config.dart'; import 'package:running_on_dart/src/modules/bot_start_duration.dart'; import 'package:injector/injector.dart'; @@ -49,13 +50,25 @@ void main() async { return; } + if (error is ConverterFailedException && error.context is CommandContext) { + switch (error.failed) { + case Converter(): + case Converter(): + (error.context as CommandContext) + .respond(MessageBuilder(content: "Cannot parse jellyfin config"), level: ResponseLevel.private); + break; + } + + return; + } + if (error is UncaughtException) { final context = error.context; switch (error.exception) { - // case JellyfinConfigNotFoundException(:final message): - // error.context.respond(MessageBuilder(content: message)); - // break; + case JellyfinConfigNotFoundException(:final message): + error.context.respond(MessageBuilder(content: message)); + break; case JellyfinAdminUserRequired _: context.respond( MessageBuilder(content: "This command can use only logged jellyfin users with administrator privileges."), @@ -83,8 +96,6 @@ void main() async { content: 'Cannot provide config automatically. Login manually using: `/jellyfin user login`.'), level: ResponseLevel.private); break; - case _: - print(error.exception); } } }); diff --git a/lib/src/commands/jellyfin.dart b/lib/src/commands/jellyfin.dart index ebb9a29..973d15d 100644 --- a/lib/src/commands/jellyfin.dart +++ b/lib/src/commands/jellyfin.dart @@ -29,6 +29,10 @@ Future getJellyfinClient(JellyfinConfigUser? config config ??= await Injector.appInstance .get() .fetchGetUserConfigWithFallback(userId: context.user.id, parentId: context.guild?.id ?? context.user.id); + + if (config == null) { + throw JellyfinConfigNotFoundException("Invalid jellyfin config or user not logged in."); + } return Injector.appInstance.get().createJellyfinClientAuthenticated(config); } @@ -273,7 +277,8 @@ final jellyfin = ChatGroup("jellyfin", "Jellyfin Testing Commands", checks: [ "Login with password to given jellyfin instance", id("jellyfin-user-login", (InteractionChatContext context, JellyfinConfig config) async { return context.respond( - getJellyfinLoginMessage(userId: context.user.id, configName: config.name, parentId: config.parentId)); + getJellyfinLoginMessage(userId: context.user.id, configName: config.name, parentId: config.parentId), + level: ResponseLevel.private); }), ), ChatCommand( diff --git a/lib/src/modules/jellyfin.dart b/lib/src/modules/jellyfin.dart index ce99c00..0851b27 100644 --- a/lib/src/modules/jellyfin.dart +++ b/lib/src/modules/jellyfin.dart @@ -570,18 +570,18 @@ class JellyfinModuleV2 implements RequiresInitialization { return SonarrClient(baseUrl: config.sonarrBasePath!, token: config.sonarrToken!); } - Future fetchGetUserConfigWithFallback( + Future fetchGetUserConfigWithFallback( {required Snowflake userId, required Snowflake parentId, String? instanceName}) async { final config = instanceName != null ? await getJellyfinConfig(instanceName, parentId) : await getJellyfinDefaultConfig(parentId); if (config == null) { - throw JellyfinConfigNotFoundException("Missing jellyfin config"); + return null; } final userConfig = await fetchJellyfinUserConfig(userId, config); if (userConfig == null) { - throw JellyfinConfigNotFoundException("User not logged in."); + return null; } return userConfig;