feat(provider): initial error handling on claude (#221)

* fix: improve error handling for common issues

* fix: Providers.on_error for handling API errors

* Update lua/avante/providers/init.lua

---------

Co-authored-by: Aaron Pham <Aaronpham0103@gmail.com>
This commit is contained in:
Jake 2024-08-25 21:26:19 -04:00 committed by GitHub
parent 5159aeefd1
commit 312543b680
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View File

@ -148,7 +148,14 @@ M.stream = function(question, code_lang, code_content, selected_content_content,
on_error = function(err)
on_complete(err)
end,
callback = function(_)
callback = function(result)
if result.status >= 400 then
if Provider.on_error then
Provider.on_error(result)
else
Utils.error("API request failed with status " .. result.status, { once = true, title = "Avante" })
end
end
active_job = nil
end,
})

View File

@ -104,4 +104,26 @@ M.parse_curl_args = function(provider, code_opts)
}
end
M.on_error = function(result)
if not result.body then
return Utils.error("API request failed with status " .. result.status, { once = true, title = "Avante" })
end
local ok, body = pcall(vim.json.decode, result.body)
if not (ok and body and body.error) then
return Utils.error("Failed to parse error response", { once = true, title = "Avante" })
end
local error_msg = body.error.message
local error_type = body.error.type
if error_type == "insufficient_quota" then
error_msg = "You don't have any credits or have exceeded your quota. Please check your plan and billing details."
elseif error_type == "invalid_request_error" and error_msg:match("temperature") then
error_msg = "Invalid temperature value. Please ensure it's between 0 and 1."
end
Utils.error(error_msg, { once = true, title = "Avante" })
end
return M

View File

@ -72,6 +72,7 @@ local Dressing = require("avante.ui.dressing")
---@field model? string
---@field parse_api_key fun(): string | nil
---@field parse_stream_data? AvanteStreamParser
---@field on_error? fun(result: table): nil
---
---@class avante.Providers
---@field openai AvanteProviderFunctor