admin管理员组文章数量:1305409
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 "".
*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')
}
}
}
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/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')
}
}
}
Share
Improve this question
edited Jun 9, 2017 at 16:25
Felix Kling
817k181 gold badges1.1k silver badges1.2k bronze badges
asked Jun 8, 2017 at 6:12
RobRob
7,1184 gold badges48 silver badges53 bronze badges
2
-
i guess PositionError is not known to the browser (but "anonymously" returned by getCUrrentPosition). You could instead check
Object.prototype.toString.call(error) === '[object PositionError]'
. – Fabian Commented Jun 8, 2017 at 7:17 -
2
async/await
is part of ES2017, not ES7. – Felix Kling Commented Jun 9, 2017 at 16:25
1 Answer
Reset to default 8The 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 ments, 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 remend 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
本文标签: javascriptCatch Geolocation ErrorAsync AwaitStack Overflow
版权声明:本文标题:javascript - Catch Geolocation Error - Async Await - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801921a2398273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论