Skip to content

Commit

Permalink
fix: improve error handling in chat client
Browse files Browse the repository at this point in the history
Enhance error handling and message parsing in the chat client:
- Add null check for job before shutdown
- Improve JSON parsing error handling with dedicated utility function
- Clean up stream event handling
- Simplify error message passing in curl utilities
- Remove redundant string conversions for error messages

The changes make the error handling more robust and consistent across
the codebase while reducing unnecessary string transformations.
  • Loading branch information
deathbeam committed Feb 21, 2025
1 parent ba97b4a commit 07d7253
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
39 changes: 20 additions & 19 deletions lua/CopilotChat/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,10 @@ function Client:ask(prompt, opts)

log.debug('Finishing stream', err)
finished = true
job:shutdown(0)

if job then
job:shutdown(0)
end
end

local function parse_line(line, job)
Expand All @@ -519,20 +522,15 @@ function Client:ask(prompt, opts)
log.debug('Response line: ', line)
notify.publish(notify.STATUS, '')

local ok, content = pcall(vim.json.decode, line, {
luanil = {
object = true,
array = true,
},
})
local content, err = utils.json_decode(line)

if not ok then
if job then
finish_stream(
'Failed to parse response: ' .. utils.make_string(content) .. '\n' .. line,
job
)
end
if err then
finish_stream(line, job)
return
end

if type(content) ~= 'table' then
finish_stream(content, job)
return
end

Expand All @@ -550,7 +548,7 @@ function Client:ask(prompt, opts)
on_progress(out.content)
end

if job and out.finish_reason then
if out.finish_reason then
local reason = out.finish_reason
if reason == 'stop' then
reason = nil
Expand All @@ -563,13 +561,16 @@ function Client:ask(prompt, opts)

local function parse_stream_line(line, job)
line = vim.trim(line)

if vim.startswith(line, 'event:') then
return
end

line = line:gsub('^data:', '')
line = vim.trim(line)

if line == '[DONE]' then
if job then
finish_stream(nil, job)
end
finish_stream(nil, job)
return
end

Expand All @@ -587,7 +588,7 @@ function Client:ask(prompt, opts)
end

if err then
finish_stream('Failed to get response: ' .. utils.make_string(err and err or line), job)
finish_stream(err and err or line, job)
return
end

Expand Down
5 changes: 2 additions & 3 deletions lua/CopilotChat/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ end
M.curl_get = async.wrap(function(url, opts, callback)
local args = {
on_error = function(err)
callback(nil, M.make_string(err and err.stderr or err))
callback(nil, err and err.stderr or err)
end,
}

Expand Down Expand Up @@ -383,8 +383,7 @@ M.curl_post = async.wrap(function(url, opts, callback)
local args = {
callback = callback,
on_error = function(err)
err = M.make_string(err and err.stderr or err)
callback(nil, err)
callback(nil, err and err.stderr or err)
end,
}

Expand Down

0 comments on commit 07d7253

Please sign in to comment.