From 2ccc236bc2cc7c361245c73c862c1e3b726cf956 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Wed, 2 Dec 2020 19:57:49 +0200 Subject: [PATCH] Global AJAX error handling --- assets/js/backend_calendar_api.js | 8 ------ assets/js/backend_calendar_default_view.js | 19 +++++--------- assets/js/backend_calendar_google_sync.js | 9 +++---- assets/js/backend_calendar_table_view.js | 29 +++++++--------------- assets/js/backend_categories_helper.js | 9 +++---- assets/js/backend_customers_helper.js | 9 +++---- assets/js/backend_services.js | 3 +-- assets/js/backend_services_helper.js | 9 +++---- assets/js/backend_settings.js | 4 +-- assets/js/backend_settings_system.js | 3 +-- assets/js/backend_settings_user.js | 3 +-- assets/js/backend_users.js | 6 ++--- assets/js/backend_users_admins.js | 9 +++---- assets/js/backend_users_providers.js | 9 +++---- assets/js/backend_users_secretaries.js | 9 +++---- assets/js/forgot_password.js | 3 +-- assets/js/frontend_book_api.js | 14 ++++------- assets/js/general_functions.js | 10 ++++++-- assets/js/installation.js | 3 +-- assets/js/login.js | 3 +-- 20 files changed, 58 insertions(+), 113 deletions(-) diff --git a/assets/js/backend_calendar_api.js b/assets/js/backend_calendar_api.js index 70526a1a..6e00d27a 100755 --- a/assets/js/backend_calendar_api.js +++ b/assets/js/backend_calendar_api.js @@ -52,8 +52,6 @@ window.BackendCalendarApi = window.BackendCalendarApi || {}; } }) .fail(function (jqXHR, textStatus, errorThrown) { - GeneralFunctions.ajaxFailureHandler(jqXHR, textStatus, errorThrown); - if (errorCallback) { errorCallback(); } @@ -82,8 +80,6 @@ window.BackendCalendarApi = window.BackendCalendarApi || {}; } }) .fail(function (jqXHR, textStatus, errorThrown) { - GeneralFunctions.ajaxFailureHandler(jqXHR, textStatus, errorThrown); - if (errorCallback) { errorCallback(); } @@ -117,8 +113,6 @@ window.BackendCalendarApi = window.BackendCalendarApi || {}; } }) .fail(function (jqXHR, textStatus, errorThrown) { - GeneralFunctions.ajaxFailureHandler(jqXHR, textStatus, errorThrown); - if (errorCallback) { errorCallback(); } @@ -141,8 +135,6 @@ window.BackendCalendarApi = window.BackendCalendarApi || {}; } }) .fail(function (jqXHR, textStatus, errorThrown) { - GeneralFunctions.ajaxFailureHandler(jqXHR, textStatus, errorThrown); - if (errorCallback) { errorCallback(); } diff --git a/assets/js/backend_calendar_default_view.js b/assets/js/backend_calendar_default_view.js index c29558b5..bc83e07d 100755 --- a/assets/js/backend_calendar_default_view.js +++ b/assets/js/backend_calendar_default_view.js @@ -224,8 +224,7 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; // Refresh calendar event items. $('#select-filter-item').trigger('change'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } } ]; @@ -255,8 +254,7 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; // Refresh calendar event items. $('#select-filter-item').trigger('change'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } }); @@ -728,8 +726,7 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.appointment_updated, [ @@ -777,8 +774,7 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.unavailable_updated, [ @@ -898,8 +894,7 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.appointment_updated, [ @@ -948,8 +943,7 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.unavailable_updated, [ @@ -1366,7 +1360,6 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; } } }) - .fail(GeneralFunctions.ajaxFailureHandler) .always(function () { $('#loading').css('visibility', '') }); diff --git a/assets/js/backend_calendar_google_sync.js b/assets/js/backend_calendar_google_sync.js index 149b937a..269261a6 100644 --- a/assets/js/backend_calendar_google_sync.js +++ b/assets/js/backend_calendar_google_sync.js @@ -77,8 +77,7 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; }); $('#select-google-calendar').modal('show'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } } } catch (Error) { @@ -125,8 +124,7 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; .done(function () { Backend.displayNotification(EALang.google_calendar_selected); $('#select-google-calendar').modal('hide'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }); /** @@ -169,8 +167,7 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; provider_id: providerId }; - $.post(url, data) - .fail(GeneralFunctions.ajaxFailureHandler); + $.post(url, data); } diff --git a/assets/js/backend_calendar_table_view.js b/assets/js/backend_calendar_table_view.js index fd2742db..1ea8924e 100755 --- a/assets/js/backend_calendar_table_view.js +++ b/assets/js/backend_calendar_table_view.js @@ -117,9 +117,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; // setCalendarViewSize(); Backend.placeFooterToBottom(); - - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }); /** @@ -266,8 +264,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; // Refresh calendar event items. $('#select-filter-item').trigger('change'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } else if (lastFocusedEventData.data.is_unavailable === '0') { var buttons = [ { @@ -293,8 +290,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; // Refresh calendar event items. $('#select-filter-item').trigger('change'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } } ]; @@ -323,8 +319,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; // Refresh calendar event items. $('#select-filter-item').trigger('change'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } }); } @@ -540,9 +535,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; $providerColumn.find('.calendar-wrapper') .fullCalendar('changeView', providerView[providerId] || 'agendaDay'); }); - - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } /** @@ -1466,8 +1459,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.appointment_updated, [ @@ -1515,8 +1507,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.unavailable_updated, [ @@ -1605,8 +1596,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.appointment_updated, [ @@ -1656,8 +1646,7 @@ window.BackendCalendarTableView = window.BackendCalendarTableView || {}; .done(function () { $('#notification').hide('blind'); revertFunc(); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; Backend.displayNotification(EALang.unavailable_updated, [ diff --git a/assets/js/backend_categories_helper.js b/assets/js/backend_categories_helper.js index 47ac674c..f0e3efe5 100644 --- a/assets/js/backend_categories_helper.js +++ b/assets/js/backend_categories_helper.js @@ -207,8 +207,7 @@ if (selectId) { this.select(selectId, display); } - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -231,8 +230,7 @@ $('#filter-categories .key').val(''); this.filter('', response.id, true); BackendServices.updateAvailableCategories(); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -255,8 +253,7 @@ this.resetForm(); this.filter($('#filter-categories .key').val()); BackendServices.updateAvailableCategories(); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** diff --git a/assets/js/backend_customers_helper.js b/assets/js/backend_customers_helper.js index 91845e57..93e84d5f 100644 --- a/assets/js/backend_customers_helper.js +++ b/assets/js/backend_customers_helper.js @@ -185,8 +185,7 @@ this.resetForm(); $('#filter-customers .key').val(''); this.filter('', response.id, true); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -207,8 +206,7 @@ Backend.displayNotification(EALang.customer_deleted); this.resetForm(); this.filter($('#filter-customers .key').val()); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -412,8 +410,7 @@ this.select(selectId, display); } - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** diff --git a/assets/js/backend_services.js b/assets/js/backend_services.js index 63cd0143..345288b8 100644 --- a/assets/js/backend_services.js +++ b/assets/js/backend_services.js @@ -110,7 +110,6 @@ window.BackendServices = window.BackendServices || {}; }); $select.append(new Option('- ' + EALang.no_category + ' -', null)).val('null'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; })(window.BackendServices); diff --git a/assets/js/backend_services_helper.js b/assets/js/backend_services_helper.js index 9e747a49..9d97bcd4 100644 --- a/assets/js/backend_services_helper.js +++ b/assets/js/backend_services_helper.js @@ -208,8 +208,7 @@ this.resetForm(); $('#filter-services .key').val(''); this.filter('', response.id, true); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -231,8 +230,7 @@ this.resetForm(); this.filter($('#filter-services .key').val()); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -365,8 +363,7 @@ if (selectId) { this.select(selectId, display); } - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** diff --git a/assets/js/backend_settings.js b/assets/js/backend_settings.js index 6a3c4ff4..53373b0b 100644 --- a/assets/js/backend_settings.js +++ b/assets/js/backend_settings.js @@ -218,8 +218,7 @@ window.BackendSettings = window.BackendSettings || {}; $input.closest('.form-group').removeClass('has-error'); $input.attr('already-exists', 'false'); } - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }); /** @@ -247,7 +246,6 @@ window.BackendSettings = window.BackendSettings || {}; .done(function () { Backend.displayNotification(EALang.working_plans_got_updated); }) - .fail(GeneralFunctions.ajaxFailureHandler) .always(function() { $('#message-box').dialog('close'); }); diff --git a/assets/js/backend_settings_system.js b/assets/js/backend_settings_system.js index 0a9b260b..cebb0e98 100644 --- a/assets/js/backend_settings_system.js +++ b/assets/js/backend_settings_system.js @@ -60,8 +60,7 @@ var workingPlan = BackendSettings.wp.get(); BackendSettings.wp.setup(workingPlan); BackendSettings.wp.timepickers(false); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; /** diff --git a/assets/js/backend_settings_user.js b/assets/js/backend_settings_user.js index 7903bb05..7d6de0ef 100644 --- a/assets/js/backend_settings_user.js +++ b/assets/js/backend_settings_user.js @@ -79,8 +79,7 @@ // Update footer greetings. $('#footer-user-display-name').text('Hello, ' + $('#first-name').val() + ' ' + $('#last-name').val() + '!'); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; /** diff --git a/assets/js/backend_users.js b/assets/js/backend_users.js index 051ee682..fe48f688 100644 --- a/assets/js/backend_users.js +++ b/assets/js/backend_users.js @@ -181,8 +181,7 @@ window.BackendUsers = window.BackendUsers || {}; }) .appendTo('#secretary-providers'); }); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } helper.resetForm(); @@ -233,8 +232,7 @@ window.BackendUsers = window.BackendUsers || {}; $input.parents().eq(3).find('.form-message').hide(); } } - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }); } diff --git a/assets/js/backend_users_admins.js b/assets/js/backend_users_admins.js index 1833f4f0..dfe1c823 100644 --- a/assets/js/backend_users_admins.js +++ b/assets/js/backend_users_admins.js @@ -200,8 +200,7 @@ this.resetForm(); $('#filter-admins .key').val(''); this.filter('', response.id, true); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -222,8 +221,7 @@ Backend.displayNotification(EALang.admin_deleted); this.resetForm(); this.filter($('#filter-admins .key').val()); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -379,8 +377,7 @@ if (selectId) { this.select(selectId, display); } - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** diff --git a/assets/js/backend_users_providers.js b/assets/js/backend_users_providers.js index 0b57b698..6f7a7cb3 100755 --- a/assets/js/backend_users_providers.js +++ b/assets/js/backend_users_providers.js @@ -236,8 +236,7 @@ this.resetForm(); $('#filter-providers .key').val(''); this.filter('', response.id, true); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -257,8 +256,7 @@ Backend.displayNotification(EALang.provider_deleted); this.resetForm(); this.filter($('#filter-providers .key').val()); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -484,8 +482,7 @@ if (selectId) { this.select(selectId, display); } - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** diff --git a/assets/js/backend_users_secretaries.js b/assets/js/backend_users_secretaries.js index 82b8e053..322cac52 100644 --- a/assets/js/backend_users_secretaries.js +++ b/assets/js/backend_users_secretaries.js @@ -214,8 +214,7 @@ this.resetForm(); $('#filter-secretaries .key').val(''); this.filter('', response.id, true); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -236,8 +235,7 @@ Backend.displayNotification(EALang.secretary_deleted); this.resetForm(); this.filter($('#filter-secretaries .key').val()); - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** @@ -406,8 +404,7 @@ if (selectId) { this.select(selectId, display); } - }.bind(this)) - .fail(GeneralFunctions.ajaxFailureHandler); + }.bind(this)); }; /** diff --git a/assets/js/forgot_password.js b/assets/js/forgot_password.js index 1f852b0c..c7aa0ad9 100644 --- a/assets/js/forgot_password.js +++ b/assets/js/forgot_password.js @@ -37,8 +37,7 @@ $(function () { $alert.text('The operation failed! Please enter a valid username ' + 'and email address in order to get a new password.'); } - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } diff --git a/assets/js/frontend_book_api.js b/assets/js/frontend_book_api.js index 5fc207dd..2c1ea174 100755 --- a/assets/js/frontend_book_api.js +++ b/assets/js/frontend_book_api.js @@ -126,8 +126,7 @@ window.FrontendBookApi = window.FrontendBookApi || {}; } else { $('#available-hours').text(EALang.no_available_hours); } - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; /** @@ -206,7 +205,6 @@ window.FrontendBookApi = window.FrontendBookApi || {}; }) .fail(function (jqxhr, textStatus, errorThrown) { $('.captcha-title small').trigger('click'); - GeneralFunctions.ajaxFailureHandler(jqxhr, textStatus, errorThrown); }) .always(function () { $layer.remove(); @@ -256,8 +254,7 @@ window.FrontendBookApi = window.FrontendBookApi || {}; unavailableDatesBackup = response; selectedDateStringBackup = selectedDateString; applyUnavailableDates(response, selectedDateString, true); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }; exports.applyPreviousUnavailableDates = function () { @@ -313,7 +310,7 @@ window.FrontendBookApi = window.FrontendBookApi || {}; consent: consent }; - $.post(url, data).fail(GeneralFunctions.ajaxFailureHandler); + $.post(url, data); }; /** @@ -331,9 +328,8 @@ window.FrontendBookApi = window.FrontendBookApi || {}; $.post(url, data) .done(function () { - location.href = GlobalVariables.baseUrl; - }) - .fail(GeneralFunctions.ajaxFailureHandler); + window.location.href = GlobalVariables.baseUrl; + }); }; })(window.FrontendBookApi); diff --git a/assets/js/general_functions.js b/assets/js/general_functions.js index 0671bc35..44e0e61a 100755 --- a/assets/js/general_functions.js +++ b/assets/js/general_functions.js @@ -22,6 +22,13 @@ window.GeneralFunctions = window.GeneralFunctions || {}; 'use strict'; + /** + * Register global error handler. + */ + $(document).ajaxError(function (event, jqxhr, settings, thrownError) { + GeneralFunctions.ajaxFailureHandler(jqxhr, settings, thrownError); + }); + /** * This functions displays a message box in the admin array. It is useful when user * decisions or verifications are needed. @@ -288,8 +295,7 @@ window.GeneralFunctions = window.GeneralFunctions || {}; $.post(url, data) .done(function () { document.location.reload(true); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }); $(document).on('click', function() { diff --git a/assets/js/installation.js b/assets/js/installation.js index 442d2113..b4c7abd7 100644 --- a/assets/js/installation.js +++ b/assets/js/installation.js @@ -56,8 +56,7 @@ $(function () { setTimeout(function () { window.location.href = GlobalVariables.baseUrl + '/index.php/backend'; }, 1000); - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); }); /** diff --git a/assets/js/login.js b/assets/js/login.js index b5cf2923..15a33d6d 100644 --- a/assets/js/login.js +++ b/assets/js/login.js @@ -34,8 +34,7 @@ $(function () { .removeClass('d-none alert-danger alert-success') .addClass('alert-danger'); } - }) - .fail(GeneralFunctions.ajaxFailureHandler); + }); } $loginForm.on('submit', onLoginFormSubmit);