Skip to content

Commit 707efa4

Browse files
committed
add wrapped io error and error parsing from json
1 parent 015d245 commit 707efa4

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

src/main/kotlin/com/cjcrafter/openai/OpenAI.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.cjcrafter.openai.chat.ChatRequest
44
import com.cjcrafter.openai.chat.ChatResponse
55
import com.cjcrafter.openai.chat.ChatResponseChunk
66
import com.cjcrafter.openai.chat.ChatUser
7+
import com.cjcrafter.openai.exception.OpenAIError
8+
import com.cjcrafter.openai.exception.WrappedIOError
79
import com.google.gson.Gson
810
import com.google.gson.GsonBuilder
911
import com.google.gson.JsonObject
@@ -61,10 +63,9 @@ class OpenAI @JvmOverloads constructor(
6163
*
6264
* @param request The input information for ChatGPT.
6365
* @return The returned response.
64-
* @throws IOException If an IO Exception occurs.
65-
* @throws IllegalArgumentException If the input arguments are invalid.
66+
* @throws OpenAIError Invalid request/timeout/io/etc.
6667
*/
67-
@Throws(IOException::class)
68+
@Throws(OpenAIError::class)
6869
fun createChatCompletion(request: ChatRequest): ChatResponse {
6970
request.stream = false // use streamResponse for stream=true
7071
val httpRequest = buildRequest(request)
@@ -77,11 +78,12 @@ class OpenAI @JvmOverloads constructor(
7778
// Servers respond to API calls with json blocks. Since raw JSON isn't
7879
// very developer friendly, we wrap for easy data access.
7980
rootObject = JsonParser.parseString(response.body!!.string()).asJsonObject
80-
require(!rootObject!!.has("error")) { rootObject!!.get("error").asJsonObject["message"].asString }
81+
if (rootObject!!.has("error"))
82+
throw OpenAIError.fromJson(rootObject!!.get("error").asJsonObject)
8183
return ChatResponse(rootObject!!)
8284
}
83-
} catch (ex: Throwable) {
84-
throw ex
85+
} catch (ex: IOException) {
86+
throw WrappedIOError(ex)
8587
}
8688
}
8789

src/main/kotlin/com/cjcrafter/openai/exception/OpenAIError.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ abstract class OpenAIError : Exception {
1313
this.param = param
1414
this.code = code
1515
}
16+
1617
constructor(param: JsonElement?, code: String?, message: String, cause: Throwable?) : super(message, cause) {
1718
this.param = param
1819
this.code = code
1920
}
21+
2022
constructor(param: JsonElement?, code: String?, cause: Throwable?) : super(cause) {
2123
this.param = param
2224
this.code = code
@@ -27,9 +29,9 @@ abstract class OpenAIError : Exception {
2729
@JvmStatic
2830
fun fromJson(json: JsonObject) : OpenAIError {
2931
val message = json["message"].asString
30-
val type = json["type"].asString
31-
val param = json["param"]
32-
val code = json["code"].asString
32+
val type = if (json["type"].isJsonNull) null else json["type"].asString
33+
val param = if (json["param"].isJsonNull) null else json["param"]
34+
val code = if (json["code"].isJsonNull) null else json["code"].asString
3335

3436
// TODO add more error types
3537
return when (json["type"].asString) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.cjcrafter.openai.exception
2+
3+
import java.io.IOException
4+
5+
class WrappedIOError(val exception: IOException) : OpenAIError(null, null, exception.message ?: "")

src/test/java/JavaChatTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import com.cjcrafter.openai.OpenAI;
22
import com.cjcrafter.openai.chat.*;
3+
import com.cjcrafter.openai.exception.OpenAIError;
34
import io.github.cdimascio.dotenv.Dotenv;
45

56
import java.io.IOException;
@@ -9,7 +10,7 @@
910

1011
public class JavaChatTest {
1112

12-
public static void main(String[] args) throws IOException {
13+
public static void main(String[] args) throws OpenAIError {
1314
Scanner scan = new Scanner(System.in);
1415

1516
// This is the prompt that the bot will refer back to for every message.

src/test/java/com/cjcrafter/openai/ExceptionTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cjcrafter.openai;
22

33
import com.cjcrafter.openai.chat.*;
4+
import com.cjcrafter.openai.exception.InvalidRequestError;
45
import io.github.cdimascio.dotenv.Dotenv;
56
import org.junit.jupiter.api.Test;
67

@@ -18,9 +19,9 @@ void test_invalidModel() {
1819
String initialPrompt = "Just say hi";
1920
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
2021
ChatRequest request = new ChatRequest("gpt-238974-invalid-model", messages);
21-
ChatBot bot = new ChatBot(key);
22+
OpenAI openai = new OpenAI(key);
2223

23-
assertThrows(Throwable.class, () -> bot.generateResponse(request));
24+
assertThrows(InvalidRequestError.class, () -> openai.createChatCompletion(request));
2425
}
2526

2627
@Test
@@ -30,8 +31,8 @@ void test_invalidToken() {
3031
String initialPrompt = "Just say hi";
3132
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
3233
ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
33-
ChatBot bot = new ChatBot(key);
34+
OpenAI openai = new OpenAI(key);
3435

35-
assertThrows(Throwable.class, () -> bot.generateResponse(request));
36+
assertThrows(InvalidRequestError.class, () -> openai.createChatCompletion(request));
3637
}
3738
}

0 commit comments

Comments
 (0)