Skip to content

Commit

Permalink
made it so it will retry on server error
Browse files Browse the repository at this point in the history
  • Loading branch information
EnderPoint07 committed Oct 22, 2023
1 parent 1e894eb commit 0c49508
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function removeTypingIndicator() {

function sendUserMessageToAI(userMessage) {
const apiUrl = 'https://gpt4free.dotm38.repl.co/custom/api/conversation';

var botResponse;
fetch(apiUrl, {
method: 'POST',
headers: {
Expand All @@ -48,12 +48,17 @@ function sendUserMessageToAI(userMessage) {
}
})
})
.then(response => response.text())
.then(response => {if (response.ok) return response.text()}) // if the response code is 200 then only continue
.then(text => {
const botResponse = text
console.log(text)
removeTypingIndicator();
appendBotMessage(botResponse);
if (text !== undefined) { // if its not 200 then text will be undefined
botResponse = text
console.log(text)
removeTypingIndicator();
appendBotMessage(botResponse);
}else { // when text is undefined i.e server has error, we just retry the request
console.log("SERVER ERROR, RETRYING REQUEST")
sendUserMessageToAI(userMessage)
}
})
.catch(error => {
console.error('Error:', error);
Expand Down

0 comments on commit 0c49508

Please sign in to comment.