More descriptive error codes
suggest changeIf geolocation fails, the error callback receives a GeolocationPositionError (the older name was PositionError). code is 1, 2, or 3. Those values also exist as PERMISSION_DENIED, POSITION_UNAVAILABLE, and TIMEOUT on the error object. Pass that object to getErrorCode. Passing only the number does not work, because the function reads err.code and err.PERMISSION_DENIED.
const getErrorCode = function(err) {
switch (err.code) {
case err.PERMISSION_DENIED:
return "PERMISSION_DENIED";
case err.POSITION_UNAVAILABLE:
return "POSITION_UNAVAILABLE";
case err.TIMEOUT:
return "TIMEOUT";
default:
return "UNKNOWN_ERROR";
}
};
It can be used in geolocationFailure() like so:
const geolocationFailure = function(err) {
console.log("ERROR (" + getErrorCode(err) + "): " + err.message);
};
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents