added common API error handler function, closes #1066

pull/1108/head
Justin Richer 2016-07-14 20:54:18 -04:00
parent ee537c404b
commit efc1c3c8bd
3 changed files with 52 additions and 16 deletions

View File

@ -1013,6 +1013,47 @@ var AppRouter = Backbone.Router.extend({
// this gets init after the templates load
var app = null;
var apiErrorHandler = function(msg) {
return function(model, response, options) {
if (msg.log) {
console.log(msg.log);
}
var header = "";
var message = "";
message += $.t('error.message');
if (response.responseJSON) {
header += response.responseJSON.error;
message += response.responseJSON.error_description;
}
if (msg.message) {
message += $.t(options.message);
}
if (response.status == 401) {
// unauthorized means the session probably timed out, prompt the user to reload the page
message += $('#tmpl-page-reload').html();
}
$('#modalAlert').i18n();
$('#modalAlert div.modal-header').html(header);
$('#modalAlert .modal-body').html(message);
$('#modalAlert .modal-body .page-reload').on('click', function(event) {
event.preventDefault();
window.location.reload(true);
});
$('#modalAlert').modal({
'backdrop': 'static',
'keyboard': true,
'show': true
});
};
};
// main
$(function () {

View File

@ -1061,22 +1061,7 @@ var ClientFormView = Backbone.View.extend({
app.clientList.add(_self.model);
app.navigate('admin/clients', {trigger:true});
},
error:function (error, response) {
console.log("An error occurred when saving a client");
//Pull out the response text.
var responseJson = JSON.parse(response.responseText);
//Display an alert with an error message
$('#modalAlert div.modal-header').html(responseJson.error);
$('#modalAlert div.modal-body').html(responseJson.error_description);
$("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
"backdrop" : "static",
"keyboard" : true,
"show" : true // ensure the modal is shown immediately
});
}
error:apiErrorHandler({log: "An error occurred when saving a client"})
});
return false;

View File

@ -110,3 +110,13 @@
<dd><%- value %></dd>
</script>
<!-- error box -->
<script type="text/html" id="tmpl-page-reload">
<p>
<span data-i18n="error.reload">It looks like you're not logged in. Reload the page to try again.</span>
<button class="btn btn-primary page-reload"><i class="icon-refresh icon-white"></i> <span data-i18n="error.reload-button">Reload</span></button>
</p>
</script>