From 3506cb13114b9f5ad4941969ba6b29ffae2a38ec Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sat, 1 Jul 2023 17:08:47 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(chatModal):=20refactor=20err?= =?UTF-8?q?or=20handling=20in=20WebSocket=20connection=20to=20improve=20re?= =?UTF-8?q?adability=20and=20error=20handling=20The=20error=20handling=20i?= =?UTF-8?q?n=20the=20WebSocket=20connection=20has=20been=20refactored=20to?= =?UTF-8?q?=20improve=20readability=20and=20error=20handling.=20Instead=20?= =?UTF-8?q?of=20nesting=20the=20`getHealth()`=20promise=20inside=20the=20`?= =?UTF-8?q?onerror`=20callback,=20it=20has=20been=20extracted=20to=20a=20s?= =?UTF-8?q?eparate=20`.then()`=20block.=20This=20allows=20for=20better=20s?= =?UTF-8?q?eparation=20of=20concerns=20and=20makes=20the=20code=20more=20r?= =?UTF-8?q?eadable.=20Additionally,=20the=20error=20handling=20logic=20has?= =?UTF-8?q?=20been=20updated=20to=20properly=20set=20the=20`setErrorData`?= =?UTF-8?q?=20state=20when=20the=20backend=20fails=20to=20respond.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/modals/chatModal/index.tsx | 32 +++++++++++---------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/frontend/src/modals/chatModal/index.tsx b/src/frontend/src/modals/chatModal/index.tsx index c39ea0294..bb6efbb70 100644 --- a/src/frontend/src/modals/chatModal/index.tsx +++ b/src/frontend/src/modals/chatModal/index.tsx @@ -205,25 +205,27 @@ export default function ChatModal({ handleOnClose(event); }; newWs.onerror = (ev) => { - getHealth().then((res) => { - if (res.status === 200) { - connectWS(); - } - }).catch((err) => { - setErrorData({ - // message when the backend failed - title: "The backend is not responding. Please try again later.", - // possible solution list - list: [ - "Check your internet connection.", - "Check if the backend is running." - ], + getHealth() + .then((res) => { + if (res.status === 200) { + connectWS(); + } + }) + .catch((err) => { + setErrorData({ + // message when the backend failed + title: "The backend is not responding. Please try again later.", + // possible solution list + list: [ + "Check your internet connection.", + "Check if the backend is running.", + ], + }); }); - }) }; ws.current = newWs; } catch (error) { - connectWS(); + connectWS(); console.log(error); } }