🐛 fix(chatModal): refactor error handling in WebSocket connection to improve readability and error handling

The error handling in the WebSocket connection has been refactored to improve readability and error handling. Instead of nesting the `getHealth()` promise inside the `onerror` callback, it has been extracted to a separate `.then()` block. This allows for better separation of concerns and makes the code more readable. Additionally, the error handling logic has been updated to properly set the `setErrorData` state when the backend fails to respond.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-01 17:08:47 -03:00
commit 3506cb1311

View file

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