Skip to content

Commit

Permalink
feat: 异常信息支持自定义message提示
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq committed Aug 27, 2024
1 parent fc97955 commit 0752eba
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

public class ChannelArgument<T extends Channel> extends ArgumentResolver<CommandSender, T> {
public static final MessageKey<String> CHANNEL_NOT_FOUND = MessageKey.of("channel_not_found", "Channel not found");
public static final MessageKey<CommandException> EMOJI_FOUND_FAILURE = MessageKey.of("emoji_found_failure", "Emoji found failure");

private final HttpAPI httpAPI;
private final MessageRegistry<CommandSender> messageRegistry;
Expand All @@ -60,7 +61,7 @@ protected ParseResult<T> parse(Invocation<CommandSender> invocation, Argument<T>
}
return ParseResult.success((T) channel);
} catch (final Exception e) {
return ParseResult.failure(new CommandException("Channel not found", e));
return ParseResult.failure(messageRegistry.getInvoked(EMOJI_FOUND_FAILURE, invocation, new CommandException("Channel not found", e)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class EmojiArgument extends ArgumentResolver<CommandSender, CustomEmoji>
}
return "Unsupported command";
});
public static final MessageKey<CommandException> EMOJI_FOUND_FAILURE = MessageKey.of("emoji_found_failure", "Emoji found failure");

private final KBCClient client;
private final MessageRegistry<CommandSender> messageRegistry;
Expand Down Expand Up @@ -105,7 +106,7 @@ protected ParseResult<CustomEmoji> parse(Invocation<CommandSender> invocation, A
}
return ParseResult.failure(messageRegistry.getInvoked(NOT_CHANNEL, invocation, message));
} catch (final Exception e) {
return ParseResult.failure(new CommandException("CustomEmoji not found", e));
return ParseResult.failure(messageRegistry.getInvoked(EMOJI_FOUND_FAILURE, invocation, new CommandException("CustomEmoji not found", e)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

public class GuildArgument extends ArgumentResolver<CommandSender, Guild> {
public static final MessageKey<String> GUILD_NOT_FOUND = MessageKey.of("guild_not_found", "Guild not found");
public static final MessageKey<CommandException> GUILD_FOUND_FAILURE = MessageKey.of("guild_found_failure", "Guild found failure");

private final HttpAPI httpAPI;
private final MessageRegistry<CommandSender> messageRegistry;
Expand All @@ -52,7 +53,7 @@ protected ParseResult<Guild> parse(Invocation<CommandSender> invocation, Argumen
}
return ParseResult.success(guild);
} catch (final Exception e) {
return ParseResult.failure(new CommandException("Guild not found", e));
return ParseResult.failure(messageRegistry.getInvoked(GUILD_FOUND_FAILURE, invocation, new CommandException("Guild not found", e)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class RoleArgument extends ArgumentResolver<CommandSender, Role> {
}
return "Unsupported command";
});
public static final MessageKey<CommandException> ROLE_FOUND_FAILURE = MessageKey.of("role_found_failure", "Role found failure");

private final KBCClient client;
private final MessageRegistry<CommandSender> messageRegistry;
Expand Down Expand Up @@ -102,7 +103,7 @@ protected ParseResult<Role> parse(Invocation<CommandSender> invocation, Argument
}
return ParseResult.failure(messageRegistry.getInvoked(NOT_CHANNEL, invocation, message));
} catch (final Exception e) {
return ParseResult.failure(new CommandException("Role not found", e));
return ParseResult.failure(messageRegistry.getInvoked(ROLE_FOUND_FAILURE, invocation, new CommandException("Role not found", e)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

public class UserArgument extends ArgumentResolver<CommandSender, User> {
public static final MessageKey<String> USER_NOT_FOUND = MessageKey.of("user_not_found", "User not found");
public static final MessageKey<CommandException> USER_FOUND_FAILURE = MessageKey.of("user_found_failure", "User found failure");

private final HttpAPI httpAPI;
private final MessageRegistry<CommandSender> messageRegistry;
Expand All @@ -58,7 +59,7 @@ protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument
}
return ParseResult.success(user);
} catch (final Exception e) {
return ParseResult.failure(new CommandException("User not found", e));
return ParseResult.failure(messageRegistry.getInvoked(USER_FOUND_FAILURE, invocation, new CommandException("User not found", e)));
}
}

Expand Down

0 comments on commit 0752eba

Please sign in to comment.