Category: JS Objects
Updated

How to handle geolocation errors in Appsmith.

Issue

There are a few issues that can occur when building apps that require geolocation. 
1. The user's browser doesn't support geolocation. 
2. The user's location is not turned on. 

Resolution

In both cases, you can have a utility function called checkUserLocation which will handle both cases of the issues highlighted above. The code to handle both cases is as follows: 
 


export default {
    async checkUserLocation() {
        try {
        	// try to get the current user's location
            await appsmith.geolocation.getCurrentPosition()
        }catch(err) {
            // if some error occurs, alert the error code and error message 
            showAlert(`ERROR(${err.message.code}): ${error.message.message}`)
        }
    
    }
}

 

For clarity, the error.message object looks like the following: 
 

Geolocation API error example