@@ -4,6 +4,8 @@ import com.cjcrafter.openai.chat.ChatRequest
44import com.cjcrafter.openai.chat.ChatResponse
55import com.cjcrafter.openai.chat.ChatResponseChunk
66import com.cjcrafter.openai.chat.ChatUser
7+ import com.cjcrafter.openai.exception.OpenAIError
8+ import com.cjcrafter.openai.exception.WrappedIOError
79import com.google.gson.Gson
810import com.google.gson.GsonBuilder
911import 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
0 commit comments