Skip to content

Commit 37651d1

Browse files
committed
Replace Platform.OS with platform-specific files for getCurrentPosition()
1 parent d335917 commit 37651d1

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type {GeolocationErrorCodeType} from '@libs/getCurrentPosition/getCurrentPosition.types';
2+
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types';
3+
4+
function getGeolocationError(error: unknown): {code: GeolocationErrorCodeType; message: string} {
5+
let message = 'Geolocation call failed';
6+
7+
if (error instanceof Error) {
8+
message = error.message;
9+
} else if (typeof error === 'string') {
10+
message = error;
11+
}
12+
13+
return {code: GeolocationErrorCode.POSITION_UNAVAILABLE, message};
14+
}
15+
16+
export default getGeolocationError;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type {GeolocationErrorCodeType} from '@libs/getCurrentPosition/getCurrentPosition.types';
2+
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types';
3+
4+
function getGeolocationError(error: unknown): {code: GeolocationErrorCodeType; message: string} {
5+
let message = 'Geolocation call failed';
6+
let code = GeolocationErrorCode.POSITION_UNAVAILABLE;
7+
8+
if (error instanceof GeolocationPositionError) {
9+
code = error.code;
10+
message = error.message;
11+
} else if (error instanceof Error) {
12+
message = error.message;
13+
} else if (typeof error === 'string') {
14+
message = error;
15+
}
16+
17+
return {code, message};
18+
}
19+
20+
export default getGeolocationError;

src/libs/getCurrentPosition/index.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {getCurrentPositionAsync, PermissionStatus, requestForegroundPermissionsAsync} from 'expo-location';
22
import type {PermissionResponse} from 'expo-location';
3-
import {Platform} from 'react-native';
43
import {GeolocationErrorCode} from './getCurrentPosition.types';
54
import type {GetCurrentPosition} from './getCurrentPosition.types';
5+
import getGeolocationError from './getGeolocationError';
66

77
const getCurrentPosition: GetCurrentPosition = async (success, error, options) => {
88
const foregroundPermissionResponse: PermissionResponse = await requestForegroundPermissionsAsync();
@@ -16,19 +16,8 @@ const getCurrentPosition: GetCurrentPosition = async (success, error, options) =
1616
const currentPosition = await getCurrentPositionAsync(options);
1717
success(currentPosition);
1818
} catch (caughtError) {
19-
let message = 'Geolocation call failed';
20-
let code = GeolocationErrorCode.POSITION_UNAVAILABLE;
21-
22-
if (Platform.OS === 'web' && caughtError instanceof GeolocationPositionError) {
23-
code = caughtError.code;
24-
message = caughtError.message;
25-
} else if (caughtError instanceof Error) {
26-
message = caughtError.message;
27-
} else if (typeof caughtError === 'string') {
28-
message = caughtError;
29-
}
30-
31-
error({code, message});
19+
const geolocationError = getGeolocationError(caughtError);
20+
error(geolocationError);
3221
}
3322
};
3423

0 commit comments

Comments
 (0)