From 29cea327cc4d16b09919cd9335f089b9a62d6883 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Wed, 10 Apr 2024 13:20:42 -0400 Subject: [PATCH] improve error UX (particular when no OpenAI API key is found) --- backend/routes/generate_code.py | 12 +++++------- backend/ws/__init__.py | 0 backend/ws/constants.py | 2 ++ frontend/src/generateCode.ts | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 backend/ws/__init__.py create mode 100644 backend/ws/constants.py diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py index d9ea660..259983f 100644 --- a/backend/routes/generate_code.py +++ b/backend/routes/generate_code.py @@ -23,7 +23,8 @@ from prompts.claude_prompts import VIDEO_PROMPT from prompts.types import Stack # from utils import pprint_prompt -from video.utils import extract_tag_content, assemble_claude_prompt_video # type: ignore +from video.utils import extract_tag_content, assemble_claude_prompt_video +from ws.constants import APP_ERROR_WEB_SOCKET_CODE # type: ignore router = APIRouter() @@ -58,7 +59,7 @@ async def stream_code(websocket: WebSocket): message: str, ): await websocket.send_json({"type": "error", "value": message}) - await websocket.close() + await websocket.close(APP_ERROR_WEB_SOCKET_CODE) # TODO: Are the values always strings? params: Dict[str, str] = await websocket.receive_json() @@ -121,11 +122,8 @@ async def stream_code(websocket: WebSocket): if not openai_api_key and code_generation_model == "gpt_4_vision": print("OpenAI API key not found") - await websocket.send_json( - { - "type": "error", - "value": "No OpenAI API key found. Please add your API key in the settings dialog or add it to backend/.env file. If you add it to .env, make sure to restart the backend server.", - } + await throw_error( + "No OpenAI API key found. Please add your API key in the settings dialog or add it to backend/.env file. If you add it to .env, make sure to restart the backend server." ) return diff --git a/backend/ws/__init__.py b/backend/ws/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/ws/constants.py b/backend/ws/constants.py new file mode 100644 index 0000000..e992ecb --- /dev/null +++ b/backend/ws/constants.py @@ -0,0 +1,2 @@ +# WebSocket protocol (RFC 6455) allows for the use of custom close codes in the range 4000-4999 +APP_ERROR_WEB_SOCKET_CODE = 4332 diff --git a/frontend/src/generateCode.ts b/frontend/src/generateCode.ts index 55523c4..fc408bc 100644 --- a/frontend/src/generateCode.ts +++ b/frontend/src/generateCode.ts @@ -48,7 +48,7 @@ export function generateCode( onCancel(); } else if (event.code !== 1000) { console.error("WebSocket error code", event); - toast.error(ERROR_MESSAGE); + onCancel(); } else { onComplete(); }