From 87796afa25c4e53f868ac9c702223c3086f87cb1 Mon Sep 17 00:00:00 2001 From: Erik Clarizio Date: Thu, 28 Jun 2018 10:37:00 -0700 Subject: [PATCH] Refactor API failure logic into DialogUserSubmitErrorHandlerService https://bugzilla.redhat.com/show_bug.cgi?id=1591484 --- .../dialog_user/dialog_user_controller.js | 36 +++++++++++++----- .../dialog_user_reconfigure_controller.js | 26 +++++++------ ...ialog_user_submit_error_handler_service.js | 13 +++++++ .../dialog_user_controller.spec.js | 38 +++++++------------ 4 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 app/assets/javascripts/services/dialog_user_submit_error_handler_service.js diff --git a/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js b/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js index e7ce8a9bad9e..ce56e18567f7 100644 --- a/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js +++ b/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js @@ -1,4 +1,28 @@ -ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', 'resourceActionId', 'targetId', 'targetType', function(API, dialogFieldRefreshService, miqService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint, resourceActionId, targetId, targetType) { +ManageIQ.angular.app.controller('dialogUserController', + ['API', + 'dialogFieldRefreshService', + 'miqService', + 'dialogUserSubmitErrorHandlerService', + 'dialogId', + 'apiSubmitEndpoint', + 'apiAction', + 'finishSubmitEndpoint', + 'cancelEndpoint', + 'resourceActionId', + 'targetId', + 'targetType', function(API, + dialogFieldRefreshService, + miqService, + dialogUserSubmitErrorHandlerService, + dialogId, + apiSubmitEndpoint, + apiAction, + finishSubmitEndpoint, + cancelEndpoint, + resourceActionId, + targetId, + targetType) { + var vm = this; vm.$onInit = function() { @@ -58,15 +82,7 @@ ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefr return API.post(apiSubmitEndpoint, apiData, {skipErrors: [400]}).then(function() { miqService.redirectBack(__('Order Request was Submitted'), 'info', finishSubmitEndpoint); }).catch(function(err) { - miqService.sparkleOff(); - var fullErrorMessage = err.data.error.message; - var allErrorMessages = fullErrorMessage.split('-')[1].split(','); - clearFlash(); - _.forEach(allErrorMessages, (function(errorMessage) { - add_flash(errorMessage, 'error'); - })); - console.error(err); - return Promise.reject(err); + return Promise.reject(dialogUserSubmitErrorHandlerService.handleError(err)); }); } diff --git a/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js b/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js index 0ed82624dc1c..012777e8e758 100644 --- a/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js +++ b/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js @@ -1,4 +1,16 @@ -ManageIQ.angular.app.controller('dialogUserReconfigureController', ['API', 'dialogFieldRefreshService', 'miqService', 'resourceActionId', 'targetId', function(API, dialogFieldRefreshService, miqService, resourceActionId, targetId) { +ManageIQ.angular.app.controller('dialogUserReconfigureController', + ['API', + 'dialogFieldRefreshService', + 'miqService', + 'dialogUserSubmitErrorHandlerService', + 'resourceActionId', + 'targetId', function(API, + dialogFieldRefreshService, + miqService, + dialogUserSubmitErrorHandlerService, + resourceActionId, + targetId) { + var vm = this; vm.$onInit = function() { @@ -32,7 +44,7 @@ ManageIQ.angular.app.controller('dialogUserReconfigureController', ['API', 'dial dialogId: vm.dialogId, resourceActionId: resourceActionId, targetId: targetId, - targetType: "service" + targetType: "service", }; return dialogFieldRefreshService.refreshField(vm.dialogData, [field.name], vm.refreshUrl, idList); @@ -52,15 +64,7 @@ ManageIQ.angular.app.controller('dialogUserReconfigureController', ['API', 'dial return API.post(apiSubmitEndpoint, apiData, {skipErrors: [400]}).then(function() { miqService.redirectBack(__('Order Request was Submitted'), 'info', '/service/explorer'); }).catch(function(err) { - miqService.sparkleOff(); - var fullErrorMessage = err.data.error.message; - var allErrorMessages = fullErrorMessage.split('-')[1].split(','); - clearFlash(); - _.forEach(allErrorMessages, (function(errorMessage) { - add_flash(errorMessage, 'error'); - })); - console.error(err); - return Promise.reject(err); + return Promise.reject(dialogUserSubmitErrorHandlerService.handleError(err)); }); } diff --git a/app/assets/javascripts/services/dialog_user_submit_error_handler_service.js b/app/assets/javascripts/services/dialog_user_submit_error_handler_service.js new file mode 100644 index 000000000000..86f3c73cdf06 --- /dev/null +++ b/app/assets/javascripts/services/dialog_user_submit_error_handler_service.js @@ -0,0 +1,13 @@ +ManageIQ.angular.app.service('dialogUserSubmitErrorHandlerService', ['miqService', function(miqService) { + this.handleError = function(err) { + miqService.sparkleOff(); + var fullErrorMessage = err.data.error.message; + var allErrorMessages = fullErrorMessage.split('-')[1].split(','); + clearFlash(); + _.forEach(allErrorMessages, function(errorMessage) { + add_flash(errorMessage, 'error'); + }); + console.error(err); + return err; + }; +}]); diff --git a/spec/javascripts/controllers/dialog_user/dialog_user_controller.spec.js b/spec/javascripts/controllers/dialog_user/dialog_user_controller.spec.js index 0fab29727311..69773f218318 100644 --- a/spec/javascripts/controllers/dialog_user/dialog_user_controller.spec.js +++ b/spec/javascripts/controllers/dialog_user/dialog_user_controller.spec.js @@ -1,11 +1,16 @@ describe('dialogUserController', function() { - var $controller, API, dialogFieldRefreshService, miqService; + var $controller, API, dialogFieldRefreshService, dialogUserSubmitErrorHandlerService, miqService; beforeEach(module('ManageIQ')); - beforeEach(inject(function(_$controller_, _API_, _dialogFieldRefreshService_, _miqService_) { + beforeEach(inject(function(_$controller_, + _API_, + _dialogFieldRefreshService_, + _dialogUserSubmitErrorHandlerService_, + _miqService_) { API = _API_; dialogFieldRefreshService = _dialogFieldRefreshService_; + dialogUserSubmitErrorHandlerService = _dialogUserSubmitErrorHandlerService_; miqService = _miqService_; var responseResult = {content: ['the dialog']}; @@ -15,6 +20,7 @@ describe('dialogUserController', function() { }); spyOn(dialogFieldRefreshService, 'refreshField'); + spyOn(dialogUserSubmitErrorHandlerService, 'handleError'); spyOn(miqService, 'miqAjaxButton'); spyOn(miqService, 'redirectBack'); spyOn(miqService, 'sparkleOn'); @@ -24,6 +30,7 @@ describe('dialogUserController', function() { $controller = _$controller_('dialogUserController', { API: API, dialogFieldRefreshService: dialogFieldRefreshService, + dialogUserSubmitErrorHandlerService: dialogUserSubmitErrorHandlerService, miqService: miqService, dialogId: '1234', apiSubmitEndpoint: 'submit endpoint', @@ -201,34 +208,17 @@ describe('dialogUserController', function() { }); context('when the API call fails', function() { + var rejectionData; + beforeEach(function() { - var rejectionData = {data: {error: {message: "Failed! -One,Two"}}}; + rejectionData = {data: {error: {message: "Failed! -One,Two"}}}; spyOn(API, 'post').and.returnValue(Promise.reject(rejectionData)); - spyOn(window, 'clearFlash'); - spyOn(window, 'add_flash'); - }); - - it('turns off the sparkle', function(done) { - $controller.submitButtonClicked() - .catch(function() { - expect(miqService.sparkleOff).toHaveBeenCalled(); - done(); - }); - }); - - it('clears flash messages', function(done) { - $controller.submitButtonClicked() - .catch(function() { - expect(window.clearFlash).toHaveBeenCalled(); - done(); - }); }); - it('adds flash messages for each message after the -', function(done) { + it('delegates to the dialogUserSubmitErrorHandlerService', function(done) { $controller.submitButtonClicked() .catch(function() { - expect(window.add_flash).toHaveBeenCalledWith('One', 'error'); - expect(window.add_flash).toHaveBeenCalledWith('Two', 'error'); + expect(dialogUserSubmitErrorHandlerService.handleError).toHaveBeenCalledWith(rejectionData); done(); }); });