我可以处理gapi.client.load的承诺中的错误吗?

时间:2021-04-22 23:09:10

Question if gapi.client.load returns the promise was discussed here. And as Mike Witt answered, the code:

如果在这里讨论了gapi.client.load返回promise的问题。正如Mike Witt回答的那样,代码:

gapi.client.load('guestbook', 'v1', undefined, '/_ah/api');

returns the promise, but without any error callback function.

返回promise,但没有任何错误回调函数。

I tried to handle the error:

我试图处理错误:

gapi.client.load('guestbook', 'v1', undefined, '/_ah/api')
  .then(
     function() {
       //success
     },
     function(error) {
       //error
     }
  );

and when I turn off an endpoint module it will never steps into the error handler. I'm only getting a following error in a console:

当我关闭端点模块时,它永远不会进入错误处理程序。我只是在控制台中收到以下错误:

GET http://localhost:8080/_ah/api/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-s…3DIQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCOu-PQv0yFB8pB9mX2w3nuej8rl5Q net::ERR_CONNECTION_REFUSED  cb=gapi.loaded_0:163

Is there any way how to handle this error? I've tried to find it in docs, but without success.

有什么办法可以处理这个错误吗?我试图在文档中找到它,但没有成功。

3 个解决方案

#1


3  

After some time I have found the answer:

过了一段时间,我找到了答案:

gapi.client.load('guestbook', 'v1', undefined, '/_ah/api')
  .then(
    function(response) {
      if(response && response.hasOwnProperty('error')) {
         // error
      } else {
         // success
      }
    }
  );

In the case of some error, the gapi.client.load returns an error object e.g.:

在出现一些错误的情况下,gapi.client.load返回一个错误对象,例如:

{error: {errors: Array[1], code: 404, message: "Not Found"}}

but it has to be "caught" in the .then() not in the .catch()

但必须在.then()中“捕获”而不是在.catch()中

#2


1  

You can easily test for a fail (at least now). I am pretty sure this is what you are looking for.

您可以轻松测试失败(至少现在)。我很确定这就是你要找的东西。

Here is the code:

这是代码:

gapi.client.init({}).then(() => {
     gapi.client.load('some-api', "v1", (err) => { callback(err) }, "https://someapi.appspot.com/_ah/api");
}, err, err);

function callback(loadErr) {
    if (loadErr) { err(loadErr); return; }
    // success code here 
}

function err(err){
     console.log('Error: ', err);
     // fail code here
}

Example

#3


0  

The syntax for this method is : gapi.client.load(API_NAME, API_VERSION, CALLBACK);

此方法的语法是:gapi.client.load(API_NAME,API_VERSION,CALLBACK);

you might have a look here [1], also this * question can help you [2].

你可能看看这里[1],这个*问题也可以帮助你[2]。

'/_ah/api' cannot be used as a callback as it is a specific handler for testing and viewing APIs of your Google App, for example when using endpoints for mobile applications.

'/ _ah / api'不能用作回调,因为它是用于测试和查看Google App API的特定处理程序,例如在为移动应用程序使用端点时。

[1] https://developers.google.com/api-client-library/javascript/dev/dev_jscript#OptionLoadtheserviceAPIthenassembletherequest

[1] https://developers.google.com/api-client-library/javascript/dev/dev_jscript#OptionLoadtheserviceAPIthenassembletherequest

[2] Catch Error from gapi.client.load

[2]来自gapi.client.load的Catch错误

#1


3  

After some time I have found the answer:

过了一段时间,我找到了答案:

gapi.client.load('guestbook', 'v1', undefined, '/_ah/api')
  .then(
    function(response) {
      if(response && response.hasOwnProperty('error')) {
         // error
      } else {
         // success
      }
    }
  );

In the case of some error, the gapi.client.load returns an error object e.g.:

在出现一些错误的情况下,gapi.client.load返回一个错误对象,例如:

{error: {errors: Array[1], code: 404, message: "Not Found"}}

but it has to be "caught" in the .then() not in the .catch()

但必须在.then()中“捕获”而不是在.catch()中

#2


1  

You can easily test for a fail (at least now). I am pretty sure this is what you are looking for.

您可以轻松测试失败(至少现在)。我很确定这就是你要找的东西。

Here is the code:

这是代码:

gapi.client.init({}).then(() => {
     gapi.client.load('some-api', "v1", (err) => { callback(err) }, "https://someapi.appspot.com/_ah/api");
}, err, err);

function callback(loadErr) {
    if (loadErr) { err(loadErr); return; }
    // success code here 
}

function err(err){
     console.log('Error: ', err);
     // fail code here
}

Example

#3


0  

The syntax for this method is : gapi.client.load(API_NAME, API_VERSION, CALLBACK);

此方法的语法是:gapi.client.load(API_NAME,API_VERSION,CALLBACK);

you might have a look here [1], also this * question can help you [2].

你可能看看这里[1],这个*问题也可以帮助你[2]。

'/_ah/api' cannot be used as a callback as it is a specific handler for testing and viewing APIs of your Google App, for example when using endpoints for mobile applications.

'/ _ah / api'不能用作回调,因为它是用于测试和查看Google App API的特定处理程序,例如在为移动应用程序使用端点时。

[1] https://developers.google.com/api-client-library/javascript/dev/dev_jscript#OptionLoadtheserviceAPIthenassembletherequest

[1] https://developers.google.com/api-client-library/javascript/dev/dev_jscript#OptionLoadtheserviceAPIthenassembletherequest

[2] Catch Error from gapi.client.load

[2]来自gapi.client.load的Catch错误