Skip to content

Commit

Permalink
Show only error message from curl instead of whole object
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Nov 15, 2024
1 parent 683e07e commit 76bfba5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ local curl_get = async.wrap(function(url, opts, callback)
opts = vim.tbl_deep_extend('force', opts, {
callback = callback,
on_error = function(err)
callback(nil, vim.inspect(err))
err = err and err.stderr or vim.inspect(err)
callback(nil, err)
end,
})
curl.get(url, opts)
Expand All @@ -69,7 +70,8 @@ local curl_post = async.wrap(function(url, opts, callback)
opts = vim.tbl_deep_extend('force', opts, {
callback = callback,
on_error = function(err)
callback(nil, vim.inspect(err))
err = err and err.stderr or vim.inspect(err)
callback(nil, err)
end,
})
curl.post(url, opts)
Expand Down
37 changes: 26 additions & 11 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ local function blend_color_with_neovim_bg(color_name, blend)
return string.format('#%02x%02x%02x', r, g, b)
end

local function dedupe_strings(str)
if not str then
return str
end
local seen = {}
local result = {}
for s in str:gmatch('[^%s,]+') do
if not seen[s] then
seen[s] = true
table.insert(result, s)
end
end
return table.concat(result, ' ')
end

local function get_error_message(err)
if type(err) == 'string' then
-- Match first occurrence of :something: and capture rest
local message = err:match('^[^:]+:[^:]+:(.+)') or err
-- Trim whitespace
message = message:match('^%s*(.-)%s*$')
return dedupe_strings(message)
end
return dedupe_strings(vim.inspect(err))
end

local function find_lines_between_separator(lines, pattern, at_least_one)
local line_count = #lines
local separator_line_start = 1
Expand Down Expand Up @@ -393,17 +419,6 @@ function M.ask(prompt, config, source)
M.stop(true, config)
end

local function get_error_message(err)
if type(err) == 'string' then
-- Match first occurrence of :something: and capture rest
local message = err:match('^[^:]+:[^:]+:(.+)') or err
-- Trim whitespace
message = message:match('^%s*(.-)%s*$')
return message
end
return vim.inspect(err)
end

local function on_error(err)
log.error(vim.inspect(err))
vim.schedule(function()
Expand Down

0 comments on commit 76bfba5

Please sign in to comment.