Skip to content

Commit

Permalink
Rework catching JellyfinConfigNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Oct 21, 2024
1 parent df03673 commit 1e64039
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ doc/api/
# IDE configuration folders
.vscode/
.idea/
*.iml

# Build files
*.exe
Expand Down
21 changes: 16 additions & 5 deletions bin/running_on_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -49,13 +50,25 @@ void main() async {
return;
}

if (error is ConverterFailedException && error.context is CommandContext) {
switch (error.failed) {
case Converter<JellyfinConfig>():
case Converter<JellyfinConfigUser>():
(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."),
Expand Down Expand Up @@ -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);
}
}
});
Expand Down
7 changes: 6 additions & 1 deletion lib/src/commands/jellyfin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Future<AuthenticatedJellyfinClient> getJellyfinClient(JellyfinConfigUser? config
config ??= await Injector.appInstance
.get<JellyfinModuleV2>()
.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<JellyfinModuleV2>().createJellyfinClientAuthenticated(config);
}

Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions lib/src/modules/jellyfin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,18 @@ class JellyfinModuleV2 implements RequiresInitialization {
return SonarrClient(baseUrl: config.sonarrBasePath!, token: config.sonarrToken!);
}

Future<JellyfinConfigUser> fetchGetUserConfigWithFallback(
Future<JellyfinConfigUser?> 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;
Expand Down

0 comments on commit 1e64039

Please sign in to comment.