Spaces:
Running
Running
Rohit Ghosh
commited on
Commit
·
7fce0c8
1
Parent(s):
b45a0d5
error handling
Browse files
script.js
CHANGED
@@ -15,6 +15,18 @@ async function sendMessage() {
|
|
15 |
body: JSON.stringify({ message }),
|
16 |
});
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
const data = await response.json();
|
19 |
displayMessage("Bot: " + data.response, "bot-message");
|
20 |
}
|
|
|
15 |
body: JSON.stringify({ message }),
|
16 |
});
|
17 |
|
18 |
+
// Check if the response is okay
|
19 |
+
try {
|
20 |
+
const jsonResponse = await response.json();
|
21 |
+
document.getElementById(
|
22 |
+
"chatbox"
|
23 |
+
).value += `Bot: ${jsonResponse.response}\n`;
|
24 |
+
} catch (error) {
|
25 |
+
console.error("Failed to parse JSON:", error);
|
26 |
+
document.getElementById(
|
27 |
+
"chatbox"
|
28 |
+
).value += `Bot: Something went wrong. Please try again.\n`;
|
29 |
+
}
|
30 |
const data = await response.json();
|
31 |
displayMessage("Bot: " + data.response, "bot-message");
|
32 |
}
|