luci-app-attendedsysupgrade: add timeout and catch
authorPaul Spooren <mail@aparcar.org>
Mon, 9 Aug 2021 18:45:58 +0000 (08:45 -1000)
committerPaul Spooren <mail@aparcar.org>
Thu, 26 Aug 2021 07:52:00 +0000 (21:52 -1000)
If the upgrade server API does not respond, show an error message.
Fix #5222

While at it, minimal code linting

Signed-off-by: Paul Spooren <mail@aparcar.org>
(cherry picked from commit ff24b78c807a52a0f9afb62ba0aba1085d9ad045)

applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade/overview.js

index 3ee99684da11f3ee34632c7619c81e6092ed2a3e..802868ad0d00b7e93a24dc4062b1227e46874014 100644 (file)
@@ -66,13 +66,13 @@ function install_sysupgrade(url, keep, sha256) {
                                                ]);
                                        } else {
                                                displayStatus('warning spinning', E('p', _('Installing the sysupgrade. Do not unpower device!')));
-                                                       L.resolveDefault(callUpgradeStart(keep), {}).then(response => {
-                                                               if (keep) {
-                                                                       ui.awaitReconnect(window.location.host);
-                                                               } else {
-                                                                       ui.awaitReconnect('192.168.1.1', 'openwrt.lan');
-                                                               }
-                                                       });
+                                               L.resolveDefault(callUpgradeStart(keep), {}).then(response => {
+                                                       if (keep) {
+                                                               ui.awaitReconnect(window.location.host);
+                                                       } else {
+                                                               ui.awaitReconnect('192.168.1.1', 'openwrt.lan');
+                                                       }
+                                               });
                                        }
                                });
                });
@@ -228,7 +228,9 @@ function check_sysupgrade(server_url, current_version, target, board_name, packa
        var advanced_mode = uci.get_first('attendedsysupgrade', 'client', 'advanced_mode') || 0;
        var candidates = [];
 
-       fetch(server_url + "/api/latest")
+       request.get(server_url + "/api/latest", {
+                       timeout: 8000
+               })
                .then(response => response.json())
                .then(response => {
                        if (current_version == "SNAPSHOT") {
@@ -320,6 +322,20 @@ function check_sysupgrade(server_url, current_version, target, board_name, packa
                                        ])
                                ]);
                        }
+               })
+               .catch(error => {
+                       ui.showModal(_('Error connecting to upgrade server'), [
+                               E('p', {}, _('Could not reach API at "%s". Please try again later.'.format(server_url))),
+                               E('pre', {}, error),
+                               E('div', {
+                                       'class': 'right'
+                               }, [
+                                       E('div', {
+                                               'class': 'btn',
+                                               'click': ui.hideModal
+                                       }, _('Close'))
+                               ])
+                       ]);
                });
 }