Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend): improve network error handling while waiting for results #281

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client-src/shared/poll/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ async function poll(url, prefix) {
}

try {
return PollUntilValid.poll(url, validateFn, 1000, onValid, onProgress, onError);
return await PollUntilValid.poll(url, validateFn, 1000, onValid, onProgress, onError);
} catch (e) {
// Network Error or other reason why the request could not be completed
console.log(e);
handleError(prefix);
handleError(prefix, e);
return null;
}
}
Expand Down
21 changes: 19 additions & 2 deletions client-src/shared/poll/pollUntilValid.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,24 @@ class PollUntilValid {
onError = () => {},
) {
do {
/* eslint-disable no-await-in-loop */
const response = await fetch(url);
let response;
try {
/* eslint-disable no-await-in-loop */
response = await fetch(url);
} catch (error) {
if (error instanceof TypeError) {
// Chrome and Firefox use different Error messages, so it's hard to be more
// specific than checking for TypeError
// see: https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions
console.log("NetworkError, continue retrying", error);
await PollUntilValid.wait(5000);
} else {
throw error;
}
}

// eslint-disable-next-line no-continue
if (!response) continue;

// if response code is not between 200-299
if (!response.ok) {
Expand All @@ -43,6 +59,7 @@ class PollUntilValid {
await onValid(response);
return response;
}

/* eslint-enable */
} while (true);
}
Expand Down