CopilotC-Nvim / CopilotChat.nvim

Chat with GitHub Copilot in Neovim
https://copilotc-nvim.github.io/CopilotChat.nvim/
GNU General Public License v3.0
1.44k stars 67 forks source link

Bad Request error is not handled #350

Closed miikanissi closed 1 month ago

miikanissi commented 2 months ago

When submitting an input to Copilot Chat which receives a Bad Request error this will cause the plugin to crash with the following:

[CopilotChat.nvim] }
Error executing luv callback:
...e/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/context.lua:251: attempt to index local 'query' (a nil value)
stack traceback:
        ...e/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/context.lua:251: in function 'on_done'
        ...e/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/copilot.lua:588: in function 'on_done'
        ...are/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/utils.lua:54: in function 'done'
        ...are/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/utils.lua:60: in function 'resolve'
        ...e/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/copilot.lua:551: in function '_user_on_exit'
        .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:241: in function '_shutdown'
        .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:48: in function <.../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:39>

Checking the CopilotChat logs I can see this:

[ERROR Sun 02 Jun 2024 12:53:35 AM EDT] /home/m/.local/share/nvim/lazy/CopilotChat.nvim/lua/CopilotChat/copilot.lua:550: Failed to get response: {
  body = "Bad Request",
  exit = 0,
  headers = { "content-security-policy: default-src 'none'; sandbox", "content-type: text/plain; charset=utf-8", "strict-transport-security: max-age=31536000", "x-content-type-options: nosniff", "date: Sun, 02 Jun 2024 04:53:35 GMT", "content-length: 12", "x-github-backend: Kubernetes", "x-github-request-id: <id removed>", "", "" },
  status = 400
}

I believe the "Bad Request" error from Copilot was caused by the query being too long. Regardless, I think the plugin should gracefully exit in this case and display an error instead of crashing.

gptlang commented 1 month ago

The reason is because we're checking errors with if err or vim.startswith(line, '{"error"') then. When status != 200 and nothing is returned, it panics. The optimal solution would be to check for status but it's not available to stream, only callback.