improve error UX (particular when no OpenAI API key is found)

This commit is contained in:
Abi Raja 2024-04-10 13:20:42 -04:00
parent 4ef26a6ae8
commit 29cea327cc
4 changed files with 8 additions and 8 deletions

View File

@ -23,7 +23,8 @@ from prompts.claude_prompts import VIDEO_PROMPT
from prompts.types import Stack from prompts.types import Stack
# from utils import pprint_prompt # 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() router = APIRouter()
@ -58,7 +59,7 @@ async def stream_code(websocket: WebSocket):
message: str, message: str,
): ):
await websocket.send_json({"type": "error", "value": message}) 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? # TODO: Are the values always strings?
params: Dict[str, str] = await websocket.receive_json() 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": if not openai_api_key and code_generation_model == "gpt_4_vision":
print("OpenAI API key not found") print("OpenAI API key not found")
await websocket.send_json( 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."
"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.",
}
) )
return return

0
backend/ws/__init__.py Normal file
View File

2
backend/ws/constants.py Normal file
View File

@ -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

View File

@ -48,7 +48,7 @@ export function generateCode(
onCancel(); onCancel();
} else if (event.code !== 1000) { } else if (event.code !== 1000) {
console.error("WebSocket error code", event); console.error("WebSocket error code", event);
toast.error(ERROR_MESSAGE); onCancel();
} else { } else {
onComplete(); onComplete();
} }