GibboK
GibboK

Reputation: 73908

How to detect request timeout errors in dojo? As dojo/request/notify does not detect it

I am using dojo/request/notify in order to globally detect errors when an issue with a Ajax callis found.

For calling an API I am using dojo/store/JsonRest. When requests have failed notify works as expected but in case of timeout notify does not work.

I need to detect timeout errors.

Upvotes: 0

Views: 364

Answers (1)

GibboK
GibboK

Reputation: 73908

As it looks like that dojo does not offer this option out of the box.

I have thought the following solutions:

  1. Write a custom version of dojo/store/JsonRest which pass a timeout value.
  2. Monkey patch dojo/_base/xhr.
  3. Money patch XMLHttpRequest.

I have chosen solution 3. here below a code example:

 (function (xhr) {
        var send = xhr.send;
        xhr.send = function (data) {
            this.timeout = 5000;
            var hasTimeOut = 'ontimeout' in this;
            if (hasTimeOut) {
                this.ontimeout = function () {
                    throw ('Error XMLHttpRequest timeout.');
                };
            }
            return send.apply(this, arguments);
        };
    })(XMLHttpRequest.prototype);

Related question.

Upvotes: 1

Related Questions