diff options
| author | Martin Bielik <martin.bielik@instea.sk> | 2024-11-08 22:21:35 +0100 |
|---|---|---|
| committer | Martin Bielik <martin.bielik@instea.sk> | 2024-11-08 22:21:35 +0100 |
| commit | 1d476136a0eb716171608290c8d189cdb79bcfa2 (patch) | |
| tree | a8816cf924290b9de4d9bdb37cbe258adc857619 | |
| parent | 758be522e6d765eeb78ce7681f4b39e3b05043b8 (diff) | |
| download | vim-ai-1d476136a0eb716171608290c8d189cdb79bcfa2.tar.gz | |
improved error handling, fixes #126
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 3401f4b..9778f1a 100644 --- a/py/utils.py +++ b/py/utils.py @@ -231,28 +231,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: |