diff --git a/assets/js/src/ajax.js b/assets/js/src/ajax.js index 0f7e4355f9..24c5bf7ff8 100644 --- a/assets/js/src/ajax.js +++ b/assets/js/src/ajax.js @@ -1,3 +1,18 @@ +function requestFailed(xhr) { + if (xhr.responseJSON) { + return xhr.responseJSON; + } + var message = "An error has happened while performing a request, the server has responded with response code " + + xhr.status; + return { + errors: [ + { + message: message + } + ] + } +} + define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) { MailPoet.Ajax = { @@ -60,9 +75,7 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, 'json' ).then(function(data) { return data; - }, function(xhr) { - return xhr.responseJSON; - }); + }, requestFailed); // clear options this.options = {};