Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch Geolocation Error - Async Await

How can I catch the geolocation specific error to notify the user that they must have geolocation turned on?

The catch logs an error called PositionError as referenced here in the Mozilla docs "https://developer.mozilla.org/en-US/docs/Web/API/PositionError".

*Note: my code does not catch the error, it simply displays:

Uncaught (in promise) ReferenceError: PositionError is not defined

Code

getCurrentLocation() {
    return new Promise((resolve, reject) => {
        navigator.geolocation.getCurrentPosition(resolve, reject, {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
        });
    });
},
async inout() {
    try {
        let location = await this.getCurrentLocation();
        let response = await axios.post(API.URL, {});
    } catch (e) {
        if(e instanceof PositionError) {
            console.log('position error')
        }
    }
}
like image 367
Rob Avatar asked Jun 08 '17 06:06

Rob


1 Answers

The getCurrentPosition() API was poorly designed, assuming users would test errors immediately in the callback, instead of passing them up.

Since PositionError has no public constructor, window.PositionError is not defined.

As Fabian mentions in comments, you can test for the error like this:

if (e.toString() == '[object PositionError]') {
  console.log('position error')
}

or use his version if you're calling any API likely to throw non-object errors (hopefully rare).

However, instead of littering your code, I recommend throwing a better error from your new async getCurrentLocation() API instead (use fiddle to get around SO code snippet sandbox):

function getCurrentLocation(options) {
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition(resolve, ({code, message}) =>
      reject(Object.assign(new Error(message), {name: "PositionError", code})),
      options);
    });
};
async function inout() {
  try {
    console.log(await this.getCurrentLocation({
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    }));
  } catch (e) {
    if (e.name == 'PositionError') {
      console.log(e.message + ". code = " + e.code);
    }
  }
}
inout().catch(e => console.log(e)); // User denied geolocation prompt. code = 1
like image 138
jib Avatar answered Nov 16 '22 02:11

jib