server_events: Extract functions for managing connection errors.

This cuts a bit of code duplication.
This commit is contained in:
Tim Abbott 2018-11-29 12:31:08 -08:00
parent 01927fb470
commit 83b127cec1
1 changed files with 14 additions and 8 deletions

View File

@ -142,6 +142,16 @@ function get_events_success(events) {
}); });
} }
function show_ui_connection_error() {
ui_report.show_error($("#connection-error"));
$("#connection-error").addClass('get-events-error');
}
function hide_ui_connection_error() {
ui_report.hide_error($("#connection-error"));
$("#connection-error").removeClass('get-events-error');
}
function get_events(options) { function get_events(options) {
options = _.extend({dont_block: false}, options); options = _.extend({dont_block: false}, options);
@ -183,8 +193,7 @@ function get_events(options) {
try { try {
get_events_xhr = undefined; get_events_xhr = undefined;
get_events_failures = 0; get_events_failures = 0;
ui_report.hide_error($("#connection-error")); hide_ui_connection_error();
$("#connection-error").removeClass('get-events-error');
get_events_success(data.events); get_events_success(data.events);
} catch (ex) { } catch (ex) {
@ -215,18 +224,15 @@ function get_events(options) {
} else if (error_type === 'timeout') { } else if (error_type === 'timeout') {
// Retry indefinitely on timeout. // Retry indefinitely on timeout.
get_events_failures = 0; get_events_failures = 0;
ui_report.hide_error($("#connection-error")); hide_ui_connection_error();
$("#connection-error").removeClass('get-events-error');
} else { } else {
get_events_failures += 1; get_events_failures += 1;
} }
if (get_events_failures >= 5) { if (get_events_failures >= 5) {
ui_report.show_error($("#connection-error")); show_ui_connection_error();
$("#connection-error").addClass('get-events-error');
} else { } else {
ui_report.hide_error($("#connection-error")); hide_ui_connection_error();
$("#connection-error").removeClass('get-events-error');
} }
} catch (ex) { } catch (ex) {
blueslip.error('Failed to handle get_events error\n' + blueslip.error('Failed to handle get_events error\n' +