From 670d6768a25e5b32e0a66ad3d2e75f17776ded72 Mon Sep 17 00:00:00 2001 From: opi Date: Wed, 16 Mar 2016 17:51:51 +0100 Subject: [PATCH] [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 26caabf3..bee3b9e2 100644 --- a/src/js/yunohost/helpers.js +++ b/src/js/yunohost/helpers.js @@ -96,6 +96,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; @@ -111,27 +113,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')); } }); };