From ec1887e726d7c7101eba207ce22a28595fa8a510 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 29 Mar 2022 13:36:50 +0300 Subject: [PATCH] closable error usage --- .../authentication/fields/test-connection.js | 28 ++++-- .../email-account/fields/test-connection.js | 35 ++++---- .../views/outbound-email/fields/test-send.js | 89 +++++++++---------- 3 files changed, 81 insertions(+), 71 deletions(-) diff --git a/client/src/views/admin/authentication/fields/test-connection.js b/client/src/views/admin/authentication/fields/test-connection.js index 4df9b99720..4f0a06fbe8 100644 --- a/client/src/views/admin/authentication/fields/test-connection.js +++ b/client/src/views/admin/authentication/fields/test-connection.js @@ -26,11 +26,16 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -Espo.define('views/admin/authentication/fields/test-connection', 'views/fields/base', function (Dep) { +define('views/admin/authentication/fields/test-connection', 'views/fields/base', function (Dep) { return Dep.extend({ - _template: '', + _template: ` + + `, events: { 'click [data-action="testConnection"]': function () { @@ -55,6 +60,7 @@ Espo.define('views/admin/authentication/fields/test-connection', 'views/fields/b 'accountDomainNameShort': this.model.get('ldapAccountDomainNameShort'), 'accountCanonicalForm': this.model.get('ldapAccountCanonicalForm') }; + return data; }, @@ -69,29 +75,33 @@ Espo.define('views/admin/authentication/fields/test-connection', 'views/fields/b url: 'Settings/action/testLdapConnection', type: 'POST', data: JSON.stringify(data), - error: function (xhr, status) { + error: (xhr, status) => { var statusReason = xhr.getResponseHeader('X-Status-Reason') || ''; + statusReason = statusReason.replace(/ $/, ''); statusReason = statusReason.replace(/,$/, ''); var msg = this.translate('Error') + ' ' + xhr.status; + if (statusReason) { msg += ': ' + statusReason; } - Espo.Ui.error(msg); + + Espo.Ui.error(msg, true); + console.error(msg); + xhr.errorIsHandled = true; this.$el.find('button').prop('disabled', false); - }.bind(this) - }).done(function () { + } + }).then(() => { this.$el.find('button').prop('disabled', false); + Espo.Ui.success(this.translate('ldapTestConnection', 'messages', 'Settings')); - }.bind(this)); + }); }, }); - }); - diff --git a/client/src/views/email-account/fields/test-connection.js b/client/src/views/email-account/fields/test-connection.js index 0d25f7fdbe..6ddf46396a 100644 --- a/client/src/views/email-account/fields/test-connection.js +++ b/client/src/views/email-account/fields/test-connection.js @@ -60,9 +60,10 @@ define('views/email-account/fields/test-connection', 'views/fields/base', functi this.checkAvailability(); this.stopListening(this.model, 'change:host'); - this.listenTo(this.model, 'change:host', function () { + + this.listenTo(this.model, 'change:host', () => { this.checkAvailability(); - }, this); + }); }, getData: function () { @@ -76,10 +77,10 @@ define('views/email-account/fields/test-connection', 'views/fields/base', functi emailAddress: this.model.get('emailAddress'), userId: this.model.get('assignedUserId'), }; + return data; }, - test: function () { var data = this.getData(); @@ -93,31 +94,35 @@ define('views/email-account/fields/test-connection', 'views/fields/base', functi url: this.url, type: 'POST', data: JSON.stringify(data), - error: function (xhr, status) { + error: (xhr, status) => { var statusReason = xhr.getResponseHeader('X-Status-Reason') || ''; statusReason = statusReason.replace(/ $/, ''); statusReason = statusReason.replace(/,$/, ''); var msg = this.translate('Error'); + if (xhr.status != 200) { msg += ' ' + xhr.status; } + if (statusReason) { msg += ': ' + statusReason; } - Espo.Ui.error(msg); - console.error(msg); - xhr.errorIsHandled = true; - $btn.removeClass('disabled'); - }.bind(this) - }).done(function () { - $btn.removeClass('disabled'); - Espo.Ui.success(this.translate('connectionIsOk', 'messages', 'EmailAccount')); - }.bind(this)); + Espo.Ui.error(msg, true); + + console.error(msg); + + xhr.errorIsHandled = true; + + $btn.removeClass('disabled'); + } + }).then(() => { + $btn.removeClass('disabled'); + + Espo.Ui.success(this.translate('connectionIsOk', 'messages', 'EmailAccount')); + }); }, }); - }); - diff --git a/client/src/views/outbound-email/fields/test-send.js b/client/src/views/outbound-email/fields/test-send.js index 1140107643..bae58e9ead 100644 --- a/client/src/views/outbound-email/fields/test-send.js +++ b/client/src/views/outbound-email/fields/test-send.js @@ -57,9 +57,9 @@ define('views/outbound-email/fields/test-send', 'views/fields/base', function (D this.stopListening(this.model, 'change:smtpServer'); - this.listenTo(this.model, 'change:smtpServer', function () { + this.listenTo(this.model, 'change:smtpServer', () => { this.checkAvailability(); - }, this); + }); }, getSmtpData: function () { @@ -74,6 +74,7 @@ define('views/outbound-email/fields/test-send', 'views/fields/base', function (D 'fromAddress': this.model.get('outboundEmailFromAddress'), 'type': 'outboundEmail', }; + return data; }, @@ -83,10 +84,10 @@ define('views/outbound-email/fields/test-send', 'views/fields/base', function (D this.createView('popup', 'views/outbound-email/modals/test-send', { emailAddress: this.getUser().get('emailAddress') - }, function (view) { + }, (view) => { view.render(); - this.listenToOnce(view, 'send', function (emailAddress) { + this.listenToOnce(view, 'send', (emailAddress) => { this.$el.find('button').addClass('disabled'); data.emailAddress = emailAddress; @@ -95,59 +96,53 @@ define('views/outbound-email/fields/test-send', 'views/fields/base', function (D view.close(); Espo.Ajax.postRequest('Email/action/sendTestEmail', data) - .then( - function () { - this.$el.find('button').removeClass('disabled'); + .then(() => { + this.$el.find('button').removeClass('disabled'); - Espo.Ui.success(this.translate('testEmailSent', 'messages', 'Email')); + Espo.Ui.success(this.translate('testEmailSent', 'messages', 'Email')); + }) + .catch((xhr) => { + var reason = xhr.getResponseHeader('X-Status-Reason') || ''; + + reason = reason + .replace(/ $/, '') + .replace(/,$/, ''); + + var msg = this.translate('Error'); + + if (xhr.status !== 200) { + msg += ' ' + xhr.status; } - .bind(this) - ) - .fail( - function (xhr) { - var reason = xhr.getResponseHeader('X-Status-Reason') || ''; - reason = reason - .replace(/ $/, '') - .replace(/,$/, ''); + if (xhr.responseText) { + try { + var data = JSON.parse(xhr.responseText); - var msg = this.translate('Error'); - - if (xhr.status !== 200) { - msg += ' ' + xhr.status; + reason = data.message || reason; } + catch (e) { + console.error('Could not parse error response body.'); - if (xhr.responseText) { - try { - var data = JSON.parse(xhr.responseText); - - reason = data.message || reason; - } - catch (e) { - console.error('Could not parse error response body.'); - - return; - } + return; } - - if (reason) { - msg += ': ' + reason; - } - - Espo.Ui.error(msg); - - console.error(msg); - - xhr.errorIsHandled = true; - - this.$el.find('button').removeClass('disabled'); } - .bind(this) - ); - }, this); - }.bind(this)); + if (reason) { + msg += ': ' + reason; + } + Espo.Ui.error(msg, true); + + console.error(msg); + + xhr.errorIsHandled = true; + + this.$el.find('button').removeClass('disabled'); + } + ); + + }); + }); }, });