angucomplete-alt icon indicating copy to clipboard operation
angucomplete-alt copied to clipboard

AJAX errors are sometimes ignored (httpErrorCallbackFn and remote-url-error-callback)

Open rvalitov opened this issue 7 years ago • 0 comments

According to the documentation remote-url-error-callback parameter defines a user function which is supposed to be called when AJAX error happens. However, this function is called only for cases when the server returns a error code (for example, 404 Not Found, or 500 Internal Server Error). But there are also other types of network errors, when the callback function is not fired, I tested the following cases (probably there are more):

  • Invalid domain name (the browser couldn't resolve the domain name = domain name not found)
  • HTTP timeout (the server didn't respond)
  • CORS error (the origin is not allowed by Access-Control-Allow-Origin)

The call to user defined callback function is ignored, because of the following code:

function httpErrorCallback(errorRes, status, headers, config) {
        scope.searching = httpCallInProgress;

        // normalize return obejct from promise
        if (!status && !headers && !config) {
          status = errorRes.status;
        }

        // cancelled/aborted
        if (status === 0 || status === -1) { return; }

It checks that error status code should not be 0 and -1. However, for all network errors (such as I mentioned above), when the HTTP status code couldn't be obtained from the server because the connection failed, the status will be -1.

It's not documented in AngularJS how to validate that the request was aborted/cancelled or it actually failed:

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout

For example, in jQuery such situation is well documented:

Possible values for the textStatus argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

I think that the user callback function should be called in any case.

rvalitov avatar Jul 02 '17 09:07 rvalitov