Warning: a promise was rejected with a non-error: [object GeolocationPositionError]

Warning: a promise was rejected with a non-error: [object GeolocationPositionError]

在尝试从用户的浏览器获取地理位置时,如果用户拒绝许可或阻止浏览器共享位置,我们会收到来自 Bluebird 的控制台警告:Warning: a promise was rejected with a non-error: [object GeolocationPositionError]

然而,当我们捕获并记录错误时,我们收到错误消息:User denied geolocation prompt,它来自地理定位 API 的 GeolocationPositionError。我想对于将返回 GeolocationPositionError 的其他两种情况(内部位置错误或超时),也会记录相同的警告。

那么,为什么我们会收到控制台警告,我们如何正确处理它?

这是处理浏览器导航器和地理定位的代码:

import Promise from 'bluebird';

function getUserLocation() {
  return new Promise(function(resolve, reject) {
    if (navigator && navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(resolve, reject);
    } else {
      // Browser does not support geolocation at all
      reject(new Error('Geolocation is unsupported'));
    }
  });
}

阅读 Bluebird 关于其警告消息的文档:"Warning: a promise was rejected with a non-error", I found that the problem was that the GeolocationPositionError 不是 Javascript Error 实例,这正是 Bluebird 所期望的。因此,相反,自定义 reject() 回调以将 GeolocationPositionError 显式转换为 Error 解决了控制台警告和错误处理,适用于任何 GeolocationPositionError 情况。

import Promise from 'bluebird';

export function getUserLocation() {
  return new Promise(function(resolve, reject) {
    if (navigator && navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        resolve,
        (geolocationPositionError) => { // reject
          // Note: must explicitly cast the `GeolocationPositionError` as an Error instance since bluebird explicitly expects a javascript Error object
          // see http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-rejected-with-a-non-error
          // and `GeolocationPositionError` is not an Error instance, see https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError
          return reject(new Error(geolocationPositionError));
        }
      );
    } else {
      // Browser does not support geolocation at all
      reject(new Error('Geolocation is unsupported'));
    }
  });