fix: handle preflight exceptions in tryCatch

This commit is contained in:
Ben Allfree 2023-06-18 05:55:52 -07:00
parent d623ed373a
commit 4797706605

View File

@ -1,5 +1,6 @@
import { Logger, safeCatch } from '@pockethost/common' import { Logger, safeCatch } from '@pockethost/common'
const TRYFETCH_RETRY_MS = 50
export const tryFetch = (logger: Logger) => export const tryFetch = (logger: Logger) =>
safeCatch( safeCatch(
`tryFetch`, `tryFetch`,
@ -10,9 +11,14 @@ export const tryFetch = (logger: Logger) =>
const tryFetch = async () => { const tryFetch = async () => {
if (preflight) { if (preflight) {
dbg(`Checking preflight`) dbg(`Checking preflight`)
try {
const shouldFetch = await preflight() const shouldFetch = await preflight()
if (!shouldFetch) { if (!shouldFetch) {
reject(new Error(`tryFetch failed preflight, aborting`)) reject(new Error(`failed preflight, aborting`))
return
}
} catch (e) {
reject(new Error(`preflight threw error, aborting`))
return return
} }
} }
@ -22,8 +28,10 @@ export const tryFetch = (logger: Logger) =>
dbg(`Fetch ${url} successful`) dbg(`Fetch ${url} successful`)
resolve() resolve()
} catch (e) { } catch (e) {
dbg(`Could not fetch ${url}, trying again in 1s`) dbg(
setTimeout(tryFetch, 1000) `Could not fetch ${url}, trying again in ${TRYFETCH_RETRY_MS}ms. Raw error ${e}`
)
setTimeout(tryFetch, TRYFETCH_RETRY_MS)
} }
} }
tryFetch() tryFetch()