summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Bielik <mx.bielik@gmail.com>2023-04-15 12:58:24 +0200
committerMartin Bielik <mx.bielik@gmail.com>2023-04-15 12:58:24 +0200
commit4bb95955c5ee1e4d5f7aaeec549b4b865d356773 (patch)
tree60a29f6da00d45da27aa990d9384236eb425ddf3
parentf69e39a0406999b6460295aa9f850b0444769080 (diff)
downloadvim-ai-4bb95955c5ee1e4d5f7aaeec549b4b865d356773.tar.gz
reusing error handler
-rw-r--r--py/chat.py9
-rw-r--r--py/complete.py9
-rw-r--r--py/utils.py9
3 files changed, 13 insertions, 14 deletions
diff --git a/py/chat.py b/py/chat.py
index 547c849..7243988 100644
--- a/py/chat.py
+++ b/py/chat.py
@@ -63,10 +63,5 @@ try:
vim.command("normal! a\n\n>>> user\n\n")
vim.command("redraw")
-except KeyboardInterrupt:
- vim.command("normal! a Ctrl-C...")
-except URLError as error:
- if isinstance(error.reason, socket.timeout):
- vim.command("normal! aRequest timeout...")
- else:
- raise error
+except BaseException as error:
+ handle_completion_error(error)
diff --git a/py/complete.py b/py/complete.py
index 1045720..9929cf7 100644
--- a/py/complete.py
+++ b/py/complete.py
@@ -49,10 +49,5 @@ try:
vim.command("redraw")
text_chunks = engines[engine](prompt)
render_text_chunks(text_chunks)
-except KeyboardInterrupt:
- vim.command("normal! a Ctrl-C...")
-except URLError as error:
- if isinstance(error.reason, socket.timeout):
- vim.command("normal! aRequest timeout...")
- else:
- raise error
+except BaseException as error:
+ handle_completion_error(error)
diff --git a/py/utils.py b/py/utils.py
index bd69401..2e0fc2d 100644
--- a/py/utils.py
+++ b/py/utils.py
@@ -125,3 +125,12 @@ def openai_request(url, data, options):
else:
openai_obj = json.loads(line_data)
yield openai_obj
+
+def handle_completion_error(error):
+ if isinstance(error, KeyboardInterrupt):
+ vim.command("normal! a Ctrl-C...")
+ if isinstance(error, URLError):
+ if isinstance(error.reason, socket.timeout):
+ vim.command("normal! aRequest timeout...")
+ else:
+ raise error