Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
cleaner backend error messages (bug 1034106)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngokevin committed Jul 8, 2014
1 parent 93226d9 commit b13d754
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/media/js/utils_local.js
@@ -1,15 +1,26 @@
define('utils_local', ['jquery', 'log'], function($, log) {
var console = log('utils_local');

var build_localized_field = function(name) {
function build_error_msg(error) {
// {'slug': ['This field is required.']} to 'This field is required.'.
var errs = [];
error = JSON.parse(error);
error_keys = Object.keys(error);
for (var i = 0; i < error_keys.length; i++) {
errs.push(error[error_keys[i]][0]);
}
return errs.join(' ');
}

function build_localized_field(name) {
var data = {};
$('.localized[data-name="' + name + '"]').each(function(i, field) {
data[this.getAttribute('data-lang')] = this.value;
});
return data;
};

var items = function(obj) {
function items(obj) {
// Like Python's dict.items().
var items = [];
var keys = Object.keys(obj);
Expand All @@ -23,6 +34,7 @@ define('utils_local', ['jquery', 'log'], function($, log) {
};

return {
build_error_msg: build_error_msg,
build_localized_field: build_localized_field,
items: items
};
Expand Down
6 changes: 3 additions & 3 deletions src/media/js/views/create.js
@@ -1,6 +1,6 @@
define('views/create',
['feed_previews', 'fields_transonic', 'forms_transonic', 'jquery', 'jquery.fakefilefield', 'l10n', 'log', 'notification', 'requests', 'templates', 'urls', 'utils', 'z'],
function(feed_previews, fields_transonic, forms_transonic, $, fakefilefield, l10n, log, notification, requests, nunjucks, urls, utils, z) {
['feed_previews', 'fields_transonic', 'forms_transonic', 'jquery', 'jquery.fakefilefield', 'l10n', 'log', 'notification', 'requests', 'templates', 'urls', 'utils', 'utils_local', 'z'],
function(feed_previews, fields_transonic, forms_transonic, $, fakefilefield, l10n, log, notification, requests, nunjucks, urls, utils, utils_local, z) {

'use strict';
var gettext = l10n.gettext;
Expand All @@ -11,7 +11,7 @@ define('views/create',
z.page.trigger('navigate', [urls.reverse('edit', [feed_type, feed_element.slug])]);
notification.notification({message: success_msg});
}).fail(function(error) {
notification.notification({message: error});
notification.notification({message: utils_local.build_error_msg(error)});
$btn.html(gettext('Submit')).removeAttr('disabled');
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/media/js/views/edit.js
Expand Up @@ -9,7 +9,7 @@ define('views/edit',
notification.notification({message: success_msg});
resetButton($btn);
}).fail(function(error) {
notification.notification({message: error});
notification.notification({message: utils_local.build_error_msg(error)});
resetButton($btn);
});
}
Expand Down

0 comments on commit b13d754

Please sign in to comment.