handle unknown server error by showing an error message to the user

This commit is contained in:
Abi Raja 2024-04-10 13:37:44 -04:00
parent 29cea327cc
commit 483d1e2209
2 changed files with 11 additions and 2 deletions

View File

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

View File

@ -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();