From 483d1e2209ad61a1a077601ff9e94d5fc4ae2418 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Wed, 10 Apr 2024 13:37:44 -0400 Subject: [PATCH] handle unknown server error by showing an error message to the user --- frontend/src/constants.ts | 2 ++ frontend/src/generateCode.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/frontend/src/constants.ts b/frontend/src/constants.ts index a5da189..1cc9373 100644 --- a/frontend/src/constants.ts +++ b/frontend/src/constants.ts @@ -1 +1,3 @@ +// WebSocket protocol (RFC 6455) allows for the use of custom close codes in the range 4000-4999 +export const APP_ERROR_WEB_SOCKET_CODE = 4332; export const USER_CLOSE_WEB_SOCKET_CODE = 4333; diff --git a/frontend/src/generateCode.ts b/frontend/src/generateCode.ts index fc408bc..7fc34e7 100644 --- a/frontend/src/generateCode.ts +++ b/frontend/src/generateCode.ts @@ -1,6 +1,9 @@ import toast from "react-hot-toast"; import { WS_BACKEND_URL } from "./config"; -import { USER_CLOSE_WEB_SOCKET_CODE } from "./constants"; +import { + APP_ERROR_WEB_SOCKET_CODE, + USER_CLOSE_WEB_SOCKET_CODE, +} from "./constants"; import { FullGenerationSettings } from "./types"; const ERROR_MESSAGE = @@ -46,8 +49,12 @@ export function generateCode( if (event.code === USER_CLOSE_WEB_SOCKET_CODE) { toast.success(CANCEL_MESSAGE); onCancel(); + } else if (event.code === APP_ERROR_WEB_SOCKET_CODE) { + console.error("Known server error", event); + onCancel(); } else if (event.code !== 1000) { - console.error("WebSocket error code", event); + console.error("Unknown server or connection error", event); + toast.error(ERROR_MESSAGE); onCancel(); } else { onComplete();