Skip to content

Commit

Permalink
Refactor API failure logic into DialogUserSubmitErrorHandlerService
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed Jun 28, 2018
1 parent f895305 commit 87796af
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
}]);
Original file line number Diff line number Diff line change
@@ -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']};
Expand All @@ -15,6 +20,7 @@ describe('dialogUserController', function() {
});

spyOn(dialogFieldRefreshService, 'refreshField');
spyOn(dialogUserSubmitErrorHandlerService, 'handleError');
spyOn(miqService, 'miqAjaxButton');
spyOn(miqService, 'redirectBack');
spyOn(miqService, 'sparkleOn');
Expand All @@ -24,6 +30,7 @@ describe('dialogUserController', function() {
$controller = _$controller_('dialogUserController', {
API: API,
dialogFieldRefreshService: dialogFieldRefreshService,
dialogUserSubmitErrorHandlerService: dialogUserSubmitErrorHandlerService,
miqService: miqService,
dialogId: '1234',
apiSubmitEndpoint: 'submit endpoint',
Expand Down Expand Up @@ -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();
});
});
Expand Down

0 comments on commit 87796af

Please sign in to comment.