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: catch errors on capabilities re-fetching #270

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
55 changes: 53 additions & 2 deletions src/app/appData.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,58 @@ export async function refetchAppData(appData, persist = false) {
*/
export async function refetchAppDataIfDirty(appData) {
// Re-fetch on dirty Talk hash and any desktop client upgrade
if (appData.talkHashDirty || packageJson.version !== appData.version.desktop) {
return await refetchAppData(appData, true)
if (!appData.talkHashDirty && packageJson.version === appData.version.desktop) {
return
}

await new Promise((resolve) => {
/**
* Try to re-fetch appData
*
* @return {Promise<boolean>} true if re-fetch finished and should not be retried
*/
async function doRefetch() {
try {
await refetchAppData(appData, true)
console.debug('AppData re-fetched')
return true
} catch (error) {
// In development mode unauthenticated response will be ERR_NETWORK due to CORS error
if (error.response?.status === 401 || process.env.NODE_ENV === 'development') {
appData.reset().persist()
console.debug('AppData credentials are invalid... Resetting')
return true
}
// Network error, maintenance, service unavailable etc.
// Let's try again later
console.debug(`Cannot get AppData... Error: ${error.code}. Response status: ${error.response?.status || 'unknown'}`)
return false
}
}

/**
* Recursively re-try a re-fetch attempt with a timeout
*
* @param {number} [delay=1000] delay in milliseconds
*/
function retryRefetch(delay = 1_000) {
const MAX_DELAY = 2 ** 7 * 1000 // 128_000 = ~2 minutes

console.debug(`Retry in ${delay} ms...`)
setTimeout(async () => {
if (await doRefetch()) {
return resolve()
}
retryRefetch(delay < MAX_DELAY ? delay * 2 : delay)
}, delay)
}

doRefetch()
.then((success) => {
if (success) {
return resolve()
}
retryRefetch()
})
})
}
Loading