luci-base: luci.js: make require() failures catcheable

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>
This commit is contained in:
Jo-Philipp Wich 2019-05-28 19:01:51 +02:00
parent 6edc057451
commit 5950b87dac
1 changed files with 4 additions and 6 deletions

View File

@ -658,7 +658,7 @@
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 "'));
@ -670,7 +670,7 @@
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(),
@ -721,7 +721,7 @@
.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 || '?');
}
@ -751,9 +751,7 @@
};
/* 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];
},