Skip to content

Commit 5251d38

Browse files
committed
show the error message if available
1 parent aa3f39b commit 5251d38

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

openai/init.lua

+16-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ do
5959
end)
6060
consume_json_head = S("\t\n\r ") ^ 0 * P("data: ") * C(consume_json) * C(P(1) ^ 0)
6161
end
62+
local parse_error_message = types.partial({
63+
error = types.partial({
64+
message = types.string:tag("message"),
65+
code = types.string:tag("code")
66+
})
67+
})
6268
local ChatSession
6369
do
6470
local _class_0
@@ -99,7 +105,16 @@ do
99105
stream = stream_callback and true or nil
100106
}, stream_callback)
101107
if status ~= 200 then
102-
return nil, "Bad status: " .. tostring(status), response
108+
local err_msg
109+
do
110+
local err = parse_error_message(response)
111+
if err then
112+
err_msg = "Bad status: " .. tostring(status) .. ": " .. tostring(err.message) .. " (" .. tostring(err.code) .. ")"
113+
else
114+
err_msg = "Bad status: " .. tostring(status)
115+
end
116+
end
117+
return nil, err_msg, response
103118
end
104119
if stream_callback then
105120
assert(type(response) == "string", "Expected string response from streaming output")

openai/init.moon

+12-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ consume_json_head = do
7878
S("\t\n\r ")^0 * P("data: ") * C(consume_json) * C(P(1)^0)
7979

8080

81+
parse_error_message = types.partial {
82+
error: types.partial {
83+
message: types.string\tag "message"
84+
code: types.string\tag "code"
85+
}
86+
}
8187

8288
-- handles appending response for each call to chat
8389
-- TODO: hadle appending the streaming response to the output
@@ -119,7 +125,12 @@ class ChatSession
119125
}, stream_callback
120126

121127
if status != 200
122-
return nil, "Bad status: #{status}", response
128+
err_msg = if err = parse_error_message response
129+
"Bad status: #{status}: #{err.message} (#{err.code})"
130+
else
131+
"Bad status: #{status}"
132+
133+
return nil, err_msg, response
123134

124135
-- if we are streaming we need to pase the entire fragmented response
125136
if stream_callback

0 commit comments

Comments
 (0)