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 1/3] [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 ?", From 9ec25c3a77ea1e47f9c2837f494f29eab470b6e1 Mon Sep 17 00:00:00 2001 From: opi Date: Wed, 16 Mar 2016 17:49:02 +0100 Subject: [PATCH 2/3] [fix] User create form POST on same url to prevent strange redirection and form cleaning. --- src/js/yunohost/controllers/users.js | 2 +- src/views/user/user_create.ms | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/yunohost/controllers/users.js b/src/js/yunohost/controllers/users.js index feafa742..c40ecf44 100644 --- a/src/js/yunohost/controllers/users.js +++ b/src/js/yunohost/controllers/users.js @@ -23,7 +23,7 @@ }); // Create user (POST) - app.post('#/users', function (c) { + app.post('#/users/create', function (c) { if (c.params['password'] == c.params['confirmation']) { if (c.params['password'].length < 4) { c.flash('fail', y18n.t('password_too_short')); diff --git a/src/views/user/user_create.ms b/src/views/user/user_create.ms index 07c5be1d..bd3faa57 100644 --- a/src/views/user/user_create.ms +++ b/src/views/user/user_create.ms @@ -7,7 +7,7 @@
-
+
From 5850920398bd72da618b1f54c5572ee6b23d4e99 Mon Sep 17 00:00:00 2001 From: opi Date: Wed, 16 Mar 2016 17:51:51 +0100 Subject: [PATCH 3/3] [fix] Prevent data loss on form errors. Add some comments too. --- src/js/yunohost/helpers.js | 55 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/js/yunohost/helpers.js b/src/js/yunohost/helpers.js index e166401d..86990b40 100644 --- a/src/js/yunohost/helpers.js +++ b/src/js/yunohost/helpers.js @@ -92,6 +92,8 @@ callback(data); }) .fail(function(xhr) { + // Postinstall is a custom case, we have to wait that + // operation is done before doing anything if (uri === '/postinstall') { if (installing) { interval = window.location.hostname === args.domain ? 20000 : 5000; @@ -107,27 +109,42 @@ } else { c.flash('fail', y18n.t('error_occured')); } - } else if (xhr.status == 200) { - // Fail with 200, WTF - callback({}); - } else if (xhr.status == 401) { - if (uri === '/login') { - c.flash('fail', y18n.t('wrong_password')); - } else { - c.flash('fail', y18n.t('unauthorized')); - c.redirect('#/login'); - } - } 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 { - var errorMessage = xhr.status+' '+xhr.statusText; - c.flash('fail', y18n.t('error_server_unexpected', [errorMessage])); } - if (uri !== '/postinstall') { + // Regular errors + else { + if (xhr.status == 200) { + // Fail with 200, WTF + callback({}); + } + // Unauthorized or wrong password + else if (xhr.status == 401) { + if (uri === '/login') { + c.flash('fail', y18n.t('wrong_password')); + } else { + c.flash('fail', y18n.t('unauthorized')); + c.redirect('#/login'); + } + } + // 502 Bad gateway means API is down + else if (xhr.status == 502) { + c.flash('fail', y18n.t('api_not_responding')); + } + // More verbose error messages first + else if (typeof xhr.responseText !== 'undefined') { + c.flash('fail', xhr.responseText); + } + // Return HTTP error code at least + else { + var errorMessage = xhr.status+' '+xhr.statusText; + c.flash('fail', y18n.t('error_server_unexpected', [errorMessage])); + } + + // Remove loader if any + $('div.loader').remove(); + + // Force scrollTop on page load + $('html, body').scrollTop(0); store.clear('slide'); - c.redirect(store.get('path-1')); } }); };