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