From ef300d82ccf2624b8cac7f750f54134509dee2fb Mon Sep 17 00:00:00 2001 From: ohAlee Date: Wed, 15 Mar 2023 16:33:07 +0100 Subject: [PATCH] Generate Runtime Exception --- src/main/java/it/ohalee/minecraftgpt/OpenAI.java | 3 ++- .../java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java index f135129..8c308f0 100644 --- a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java +++ b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java @@ -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); }); diff --git a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java index 693e56e..c9effc4 100644 --- a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java +++ b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java @@ -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; }