5
This commit is contained in:
parent
1201afcfd8
commit
199e46cb75
18
chatbot.php
18
chatbot.php
@ -55,12 +55,26 @@
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ message: messageText })
|
body: JSON.stringify({ message: messageText })
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
const botResponse = data.reply || 'Sorry, I could not understand.';
|
const botResponse = data.reply || 'Sorry, I could not understand.';
|
||||||
appendMessage(botResponse, 'bot');
|
appendMessage(botResponse, 'bot');
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error:', error);
|
console.error('Fetch error:', error);
|
||||||
appendMessage('Sorry, something went wrong.', 'bot');
|
let errorMessage = 'Sorry, something went wrong. Please try again later.';
|
||||||
|
if (error instanceof TypeError) { // Network error
|
||||||
|
errorMessage = 'Could not connect to the server. Please check your network connection.';
|
||||||
|
} else if (error instanceof SyntaxError) { // JSON parsing error
|
||||||
|
errorMessage = 'Received an invalid response from the server.';
|
||||||
|
} else if (error.message.startsWith('HTTP error!')) {
|
||||||
|
errorMessage = 'There was a problem communicating with the server.';
|
||||||
|
}
|
||||||
|
appendMessage(errorMessage, 'bot');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user