Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch 400 error and display it in flash message in GOD #4951

Merged
merged 4 commits into from Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -125,13 +125,34 @@ function mainCustomButtonFormController(API, miqService, $q, $http) {
};

vm.saveClicked = function() {
miqService.sparkleOn();
Copy link
Contributor

@himdel himdel Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and we probably need miqService.miqFlashClear(); in both saveClicked and addClicked.

Because otherwise:

  1. fail the validation with duplicate name
    => Name has already been taken appears

  2. fix the name (and also edit the description)

  3. fail the validation with duplicate desciption
    => Desciption has already been taken appears

  4. you're seeing both flash messages, but one is obsolete now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(or you could just replace add_flash with miqService.miqFlash, but that would not work when both are supposed to be shown, so you'd have to merge the messages into one)

miqService.miqFlashClear(); // remove previous messages
var saveMsg = sprintf(__('%s "%s" has been successfully saved.'), vm.entity, vm.customButtonModel.name);
vm.saveWithAPI('put', '/api/custom_buttons/' + vm.customButtonRecordId, vm.prepSaveObject(), saveMsg);
himdel marked this conversation as resolved.
Show resolved Hide resolved
return API.put('/api/custom_buttons/' + vm.customButtonRecordId, vm.prepSaveObject(), {skipErrors: [400]})
.then(function() {
miqService.redirectBack(saveMsg, 'success', vm.redirectUrl);
})
.catch(handleErrorMessages);
};

vm.addClicked = function() {
miqService.sparkleOn();
miqService.miqFlashClear(); // remove previous messages
var saveMsg = sprintf(__('%s "%s" has been successfully added.'), vm.entity, vm.customButtonModel.name);
vm.saveWithAPI('post', '/api/custom_buttons/', vm.prepSaveObject(), saveMsg);
return API.post('/api/custom_buttons/', vm.prepSaveObject(), {skipErrors: [400]})
.then(function(response) {
if (vm.customButtonGroupRecordId) {
var saveMsgBtnInGrp = sprintf(__('%s "%s" has been successfully added under the selected button group.'), vm.entity, vm.customButtonModel.name);
return $http.post('/generic_object_definition/add_button_in_group/' + vm.customButtonGroupRecordId + '?button_id=' + response.results[0].id)
.then(function() {
miqService.redirectBack(saveMsgBtnInGrp, 'success', vm.redirectUrl);
})
.catch(miqService.handleFailure);
} else {
miqService.redirectBack(saveMsg, 'success', vm.redirectUrl);
}
})
.catch(handleErrorMessages);
};

vm.prepSaveObject = function() {
Expand Down Expand Up @@ -184,26 +205,27 @@ function mainCustomButtonFormController(API, miqService, $q, $http) {
};
};

vm.saveWithAPI = function(method, url, saveObject, saveMsg) {
miqService.sparkleOn();

if (vm.customButtonGroupRecordId) {
var saveCustomButtonPromise = API[method](url, saveObject);
var saveMsgBtnInGrp = sprintf(__('%s "%s" has been successfully added under the selected button group.'), vm.entity, vm.customButtonModel.name);

saveCustomButtonPromise.then(function(response) {
$http.post('/generic_object_definition/add_button_in_group/' + vm.customButtonGroupRecordId + '?button_id=' + response.results[0].id)
.then(miqService.redirectBack.bind(vm, saveMsgBtnInGrp, 'success', vm.redirectUrl))
.catch(miqService.handleFailure);
// private functions
function handleErrorMessages(error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing miqSparkleOff()

it never stops sparkling on error

miqSparkleOff();
if (error.status === 400) {
var errorMessages = error.data.error.message.split(',');
errorMessages.forEach(function(message) {
if (message.includes("Name has already been taken")) {
add_flash(__("Name has already been taken"), "error");
return Promise.reject();
} else if (message.includes("Description has already been taken")) {
add_flash(__("Description has already been taken"), "error");
return Promise.reject();
} else {
return miqService.handleFailure();
}
});
} else {
API[method](url, saveObject)
.then(miqService.redirectBack.bind(vm, saveMsg, 'success', vm.redirectUrl))
.catch(miqService.handleFailure);
return miqService.handleFailure();
}
};
}

// private functions
function getCustomButtonFormData(response) {
Object.assign(vm.customButtonModel, response);

Expand Down
@@ -0,0 +1,70 @@
describe('main-custom-button-form-component', function() {
var $componentController, vm, miqService, API, $scope;
beforeEach(module('ManageIQ'));
beforeEach(inject(function (_$componentController_, _API_, _$httpBackend_, _miqService_, $rootScope, $q) {
$componentController = _$componentController_;
API = _API_;
miqService = _miqService_;
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;

var bindings = {genericObjectDefinitionRecordId: '1', customButtonGroupRecordId: '2', customButtonRecordId: '3', redirectUrl: '/redirect/url'};
vm = $componentController("mainCustomButtonFormComponent", null, bindings);
var deferred = $q.defer();
spyOn(API, 'get').and.callFake(function() {return deferred.promise;});
var domainsResponse = {
data:{
distinct_instances_across_domains: ["Automation", "Event", "GenericObject", "MiqEvent", "Request", "parse_automation_request", "parse_event_stream", "parse_provider_category"]
}
};
$httpBackend.whenGET('/generic_object_definition/retrieve_distinct_instances_across_domains').respond(domainsResponse);
vm.$onInit();
$httpBackend.flush();
}));

describe('#saveClick', function() {
beforeEach(function(){
spyOn(window, 'add_flash');
});

it('catch 400 and call add_flash if error is name/description taken', function(done) {
var rejectionData = {status: 400, data: {error: {message: "Name has already been taken, Description has already been taken"}}};
spyOn(API, 'put').and.returnValue(Promise.reject(rejectionData));
vm.saveClicked()
.then( function() {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");
done();
})
.catch(function() {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");

done();
});
});
});

describe('#addClick', function() {
beforeEach(function(){
spyOn(window, 'add_flash');
spyOn(miqService, 'handleFailure').and.callFake(function() {debugger;return Promise.reject()});
});

it('catch 400 and call add_flash', function(done) {
var rejectionData = {status: 400, data: {error: {message: "Name has already been taken, Description has already been taken"}}};
spyOn(API, 'post').and.returnValue(Promise.reject(rejectionData));
vm.addClicked()
.then( function() {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");
done();
})
.catch(function () {
expect(add_flash).toHaveBeenCalledWith("Name has already been taken", "error");
expect(add_flash).toHaveBeenCalledWith("Description has already been taken", "error");
done();
});
});
});
});