Improve error handling in gui

This commit is contained in:
Heiner Lohaus 2023-12-10 22:10:56 +01:00
parent bf41cfc5d1
commit c7307030a9
2 changed files with 45 additions and 59 deletions

View File

@ -65,7 +65,7 @@ const remove_cancel_button = async () => {
const ask_gpt = async () => { const ask_gpt = async () => {
regenerate.classList.add(`regenerate-hidden`); regenerate.classList.add(`regenerate-hidden`);
messages = await get_messages(window.conversation_id); messages = await get_messages(window.conversation_id);
try {
window.scrollTo(0, 0); window.scrollTo(0, 0);
window.controller = new AbortController(); window.controller = new AbortController();
@ -95,9 +95,7 @@ const ask_gpt = async () => {
message_box.scrollTop = message_box.scrollHeight; message_box.scrollTop = message_box.scrollHeight;
window.scrollTo(0, 0); window.scrollTo(0, 0);
await new Promise((r) => setTimeout(r, 1000)); try {
window.scrollTo(0, 0);
const response = await fetch(`/backend-api/v2/conversation`, { const response = await fetch(`/backend-api/v2/conversation`, {
method: `POST`, method: `POST`,
signal: window.controller.signal, signal: window.controller.signal,
@ -122,8 +120,10 @@ const ask_gpt = async () => {
}), }),
}); });
const reader = response.body.getReader(); await new Promise((r) => setTimeout(r, 1000));
window.scrollTo(0, 0);
const reader = response.body.getReader();
while (true) { while (true) {
const { value, done } = await reader.read(); const { value, done } = await reader.read();
if (done) break; if (done) break;
@ -142,43 +142,29 @@ const ask_gpt = async () => {
} }
if (text.includes(`G4F_ERROR`)) { if (text.includes(`G4F_ERROR`)) {
console.log("response", text);
document.getElementById(`gpt_${window.token}`).innerHTML = "An error occured, please try again, if the problem persists, please use a other model or provider"; document.getElementById(`gpt_${window.token}`).innerHTML = "An error occured, please try again, if the problem persists, please use a other model or provider";
} }
add_message(window.conversation_id, "assistant", text);
message_box.scrollTop = message_box.scrollHeight;
await remove_cancel_button();
prompt_lock = false;
await load_conversations(20, 0);
window.scrollTo(0, 0);
} catch (e) { } catch (e) {
message_box.scrollTop = message_box.scrollHeight;
await remove_cancel_button();
prompt_lock = false;
await load_conversations(20, 0);
console.log(e); console.log(e);
let cursorDiv = document.getElementById(`cursor`); let cursorDiv = document.getElementById(`cursor`);
if (cursorDiv) cursorDiv.parentNode.removeChild(cursorDiv); if (cursorDiv) cursorDiv.parentNode.removeChild(cursorDiv);
if (e.name != `AbortError`) { if (e.name != `AbortError`) {
let error_message = `oops ! something went wrong, please try again / reload. [stacktrace in console]`; text = `oops ! something went wrong, please try again / reload. [stacktrace in console]`;
document.getElementById(`gpt_${window.token}`).innerHTML = text;
document.getElementById(`gpt_${window.token}`).innerHTML = error_message;
add_message(window.conversation_id, "assistant", error_message);
} else { } else {
document.getElementById(`gpt_${window.token}`).innerHTML += ` [aborted]`; document.getElementById(`gpt_${window.token}`).innerHTML += ` [aborted]`;
add_message(window.conversation_id, "assistant", text + ` [aborted]`); text += ` [aborted]`
} }
}
add_message(window.conversation_id, "assistant", text);
message_box.scrollTop = message_box.scrollHeight;
await remove_cancel_button();
prompt_lock = false;
window.scrollTo(0, 0); window.scrollTo(0, 0);
} await load_conversations(20, 0);
regenerate.classList.remove(`regenerate-hidden`); regenerate.classList.remove(`regenerate-hidden`);
}; };

View File

@ -74,6 +74,7 @@ class Backend_Api:
provider = g4f.Provider.ProviderUtils.convert.get(provider) provider = g4f.Provider.ProviderUtils.convert.get(provider)
def try_response(): def try_response():
try:
response = g4f.ChatCompletion.create( response = g4f.ChatCompletion.create(
model=model, model=model,
provider=provider, provider=provider,
@ -81,7 +82,6 @@ class Backend_Api:
stream=True, stream=True,
ignore_stream_and_auth=True ignore_stream_and_auth=True
) )
try:
yield from response yield from response
except Exception as e: except Exception as e:
print(e) print(e)