From 7af303b42151ab8e4043aedd644047c96a132d95 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 24 Jul 2025 16:03:15 +0800 Subject: [PATCH] fix: missing curl error message (#2527) --- lua/avante/llm_tools/edit_file.lua | 55 +++++++++++++++++++++--- lua/avante/llm_tools/replace_in_file.lua | 2 +- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lua/avante/llm_tools/edit_file.lua b/lua/avante/llm_tools/edit_file.lua index 70a8104..595962e 100644 --- a/lua/avante/llm_tools/edit_file.lua +++ b/lua/avante/llm_tools/edit_file.lua @@ -99,7 +99,7 @@ M.func = vim.schedule_wrap(function(input, opts) local json_content = vim.json.encode(body) vim.fn.writefile(vim.split(json_content, "\n"), curl_body_file) - -- Construct curl command + -- Construct curl command with additional debugging and error handling local curl_cmd = { "curl", "-X", @@ -108,6 +108,13 @@ M.func = vim.schedule_wrap(function(input, opts) "Content-Type: application/json", "-d", "@" .. curl_body_file, + "--fail", -- Return error for HTTP status codes >= 400 + "--show-error", -- Show error messages + "--verbose", -- Enable verbose output for better debugging + "--connect-timeout", + "30", -- Connection timeout in seconds + "--max-time", + "120", -- Maximum operation time Utils.url_join(provider_conf.endpoint, "/chat/completions"), } @@ -129,11 +136,49 @@ M.func = vim.schedule_wrap(function(input, opts) if result.code ~= 0 then local error_msg = result.stderr - if error_msg ~= nil and error_msg ~= "" then - on_complete(false, "Failed to edit file: " .. error_msg) - return + if not error_msg or error_msg == "" then error_msg = result.stdout end + if not error_msg or error_msg == "" then error_msg = "No detailed error message available" end + + -- 检查curl常见的错误码 + local curl_error_map = { + [1] = "Unsupported protocol (curl error 1)", + [2] = "Failed to initialize (curl error 2)", + [3] = "URL malformed (curl error 3)", + [4] = "Requested FTP action not supported (curl error 4)", + [5] = "Failed to resolve proxy (curl error 5)", + [6] = "Could not resolve host (DNS resolution failed)", + [7] = "Failed to connect to host (connection refused)", + [28] = "Operation timeout (connection timed out)", + [35] = "SSL connection error (handshake failed)", + [52] = "Empty reply from server", + [56] = "Failure in receiving network data", + [60] = "SSL certificate problem (certificate verification failed)", + [77] = "Problem with reading SSL CA certificate", + } + + local curl_cmd_str = table.concat(curl_cmd, " ") + local error_hint = curl_error_map[result.code] or "curl exited with code " .. result.code + local full_error = "curl command failed: " + .. error_hint + .. "\n" + .. "Command: " + .. curl_cmd_str + .. "\n" + .. "Exit code: " + .. result.code + + if error_msg and error_msg ~= "" then full_error = full_error .. "\nError details: " .. error_msg end + + if provider_conf.endpoint and provider_conf.model then + full_error = full_error + .. "\nEndpoint: " + .. provider_conf.endpoint + .. "/chat/completions" + .. "\nModel: " + .. provider_conf.model end - on_complete(false, "Failed to edit file: " .. result.code) + + on_complete(false, full_error) return end diff --git a/lua/avante/llm_tools/replace_in_file.lua b/lua/avante/llm_tools/replace_in_file.lua index c21d5a1..4520ce6 100644 --- a/lua/avante/llm_tools/replace_in_file.lua +++ b/lua/avante/llm_tools/replace_in_file.lua @@ -790,7 +790,7 @@ For example: local parent_dir = vim.fn.fnamemodify(abs_path, ":h") --- check if the parent dir is exists, if not, create it if vim.fn.isdirectory(parent_dir) == 0 then vim.fn.mkdir(parent_dir, "p") end - vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end) + vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write!") end) if session_ctx then Helpers.mark_as_not_viewed(input.path, session_ctx) end on_complete(true, nil) end, { focus = not Config.behaviour.auto_focus_on_diff_view }, session_ctx, M.name)