Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/main/java/de/pixelmindmc/pixelchat/utils/APIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import de.pixelmindmc.pixelchat.model.MessageClassification;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -77,8 +74,10 @@ public APIHelper(@NotNull PixelChat plugin) {
return processResponse(jsonResponse);
} else {
String errorResponse = decodeResponse(connection);
throw new MessageClassificationException("HTTP error code: " + responseCode + ", Error message: " + errorResponse);
throw createHttpException(responseCode, errorResponse);
}
} catch (MessageClassificationException e) {
throw e; //Re-raise previously thrown exception
} catch (IOException e) {
throw new MessageClassificationException("Failed to classify message due to an IO issue.", e);
} catch (URISyntaxException e) {
Expand All @@ -88,6 +87,28 @@ public APIHelper(@NotNull PixelChat plugin) {
}
}

/**
* Creates a MessageClassificationException based on the HTTP response code and error response
*
* @param responseCode The response code returned from the API
* @param errorResponse The JSON-string error response from the API
* @return A MessageClassificationException with the appropriate message
*/
private @NotNull MessageClassificationException createHttpException(int responseCode, @NotNull String errorResponse) {
JsonObject jsonObject = JsonParser.parseString(errorResponse).getAsJsonObject();
String apiErrorCode = jsonObject.getAsJsonObject("error").get("code").getAsString();

if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED && "invalid_api_key".equals(apiErrorCode)) {
return new MessageClassificationException("Invalid API key. Please check your configuration.");
}

if (responseCode == HttpURLConnection.HTTP_NOT_FOUND && "model_not_found".equals(apiErrorCode)) {
return new MessageClassificationException("Model not found. Please check your configuration.");
}

return new MessageClassificationException("HTTP error code: " + responseCode + ", Error message: " + errorResponse);
}

/**
* Sets up a connection to the API server and configures it
*
Expand Down Expand Up @@ -169,7 +190,15 @@ private void sendRequest(@NotNull HttpURLConnection connection, @NotNull String
*/
private @NotNull String decodeResponse(@NotNull HttpURLConnection connection) throws IOException {
StringBuilder response = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
InputStream stream;

try {
stream = connection.getInputStream();
} catch (IOException _) {
stream = connection.getErrorStream();
}

try (BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
Expand Down
Loading