Error while calling endpoint with JS Library (A network error occurred, and the request could not be completed.)

630 views
Skip to first unread message

Renaud Tarnec

unread,
Apr 27, 2016, 3:43:17 PM4/27/16
to Google App Engine
Hello,

Today, after calling one of my Endpoints from a web page through the JavaScript API I got the following values for the response from the API:

resp.code: -1

resp.message:  A network error occurred, and the request could not be completed.

This endpoint saves an entity in the Datastore with Objectify and adds a Task to a Queue (all within an Objectify transaction). Actually the code was executed correctly, despite the error message (i.e. the entity was saved in the Datastore and the task executed correctly).

It's the first time I encounter this error, and its several months I work on this application. Right after I was able to call the API the same way without anymore problems.

Any explanations or recommendations?

Many thanks in advance
Renaud

Nicholas (Google Cloud Support)

unread,
Apr 28, 2016, 1:37:06 PM4/28/16
to Google App Engine
Thanks for posting your issue here and providing some context. It's difficult to know exactly what the error is, especially if it's transient.

From what I understand, the client is making requests to your Cloud Endpoints using the API Client Library for Javascript. If this is the case, the generic way of issuing such requests is using the gapi.client.request(args) method. This returns a promise with which to use the then() or execute() methods.

Unfortunately, the documentation for both those methods does not state explicitly under what conditions it would return the values you received. From this, it's fair to suspect those values are actually returned from the server and not an issue with the Javascript API. This could be confirmed by inspecting the network requests using Chrome Developer Tools and looking at the raw response.

That being said, if it's confirmed to be a response from the server and is reproducible, please feel free provide the Endpoints code that causes this so we can attempt to reproduce and identify the root cause. Also, please test this endpoint using the API Explorer. If it's very rare, I would suggest adding some retry system to your front end to account for network issues like this. An example follows:

var request_args = {
 
'params': {
   
'q': 'search_values'
 
},
 
'path': 'path/to/resource'
};


function success(response) {}
function failure(reason) {
  console
.error(reason);
}


var promise = gapi.client.request(request_args).then(success, function (reason) {
  failure
(reason);
  console
.debug('retrying');
  setTimeout
(function () {
    gapi
.client.request(request_args).then(success, failure);
 
}, 500);
});


Hope this helps. Let us know if you continue to experience this network error and can confirm it's a platform issue or if you have any other questions on the subject.

Renaud Tarnec

unread,
Apr 29, 2016, 11:57:11 AM4/29/16
to Google App Engine
Hello Nicholas,

Thanks for your answer. Indeed the error is transient, as it happened only once in the last 6 months. I will not hesitate to contact you if it happens again, as you suggest.

Thanks also for the code example. I've realized that actually I do not use the promises "mode" but the callback "mode" of the API Client Library for JavaScript. I call the endpoints the same way it is shown in this Google example https://cloud.google.com/appengine/docs/java/endpoints/calling-from-javascript, i.e. like:

        gapi.client.myOwnApi.get(requestData).execute(function (resp) {
            if (!resp.code) {
              //success
            } else {
              //error
            }
        });

In the documentation of the JS Library you linked to, it is "strongly recommend" to "use promises instead of callbacks". I guess you would recommend so!

Thanks in advance for your answer,
Renaud

Nicholas (Google Cloud Support)

unread,
May 5, 2016, 3:54:56 PM5/5/16
to Google App Engine
Promises are definitely the way to go. They can take a little getting used when using them in a nested fashion but are definitely robust and extremely effective when designing truly asynchronous front-ends.

The spec can be found at Promises/A+. Some good examples can be found here. An important consideration is browser compatibility. For that, I would suggest caniuse or kangax. With some practiced modular design, one can get to code like so:

asyncRequestForJSON(url)
    .then(validateData)
    .then(saveToLocalStorage)
    .then(updateUI);

In the above, the intent of the code is very clear. In a sense, this makes asynchronous code readable in a top-to-bottom linear fashion. I hope this information is helpful and provides some food for thought.


On Wednesday, April 27, 2016 at 3:43:17 PM UTC-4, Renaud Tarnec wrote:
Reply all
Reply to author
Forward
0 new messages