Log geolocation error at most once per session
This commit is contained in:
@@ -48,7 +48,7 @@ async function getGeolocation(): Promise<Device['geolocation']> {
|
||||
/**
|
||||
* Local promise used within this file only.
|
||||
*/
|
||||
let geolocationResolution: Promise<void> | undefined
|
||||
let geolocationResolution: Promise<{success: boolean}> | undefined
|
||||
|
||||
/**
|
||||
* Begin the process of resolving geolocation. This should be called once at
|
||||
@@ -65,12 +65,14 @@ export function beginResolveGeolocation() {
|
||||
* and fail closed.
|
||||
*/
|
||||
if (__DEV__) {
|
||||
geolocationResolution = new Promise(y => y())
|
||||
geolocationResolution = new Promise(y => y({success: true}))
|
||||
device.set(['geolocation'], DEFAULT_GEOLOCATION)
|
||||
return
|
||||
}
|
||||
|
||||
geolocationResolution = new Promise(async resolve => {
|
||||
let success = true
|
||||
|
||||
try {
|
||||
// Try once, fail fast
|
||||
const geolocation = await getGeolocation()
|
||||
@@ -83,7 +85,9 @@ export function beginResolveGeolocation() {
|
||||
throw new Error(`geolocation: nothing returned from initial request`)
|
||||
}
|
||||
} catch (e: any) {
|
||||
logger.error(`geolocation: failed initial request`, {
|
||||
success = false
|
||||
|
||||
logger.debug(`geolocation: failed initial request`, {
|
||||
safeMessage: e.message,
|
||||
})
|
||||
|
||||
@@ -97,6 +101,7 @@ export function beginResolveGeolocation() {
|
||||
device.set(['geolocation'], geolocation)
|
||||
emitGeolocationUpdate(geolocation)
|
||||
logger.debug(`geolocation: success`, {geolocation})
|
||||
success = true
|
||||
} else {
|
||||
// endpoint should throw on all failures, this is insurance
|
||||
throw new Error(`geolocation: nothing returned from retries`)
|
||||
@@ -107,7 +112,7 @@ export function beginResolveGeolocation() {
|
||||
logger.debug(`geolocation: failed retries`, {safeMessage: e.message})
|
||||
})
|
||||
} finally {
|
||||
resolve(undefined)
|
||||
resolve({success})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -127,10 +132,14 @@ export async function ensureGeolocationResolved() {
|
||||
logger.debug(`geolocation: using cache`, {cached})
|
||||
} else {
|
||||
logger.debug(`geolocation: no cache`)
|
||||
await geolocationResolution
|
||||
logger.debug(`geolocation: resolved`, {
|
||||
resolved: device.get(['geolocation']),
|
||||
})
|
||||
const {success} = await geolocationResolution
|
||||
if (success) {
|
||||
logger.debug(`geolocation: resolved`, {
|
||||
resolved: device.get(['geolocation']),
|
||||
})
|
||||
} else {
|
||||
logger.error(`geolocation: failed to resolve`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user