[fix] Reorder and catch proper response from API failed calls

This commit is contained in:
Jérôme Lebleu 2016-03-15 21:31:38 +01:00 committed by opi
parent 87a7ea0d4a
commit 0ca78c8769
2 changed files with 25 additions and 30 deletions

View file

@ -96,7 +96,22 @@
callback(data); callback(data);
}) })
.fail(function(xhr) { .fail(function(xhr) {
if (xhr.status == 200) { if (uri === '/postinstall') {
if (installing) {
interval = window.location.hostname === args.domain ? 20000 : 5000;
checkInstall = setInterval(function () {
c.checkInstall(function(isInstalled) {
if (isInstalled || typeof isInstalled === 'undefined') {
c.flash('success', y18n.t('installation_complete'));
clearInterval(checkInstall);
window.location.href = 'https://'+ window.location.hostname +'/yunohost/admin/';
}
});
}, interval);
} else {
c.flash('fail', y18n.t('error_occured'));
}
} else if (xhr.status == 200) {
// Fail with 200, WTF // Fail with 200, WTF
callback({}); callback({});
} else if (xhr.status == 401) { } else if (xhr.status == 401) {
@ -106,34 +121,13 @@
c.flash('fail', y18n.t('unauthorized')); c.flash('fail', y18n.t('unauthorized'));
c.redirect('#/login'); c.redirect('#/login');
} }
} else if (typeof xhr.responseJSON !== 'undefined') { } else if (xhr.status == 502) {
c.flash('fail', xhr.responseJSON.error); c.flash('fail', y18n.t('api_not_responding'));
} else if (typeof xhr.statusText !== 'undefined' && uri !== '/postinstall') { } else if (typeof xhr.responseText !== 'undefined') {
var errorMessage = xhr.status+' '+xhr.statusText; c.flash('fail', xhr.responseText);
// If some more text is available, display it to user.
if (typeof xhr.responseText !== 'undefined') {
errorMessage += ' - ' + xhr.responseText;
}
c.flash('fail', y18n.t('api_not_responding', [errorMessage]));
} else { } else {
if (uri === '/postinstall') { var errorMessage = xhr.status+' '+xhr.statusText;
if (installing) { c.flash('fail', y18n.t('error_server_unexpected', [errorMessage]));
interval = window.location.hostname === args.domain ? 20000 : 5000;
checkInstall = setInterval(function () {
c.checkInstall(function(isInstalled) {
if (isInstalled || typeof isInstalled === 'undefined') {
c.flash('success', y18n.t('installation_complete'));
clearInterval(checkInstall);
window.location.href = 'https://'+ window.location.hostname +'/yunohost/admin/';
}
});
}, interval);
} else {
c.flash('fail', y18n.t('error_occured'));
}
} else {
c.flash('fail', y18n.t('error_server'));
}
} }
if (uri !== '/postinstall') { if (uri !== '/postinstall') {
store.clear('slide'); store.clear('slide');
@ -364,4 +358,4 @@
}, },
}); });
})(); })();

View file

@ -55,13 +55,14 @@
"unauthorized" : "Unauthorized", "unauthorized" : "Unauthorized",
"error_occured" : "An error occured, try again", "error_occured" : "An error occured, try again",
"error_server" : "Server error", "error_server" : "Server error",
"error_server_unexpected" : "Unexpected server error (%s)",
"error_select_domain" : "You should indicate a domain", "error_select_domain" : "You should indicate a domain",
"error_modify_something" : "You should modify something", "error_modify_something" : "You should modify something",
"non_compatible_api" : "Non-compatible API", "non_compatible_api" : "Non-compatible API",
"warning_first_user" : "You probably need to <a href='#/users/create' class='alert-link'>create a user</a> first.", "warning_first_user" : "You probably need to <a href='#/users/create' class='alert-link'>create a user</a> first.",
"unknown_action" : "Unknown action %s", "unknown_action" : "Unknown action %s",
"unknown_argument" : "Unknown argument : %s", "unknown_argument" : "Unknown argument : %s",
"api_not_responding" : "API is not responding (Error : %s)", "api_not_responding" : "API is not responding",
"error_retrieve_feed" : "Could not retrieve feed : %s", "error_retrieve_feed" : "Could not retrieve feed : %s",
"confirm_delete" : "Are you sure you want to delete %s ?", "confirm_delete" : "Are you sure you want to delete %s ?",