从gapi.client.load捕获错误 [英] Catch Error from gapi.client.load

查看:76
本文介绍了从gapi.client.load捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Google App Engine与Java和Google Cloud Endpoints结合使用.在我的JavaScript前端中,我正在使用此代码来处理初始化,按推荐:

I'm using Google App Engine with Java and Google Cloud Endpoints. In my JavaScript front end, I'm using this code to handle initialization, as recommended:

var apisToLoad = 2;
var url = '//' + $window.location.host + '/_ah/api';
gapi.client.load('sd', 'v1', handleLoad, url);
gapi.client.load('oauth2', 'v2', handleLoad);
function handleLoad() {
    // this only executes once,
    if (--apisToLoad === 0) {
        // so this is not executed
    }
}

gapi.client.load失败时如何检测和处理?目前,我在JavaScript控制台上显示了错误:Could not fetch URL: https://webapis-discovery.appspot.com/_ah/api/static/proxy.html).也许这是我的错,或者这可能是Google的暂时问题-现在,这不是我关心的问题.我正在尝试利用这个机会在客户端很好地处理此类错误.

How can I detect and handle when gapi.client.load fails? Currently I am getting an error printed to the JavaScript console that says: Could not fetch URL: https://webapis-discovery.appspot.com/_ah/api/static/proxy.html). Maybe that's my fault, or maybe it's a temporary problem on Google's end - right now that is not my concern. I'm trying to take advantage of this opportunity to handle such errors well on the client side.

那么-我该如何处理? handleLoad不会针对错误的调用执行,gapi.client.load似乎没有单独的错误回调(请参见

So - how can I handle it? handleLoad is not executed for the call that errs, gapi.client.load does not seem to have a separate error callback (see the documentation), it does not actually throw the error (only prints it to the console), and it does not return anything. What am I missing? My only idea so far is to set a timeout and assume there was an error if initialization doesn't complete after X seconds, but that is obviously less than ideal.

这个问题再次出现,这一次是消息ERR_CONNECTION_TIMED_OUT,当试图加载oauth东西时(这绝对是我无法控制的).再次,我不是要修复该错误,它只是确认值得进行适当地检测和处理.

This problem came up again, this time with the message ERR_CONNECTION_TIMED_OUT when trying to load the oauth stuff (which is definitely out of my control). Again, I am not trying to fix the error, it just confirms that it is worth detecting and handling gracefully.

推荐答案

不幸的是,这里的文档没什么用,调试有问题的代码也不是那么容易. gapi.client.load()显然在为每个API插入一个<iframe>元素.然后,该框架提供了必要的功能,并允许通过 postMessage()对其进行访问.从外观上看,API不会在该框架上附加load事件侦听器,而是依靠框架本身来表明它已准备就绪(这将导致触发回调).因此,缺少的错误回调是一个固有的问题-API无法看到失败,因为那里没有任何帧来发出信号.

Unfortunately, the documentation is pretty useless here and it's not exactly easy to debug the code in question. What gapi.client.load() apparently does is inserting an <iframe> element for each API. That frame then provides the necessary functionality and allows accessing it via postMessage(). From the look of it, the API doesn't attach a load event listener to that frame and rather relies on the frame itself to indicate that it is ready (this will result in the callback being triggered). So the missing error callback is an inherent issue - the API cannot see a failure because no frame will be there to signal it.

据我所知,最好的办法是将自己的load事件侦听器附加到文档上(事件将从框架中冒出)并在加载时进行检查. 警告:尽管这可能适用于当前版本的API,但不能保证将来会继续更改该API的实现.目前类似的东西应该可以工作:

From what I can tell, the best thing you can do is attaching your own load event listener to the document (the event will bubble up from the frames) and checking yourself when they load. Warning: While this might work with the current version of the API, it is not guaranteed to continue working in future as the implementation of that API changes. Currently something like this should work:

var framesToLoad = apisToLoad;
document.addEventListener("load", function(event)
{
  if (event.target.localName == "iframe")
  {
    framesToLoad--;
    if (framesToLoad == 0)
    {
      // Allow any outstanding synchronous actions to execute, just in case
      window.setTimeout(function()
      {
        if (apisToLoad > 0)
          alert("All frames are done but not all APIs loaded - error?");
      }, 0);
    }
  }
}, true);

仅从上面重复警告:此代码进行了许多假设.尽管使用此API可能会在一段时间内维持这些假设,但也可能是Google会做出一些更改,并且此代码将停止工作.甚至可能是Google根据浏览器使用了不同的方法,我只在Firefox中进行了测试.

Just to repeat the warning from above: this code makes lots of assumptions. While these assumptions might stay true for a while with this API, it might also be that Google will change something and this code will stop working. It might even be that Google uses a different approach depending on the browser, I only tested in Firefox.

这篇关于从gapi.client.load捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆