fix sse error handling if shop api error part 2

This commit is contained in:
team2
2026-04-25 20:21:59 +02:00
parent cf970d2b49
commit 2044a465ad
3 changed files with 37 additions and 3 deletions

View File

@@ -386,12 +386,24 @@ document.addEventListener('DOMContentLoaded', () => {
const source = new EventSource(`/ask-sse/${encodeURIComponent(jobId)}`);
state.eventSource = source;
let networkErrorTimer = null;
const clearNetworkErrorTimer = () => {
if (!networkErrorTimer) {
return;
}
clearTimeout(networkErrorTimer);
networkErrorTimer = null;
};
const complete = () => {
if (finished) {
return;
}
finished = true;
clearNetworkErrorTimer();
finishEventStream();
resolve();
};
@@ -402,6 +414,7 @@ document.addEventListener('DOMContentLoaded', () => {
}
finished = true;
clearNetworkErrorTimer();
finishEventStream();
reject(err);
};
@@ -409,12 +422,18 @@ document.addEventListener('DOMContentLoaded', () => {
state.completeStream = complete;
state.failStream = fail;
source.onopen = () => {
clearNetworkErrorTimer();
};
source.onmessage = (event) => {
if (state.abortRequested || finished) {
complete();
return;
}
clearNetworkErrorTimer();
if (event.data === undefined || event.data === null || event.data === '') {
return;
}
@@ -442,7 +461,18 @@ document.addEventListener('DOMContentLoaded', () => {
return;
}
fail(new Error('EventSource connection error'));
if (source.readyState === EventSource.CLOSED) {
fail(new Error('EventSource connection closed'));
return;
}
if (!networkErrorTimer) {
networkErrorTimer = setTimeout(() => {
if (!finished && !state.abortRequested) {
fail(new Error('EventSource connection error'));
}
}, 20000);
}
});
});
} catch (err) {