Catch errors on geolocation request, reduce Sentry logs (#9098)
This commit is contained in:
@@ -1,10 +1,45 @@
|
|||||||
import {useEffect, useRef} from 'react'
|
import {useEffect, useRef} from 'react'
|
||||||
import * as Location from 'expo-location'
|
import * as Location from 'expo-location'
|
||||||
|
import {createPermissionHook} from 'expo-modules-core'
|
||||||
|
|
||||||
import {logger} from '#/state/geolocation/logger'
|
import {logger} from '#/state/geolocation/logger'
|
||||||
import {getDeviceGeolocation} from '#/state/geolocation/util'
|
import {getDeviceGeolocation} from '#/state/geolocation/util'
|
||||||
import {device, useStorage} from '#/storage'
|
import {device, useStorage} from '#/storage'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Location.useForegroundPermissions on web just errors if the navigator.permissions API is not available.
|
||||||
|
* We need to catch and ignore it, since it's effectively denied.
|
||||||
|
* @see https://github.com/expo/expo/blob/72f1562ed9cce5ff6dfe04aa415b71632a3d4b87/packages/expo-location/src/Location.ts#L290-L293
|
||||||
|
*/
|
||||||
|
const useForegroundPermissions = createPermissionHook({
|
||||||
|
getMethod: () =>
|
||||||
|
Location.getForegroundPermissionsAsync().catch(error => {
|
||||||
|
logger.debug(
|
||||||
|
'useForegroundPermission: error getting location permissions',
|
||||||
|
{safeMessage: error},
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
status: Location.PermissionStatus.DENIED,
|
||||||
|
granted: false,
|
||||||
|
canAskAgain: false,
|
||||||
|
expires: 0,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
requestMethod: () =>
|
||||||
|
Location.requestForegroundPermissionsAsync().catch(error => {
|
||||||
|
logger.debug(
|
||||||
|
'useForegroundPermission: error requesting location permissions',
|
||||||
|
{safeMessage: error},
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
status: Location.PermissionStatus.DENIED,
|
||||||
|
granted: false,
|
||||||
|
canAskAgain: false,
|
||||||
|
expires: 0,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to get and sync the device geolocation from the device GPS and store it
|
* Hook to get and sync the device geolocation from the device GPS and store it
|
||||||
* using device storage. If permissions are not granted, it will clear any cached
|
* using device storage. If permissions are not granted, it will clear any cached
|
||||||
@@ -12,7 +47,7 @@ import {device, useStorage} from '#/storage'
|
|||||||
*/
|
*/
|
||||||
export function useSyncedDeviceGeolocation() {
|
export function useSyncedDeviceGeolocation() {
|
||||||
const synced = useRef(false)
|
const synced = useRef(false)
|
||||||
const [status] = Location.useForegroundPermissions()
|
const [status] = useForegroundPermissions()
|
||||||
const [deviceGeolocation, setDeviceGeolocation] = useStorage(device, [
|
const [deviceGeolocation, setDeviceGeolocation] = useStorage(device, [
|
||||||
'deviceGeolocation',
|
'deviceGeolocation',
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in New Issue
Block a user