From 446077ae00a2cf8a062fd858a49b0a64efa389d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Tue, 15 Mar 2016 21:31:38 +0100 Subject: [PATCH] [fix] Reorder and catch proper response from API failed calls --- src/js/yunohost/helpers.js | 52 +++++++++++++++++--------------------- src/locales/en.json | 3 ++- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/js/yunohost/helpers.js b/src/js/yunohost/helpers.js index 30cd6978..e166401d 100644 --- a/src/js/yunohost/helpers.js +++ b/src/js/yunohost/helpers.js @@ -92,7 +92,22 @@ callback(data); }) .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 callback({}); } else if (xhr.status == 401) { @@ -102,34 +117,13 @@ c.flash('fail', y18n.t('unauthorized')); c.redirect('#/login'); } - } else if (typeof xhr.responseJSON !== 'undefined') { - c.flash('fail', xhr.responseJSON.error); - } else if (typeof xhr.statusText !== 'undefined' && uri !== '/postinstall') { - var errorMessage = xhr.status+' '+xhr.statusText; - // 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 if (xhr.status == 502) { + c.flash('fail', y18n.t('api_not_responding')); + } else if (typeof xhr.responseText !== 'undefined') { + c.flash('fail', xhr.responseText); } else { - 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 { - c.flash('fail', y18n.t('error_server')); - } + var errorMessage = xhr.status+' '+xhr.statusText; + c.flash('fail', y18n.t('error_server_unexpected', [errorMessage])); } if (uri !== '/postinstall') { store.clear('slide'); @@ -361,4 +355,4 @@ }, }); -})(); \ No newline at end of file +})(); diff --git a/src/locales/en.json b/src/locales/en.json index 122f5bc6..d30ae9b2 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -55,13 +55,14 @@ "unauthorized" : "Unauthorized", "error_occured" : "An error occured, try again", "error_server" : "Server error", + "error_server_unexpected" : "Unexpected server error (%s)", "error_select_domain" : "You should indicate a domain", "error_modify_something" : "You should modify something", "non_compatible_api" : "Non-compatible API", "warning_first_user" : "You probably need to create a user first.", "unknown_action" : "Unknown action %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", "confirm_delete" : "Are you sure you want to delete %s ?",