Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

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

Abi Raja29cea327cc4d
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index d9ea660d..259983fe 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 00000000..e69de29b
diff --git a/backend/ws/constants.py b/backend/ws/constants.py
new file mode 100644
index 00000000..e992ecbd
--- /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 55523c49..fc408bc8 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();
}