Skip to content

Commit 2792dd9

Browse files
lucavbgithub-actions[bot]
authored andcommitted
Expose all error properties in GeolocateControl error event
(h/t @lucavb) (#13597) GitOrigin-RevId: de663d3c7b6bb37d29e21c8e3d3f5c9577c0ecd7
1 parent e08a5b5 commit 2792dd9

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/ui/control/geolocate_control.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,13 @@ class GeolocateControl extends Evented<GeolocateControlEvents> implements IContr
459459
this._userLocationDotMarker.addClassName('mapboxgl-user-location-dot-stale');
460460
}
461461

462-
this.fire(new Event('error', error));
462+
this.fire(new Event('error', {
463+
code: error.code,
464+
message: error.message,
465+
PERMISSION_DENIED: error.PERMISSION_DENIED,
466+
POSITION_UNAVAILABLE: error.POSITION_UNAVAILABLE,
467+
TIMEOUT: error.TIMEOUT
468+
} as GeolocationPositionError));
463469

464470
this._finish();
465471
}

test/unit/ui/control/geolocate.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ test('GeolocateControl error event', async () => {
6161
});
6262
});
6363

64+
test('GeolocateControl error event includes GeolocationPositionError constants', async () => {
65+
const map = createMap();
66+
const geolocate = new GeolocateControl();
67+
map.addControl(geolocate);
68+
69+
// Directly call _onError with a mock GeolocationPositionError to test
70+
// that PERMISSION_DENIED, POSITION_UNAVAILABLE, and TIMEOUT are passed through.
71+
// The mock-geolocation library doesn't support these constants.
72+
const mockError = {
73+
PERMISSION_DENIED: 1,
74+
POSITION_UNAVAILABLE: 2,
75+
TIMEOUT: 3,
76+
code: 1,
77+
message: 'User denied Geolocation',
78+
} as const satisfies GeolocationPositionError;
79+
80+
await afterUIChanges((resolve) => {
81+
geolocate.on('error', (error) => {
82+
expect(error.code).toEqual(1);
83+
expect(error.message).toEqual('User denied Geolocation');
84+
expect(error.PERMISSION_DENIED).toEqual(1);
85+
expect(error.POSITION_UNAVAILABLE).toEqual(2);
86+
expect(error.TIMEOUT).toEqual(3);
87+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
88+
resolve();
89+
});
90+
geolocate._onError(mockError);
91+
});
92+
});
93+
6494
test('GeolocateControl outofmaxbounds event in active lock state', async () => {
6595
const map = createMap();
6696
const geolocate = new GeolocateControl();

0 commit comments

Comments
 (0)