Show error in adding associations

https://fedorahosted.org/freeipa/ticket/1628
This commit is contained in:
Petr Vobornik 2011-08-17 15:06:41 +02:00 committed by Endi S. Dewata
parent be7de56e5d
commit 37cef6f60d
2 changed files with 93 additions and 23 deletions

View File

@ -331,6 +331,7 @@ IPA.command = function(spec) {
} }
function success_handler(data, text_status, xhr) { function success_handler(data, text_status, xhr) {
var failed;
if (!data) { if (!data) {
// error_handler() calls IPA.hide_activity_icon() // error_handler() calls IPA.hide_activity_icon()
@ -347,7 +348,18 @@ IPA.command = function(spec) {
message: data.error.message, message: data.error.message,
data: data data: data
}); });
} else if ((failed = that.get_failed(that, data.result, text_status, xhr)) &&
!failed.is_empty()) {
var message = failed.errors[0].message;
for(var i = 1; i < failed.errors.length; i++) {
message += '\n' + failed.errors[i].message;
}
error_handler.call(this, xhr, text_status, /* error_thrown */ {
name: failed.errors[0].name,
message: message,
data: data
});
} else if (that.on_success) { } else if (that.on_success) {
IPA.hide_activity_icon(); IPA.hide_activity_icon();
//custom success handling, maintaining AJAX call's context //custom success handling, maintaining AJAX call's context
@ -379,6 +391,27 @@ IPA.command = function(spec) {
$.ajax(request); $.ajax(request);
}; };
that.get_failed = function(command, result, text_status, xhr) {
var errors = IPA.error_list();
if(result && result.failed) {
for(var association in result.failed) {
for(var member_name in result.failed[association]) {
var member = result.failed[association][member_name];
for(var i = 0; i < member.length; i++) {
if(member[i].length > 1) {
var name = 'IPA Error';
var message = member[i][1];
if(member[i][0])
message = member[i][0] + ': ' + message;
errors.add(command, name, message, text_status);
}
}
}
}
}
return errors;
};
that.to_json = function() { that.to_json = function() {
var json = {}; var json = {};
@ -417,7 +450,7 @@ IPA.batch_command = function (spec) {
var that = IPA.command(spec); var that = IPA.command(spec);
that.commands = []; that.commands = [];
that.errors = []; that.errors = IPA.error_list();
that.error_message = spec.error_message || (IPA.messages.dialogs ? that.error_message = spec.error_message || (IPA.messages.dialogs ?
IPA.messages.dialogs.batch_error_message : 'Some operations failed.'); IPA.messages.dialogs.batch_error_message : 'Some operations failed.');
that.show_error = typeof spec.show_error == 'undefined' ? that.show_error = typeof spec.show_error == 'undefined' ?
@ -434,21 +467,8 @@ IPA.batch_command = function (spec) {
} }
}; };
var clear_errors = function() {
that.errors = [];
};
var add_error = function(command, name, message, status) {
that.errors.push({
command: command,
name: name,
message: message,
status: status
});
};
that.execute = function() { that.execute = function() {
clear_errors(); that.errors.clear();
IPA.command({ IPA.command({
name: that.name, name: that.name,
@ -465,12 +485,13 @@ IPA.batch_command = function (spec) {
var name = ''; var name = '';
var message = ''; var message = '';
var failed;
if (!result) { if (!result) {
name = 'Internal Error '+xhr.status; name = 'Internal Error '+xhr.status;
message = result ? xhr.statusText : "Internal error"; message = result ? xhr.statusText : "Internal error";
add_error(command, name, message, text_status); that.errors.add(command, name, message, text_status);
if (command.on_error) command.on_error.call( if (command.on_error) command.on_error.call(
this, this,
@ -486,7 +507,7 @@ IPA.batch_command = function (spec) {
name = 'IPA Error ' + (result.error.code || ''); name = 'IPA Error ' + (result.error.code || '');
message = result.error.message || result.error; message = result.error.message || result.error;
add_error(command, name, message, text_status); that.errors.add(command, name, message, text_status);
if (command.on_error) command.on_error.call( if (command.on_error) command.on_error.call(
this, this,
@ -494,7 +515,28 @@ IPA.batch_command = function (spec) {
text_status, text_status,
{ {
name: name, name: name,
message: message message: message,
data: result
}
);
} else if ((failed = that.get_failed(command, result, text_status, xhr)) &&
!failed.is_empty()) {
that.errors.add_range(failed);
message = failed.errors[0].message;
for(var j = 1; j < failed.errors.length; j++) {
message += '\n' + failed.errors[j].message;
}
if (command.on_error) command.on_error.call(
this,
xhr,
text_status,
{
name: failed.errors[0].name,
message: message,
data: result
} }
); );
@ -503,7 +545,7 @@ IPA.batch_command = function (spec) {
} }
} }
//check for partial errors and show error dialog //check for partial errors and show error dialog
if(that.show_error && that.errors.length > 0) { if(that.show_error && that.errors.errors.length > 0) {
var dialog = IPA.error_dialog({ var dialog = IPA.error_dialog({
xhr: xhr, xhr: xhr,
text_status: text_status, text_status: text_status,
@ -513,7 +555,7 @@ IPA.batch_command = function (spec) {
message: that.error_message message: that.error_message
}, },
command: that, command: that,
errors: that.errors, errors: that.errors.errors,
visible_buttons: ['ok'] visible_buttons: ['ok']
}); });
dialog.open(); dialog.open();
@ -685,7 +727,7 @@ IPA.error_dialog = function(spec) {
} }
$('<p/>', { $('<p/>', {
html: that.error_thrown.message html: that.error_thrown.message.replace('\n', '<br />')
}).appendTo(that.container); }).appendTo(that.container);
if(that.errors && that.errors.length > 0) { if(that.errors && that.errors.length > 0) {
@ -785,3 +827,31 @@ IPA.error_dialog = function(spec) {
return that; return that;
}; };
IPA.error_list = function() {
var that = {};
that.clear = function() {
that.errors = [];
};
that.add = function(command, name, message, status) {
that.errors.push({
command: command,
name: name,
message: message,
status: status
});
};
that.add_range = function(error_list) {
that.errors = that.errors.concat(error_list.errors);
};
that.is_empty = function () {
return that.errors.length === 0;
};
that.clear();
return that;
};

View File

@ -60,7 +60,7 @@ test("Testing serial_associator().", function() {
'Checking IPA.command() parameter: primary key' 'Checking IPA.command() parameter: primary key'
); );
that.on_success(); that.on_success({});
}; };
return that; return that;
@ -115,7 +115,7 @@ test("Testing bulk_associator().", function() {
'Checking IPA.command() parameter: options[\""+params.other_entity+"\"]' 'Checking IPA.command() parameter: options[\""+params.other_entity+"\"]'
); );
that.on_success(); that.on_success({});
}; };
return that; return that;