🐛 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:
parent
e5eea2d9fb
commit
3506cb1311
1 changed files with 17 additions and 15 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue