luci-base: luci.js: make require() failures catcheable
authorJo-Philipp Wich <jo@mein.io>
Tue, 28 May 2019 17:01:51 +0000 (19:01 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 7 Jul 2019 13:36:25 +0000 (15:36 +0200)
Refactor L.require() to use L.raise() instead of L.error() to signal
class loading failures. This allows callers to handle class loading
errors in a graceful manner.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/luci.js

index 6332790c77b726408f786b56f32a94b27570eef4..853bbfc05d5de5c2d954b3ece976e31b14d5300c 100644 (file)
                        if (classes[name] != null) {
                                /* Circular dependency */
                                if (from.indexOf(name) != -1)
-                                       L.error('DependencyError',
+                                       L.raise('DependencyError',
                                                'Circular dependency: class "%s" depends on "%s"',
                                                name, from.join('" which depends on "'));
 
 
                        var compileClass = function(res) {
                                if (!res.ok)
-                                       L.error('NetworkError',
+                                       L.raise('NetworkError',
                                                'HTTP error %d while loading class file "%s"', res.status, url);
 
                                var source = res.text(),
                                                                .format(args, source, res.url));
                                        }
                                        catch (error) {
-                                               L.error('SyntaxError', '%s\n  in %s:%s',
+                                               L.raise('SyntaxError', '%s\n  in %s:%s',
                                                        error.message, res.url, error.lineNumber || '?');
                                        }
 
                        };
 
                        /* Request class file */
-                       classes[name] = Request.get(url, { cache: true })
-                               .then(compileClass)
-                               .catch(L.error);
+                       classes[name] = Request.get(url, { cache: true }).then(compileClass);
 
                        return classes[name];
                },