Improve handle of network connection errors (fix #203)

This commit is contained in:
Fedor A. Fetisov 2021-03-18 15:50:49 +03:00
parent 3148d0eb2a
commit 805bb610a5
3 changed files with 35 additions and 4 deletions

View File

@ -41,6 +41,8 @@
// Pause before restarting updater using Server.restartUpdater // Pause before restarting updater using Server.restartUpdater
// function (in seconds) // function (in seconds)
reconnectPause: 1, reconnectPause: 1,
// Call on successful response
onReceiveResponse: function() {},
// Call on request timeout // Call on request timeout
onTimeout: function() {}, onTimeout: function() {},
// Call when transport error was caught // Call when transport error was caught
@ -371,6 +373,8 @@
* @private * @private
*/ */
Mibew.Server.prototype.receiveResponse = function(data, textStatus, jqXHR) { Mibew.Server.prototype.receiveResponse = function(data, textStatus, jqXHR) {
// Call hook on successful request
this.options.onReceiveResponse();
// Do not parse empty responses // Do not parse empty responses
if (data == '') { if (data == '') {
this.updateAfter(this.options.requestsFrequency); this.updateAfter(this.options.requestsFrequency);

View File

@ -197,7 +197,7 @@
// Do not open alert if one already opened. // Do not open alert if one already opened.
return; return;
} }
vex.dialog.alert({message: sanitizeMessage(message)}); return vex.dialog.alert({message: sanitizeMessage(message)});
}; };
/** /**
@ -230,4 +230,12 @@
}); });
}; };
/**
* Close alert.
* @param {Object} alert instance.
*/
Mibew.Utils.closeAlert = function(alertInstance) {
var res = vex.close(alertInstance.id);
};
})(Mibew, jQuery, _, vex, validator); })(Mibew, jQuery, _, vex, validator);

View File

@ -24,6 +24,12 @@
*/ */
var badRequestsCount = 0; var badRequestsCount = 0;
/**
* Represent alert object for network problems notification
* @type Object
*/
var networkProblemsAlert = null;
/** /**
* Increase badRequestsCount and show reconnect message if need. * Increase badRequestsCount and show reconnect message if need.
* Calls on every bad request. * Calls on every bad request.
@ -32,12 +38,24 @@
// Increase bad requests count // Increase bad requests count
badRequestsCount++; badRequestsCount++;
// Check if there is // Check if there is
if (badRequestsCount == 10) { if (!networkProblemsAlert && (badRequestsCount == 10)) {
Mibew.Utils.alert(Mibew.Localization.trans('Network problems detected. Please refresh the page.')); networkProblemsAlert = Mibew.Utils.alert(Mibew.Localization.trans('Network problems detected. Please refresh the page.'));
badRequestsCount = 0; badRequestsCount = 0;
} }
}; };
/**
* Reset badRequestsCount and close reconnect message if need.
* Calls on every successful request.
*/
var successfulResponseHandler = function() {
badRequestsCount = 0;
if (networkProblemsAlert) {
Mibew.Utils.closeAlert(networkProblemsAlert);
}
networkProblemsAlert = null;
}
// Create application instance // Create application instance
var App = new Backbone.Marionette.Application(); var App = new Backbone.Marionette.Application();
@ -61,7 +79,8 @@
{ {
'interactionType': MibewAPIUsersInteraction, 'interactionType': MibewAPIUsersInteraction,
onTimeout: requestErrorHandler, onTimeout: requestErrorHandler,
onTransportError: requestErrorHandler onTransportError: requestErrorHandler,
onReceiveResponse: successfulResponseHandler,
}, },
options.server options.server
)); ));