diff options
| author | Martin Bielik <martin.bielik@instea.sk> | 2024-12-03 21:39:43 +0100 |
|---|---|---|
| committer | Martin Bielik <martin.bielik@instea.sk> | 2024-12-03 21:39:43 +0100 |
| commit | ce66bb43831dfe2d32b578d6abf84976a3c4ce0e (patch) | |
| tree | 964639caa959601741b0ca095963fb3609661737 | |
| parent | cefa3c1fa9ed73829b57f66829dd3138c315e0fa (diff) | |
| parent | 1d476136a0eb716171608290c8d189cdb79bcfa2 (diff) | |
| download | vim-ai-ce66bb43831dfe2d32b578d6abf84976a3c4ce0e.tar.gz | |
Merge branch 'main' into support-non-streaming
Diffstat (limited to '')
| -rw-r--r-- | py/utils.py | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/py/utils.py b/py/utils.py index 381ba75..c54f2e5 100644 --- a/py/utils.py +++ b/py/utils.py @@ -237,28 +237,33 @@ def openai_request(url, data, options): def print_info_message(msg): vim.command("redraw") - vim.command(r'call feedkeys("\<Esc>")') vim.command("echohl ErrorMsg") vim.command(f"echomsg '{msg}'") vim.command("echohl None") +def parse_error_message(error): + try: + parsed = json.loads(error.read().decode()) + return parsed["error"]["message"] + except: + pass + def handle_completion_error(error): # nvim throws - pynvim.api.common.NvimError: Keyboard interrupt is_nvim_keyboard_interrupt = "Keyboard interrupt" in str(error) if isinstance(error, KeyboardInterrupt) or is_nvim_keyboard_interrupt: print_info_message("Completion cancelled...") - elif isinstance(error, URLError) and isinstance(error.reason, socket.timeout): - print_info_message("Request timeout...") elif isinstance(error, HTTPError): status_code = error.getcode() + error_message = parse_error_message(error) msg = f"OpenAI: HTTPError {status_code}" - if status_code == 401: - msg += ' (Hint: verify that your API key is valid)' - if status_code == 404: - msg += ' (Hint: verify that you have access to the OpenAI API and to the model)' - elif status_code == 429: - msg += ' (Hint: verify that your billing plan is "Pay as you go")' + if error_message: + msg += f": {error_message}" print_info_message(msg) + elif isinstance(error, URLError) and isinstance(error.reason, socket.timeout): + print_info_message("Request timeout...") + elif isinstance(error, URLError): + print_info_message(f"URLError: {error.reason}") elif isinstance(error, KnownError): print_info_message(str(error)) else: |