Generate Runtime Exception

This commit is contained in:
ohAlee 2023-03-15 16:33:07 +01:00
parent 4388494ab6
commit ef300d82cc
2 changed files with 3 additions and 2 deletions

View File

@ -31,12 +31,13 @@ public class OpenAI {
.build())
.getChoices().get(0).getText()).exceptionally(throwable -> {
if (throwable.getCause() instanceof HttpException e) {
return switch (e.response().code()) {
String reason = switch (e.response().code()) {
case 401 -> "Invalid API key! Please check your configuration.";
case 429 -> "Too many requests! Please wait a few seconds and try again.";
case 500 -> "OpenAI service is currently unavailable. Please try again later.";
default -> "Unknown error! Please try again later. If this error persists, contact the plugin developer.";
};
throw new RuntimeException(reason, throwable);
}
throw new RuntimeException(throwable);
});

View File

@ -52,7 +52,7 @@ public class PlayerHandlers implements Listener {
if (builder == null) builder = new StringBuilder();
OpenAI.getResponse(plugin.getConfig().getConfigurationSection("chatgpt"), builder, e.getMessage()).whenComplete((response, throwable) -> {
if (response == null) {
if (throwable != null) {
player.sendMessage(Messages.format(plugin.getConfig().getString("command.error")));
return;
}