Improve error handling for failed AJAX requests

[MAILPOET-929]
This commit is contained in:
Pavel Dohnal
2017-06-22 13:35:55 +01:00
parent f874ae6ca8
commit ec3e88e658

View File

@ -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 = {};