Search code examples
javascriptgoogle-app-enginepromisegoogle-cloud-endpoints

Can I handle an error from the gapi.client.load's promise?


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

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

returns the promise, but without any error callback function.

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.


Solution

  • 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.:

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

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