From d3dbb9468949e5122a18c1e07e28fe76cbeb5d36 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 29 Sep 2025 23:12:50 +0300 Subject: [PATCH] Catch errors on geolocation request, reduce Sentry logs (#9098) --- .../geolocation/useSyncedDeviceGeolocation.ts | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/state/geolocation/useSyncedDeviceGeolocation.ts b/src/state/geolocation/useSyncedDeviceGeolocation.ts index 602f29a30d..fea6198d46 100644 --- a/src/state/geolocation/useSyncedDeviceGeolocation.ts +++ b/src/state/geolocation/useSyncedDeviceGeolocation.ts @@ -1,10 +1,45 @@ import {useEffect, useRef} from 'react' import * as Location from 'expo-location' +import {createPermissionHook} from 'expo-modules-core' import {logger} from '#/state/geolocation/logger' import {getDeviceGeolocation} from '#/state/geolocation/util' 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 * 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() { const synced = useRef(false) - const [status] = Location.useForegroundPermissions() + const [status] = useForegroundPermissions() const [deviceGeolocation, setDeviceGeolocation] = useStorage(device, [ 'deviceGeolocation', ])