luci-base: network.js: simplify rpc error handling
authorJo-Philipp Wich <jo@mein.io>
Fri, 18 Oct 2019 09:49:13 +0000 (11:49 +0200)
committerJo-Philipp Wich <jo@mein.io>
Fri, 18 Oct 2019 09:49:13 +0000 (11:49 +0200)
Use L.resolveDefault() catch unexpected RPC replies and rename declared
rpc functions in a consistent manner.

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

index 30d508df23b90bea0426adcb5bf1c0fbcaa113ab..c5b0c3027d30bc0d9ddd1cfb61ec4fa0f75c8f47 100644 (file)
@@ -44,13 +44,13 @@ var iface_patterns_wireless = [
 
 var iface_patterns_virtual = [ ];
 
-var callLuciNetdevs = rpc.declare({
+var callLuciNetworkDevices = rpc.declare({
        object: 'luci',
        method: 'getNetworkDevices',
        expect: { '': {} }
 });
 
-var callLuciWifidevs = rpc.declare({
+var callLuciWirelessDevices = rpc.declare({
        object: 'luci',
        method: 'getWirelessDevices',
        expect: { '': {} }
@@ -62,11 +62,17 @@ var callLuciIfaddrs = rpc.declare({
        expect: { result: [] }
 });
 
-var callLuciBoardjson = rpc.declare({
+var callLuciBoardJSON = rpc.declare({
        object: 'luci',
        method: 'getBoardJSON'
 });
 
+var callLuciHostHints = rpc.declare({
+       object: 'luci',
+       method: 'getHostHints',
+       expect: { '': {} }
+});
+
 var callIwinfoAssoclist = rpc.declare({
        object: 'iwinfo',
        method: 'assoclist',
@@ -82,7 +88,7 @@ var callIwinfoScan = rpc.declare({
        expect: { results: [] }
 });
 
-var callNetworkInterfaceStatus = rpc.declare({
+var callNetworkInterfaceDump = rpc.declare({
        object: 'network.interface',
        method: 'dump',
        expect: { 'interface': [] }
@@ -94,88 +100,19 @@ var callNetworkDeviceStatus = rpc.declare({
        expect: { '': {} }
 });
 
-var callGetProtoHandlers = rpc.declare({
+var callNetworkProtoHandlers = rpc.declare({
        object: 'network',
        method: 'get_proto_handlers',
        expect: { '': {} }
 });
 
-var callGetHostHints = rpc.declare({
-       object: 'luci',
-       method: 'getHostHints',
-       expect: { '': {} }
-});
-
 var _init = null,
     _state = null,
     _protocols = {},
     _protospecs = {};
 
-function getInterfaceState(cache) {
-       return callNetworkInterfaceStatus().then(function(state) {
-               if (!Array.isArray(state))
-                       throw !1;
-               return state;
-       }).catch(function() {
-               return [];
-       });
-}
-
-function getDeviceState(cache) {
-       return callNetworkDeviceStatus().then(function(state) {
-               if (!L.isObject(state))
-                       throw !1;
-               return state;
-       }).catch(function() {
-               return {};
-       });
-}
-
-function getIfaddrState(cache) {
-       return callLuciIfaddrs().then(function(addrs) {
-               if (!Array.isArray(addrs))
-                       throw !1;
-               return addrs;
-       }).catch(function() {
-               return [];
-       });
-}
-
-function getNetdevState(cache) {
-       return callLuciNetdevs().then(function(state) {
-               if (!L.isObject(state))
-                       throw !1;
-               return state;
-       }).catch(function() {
-               return {};
-       });
-}
-
-function getWifidevState(cache) {
-       return callLuciWifidevs().then(function(state) {
-               if (!L.isObject(state))
-                       throw !1;
-               return state;
-       }).catch(function() {
-               return {};
-       });
-}
-
-function getBoardState(cache) {
-       return callLuciBoardjson().then(function(state) {
-               if (!L.isObject(state))
-                       throw !1;
-               return state;
-       }).catch(function() {
-               return {};
-       });
-}
-
 function getProtocolHandlers(cache) {
-       return callGetProtoHandlers().then(function(protos) {
-               if (!L.isObject(protos))
-                       throw !1;
-
+       return callNetworkProtoHandlers().then(function(protos) {
                /* Register "none" protocol */
                if (!protos.hasOwnProperty('none'))
                        Object.assign(protos, { none: { no_device: false } });
@@ -199,16 +136,6 @@ function getProtocolHandlers(cache) {
        });
 }
 
-function getHostHints(cache) {
-       return callGetHostHints().then(function(hosts) {
-               if (!L.isObject(hosts))
-                       throw !1;
-               return hosts;
-       }).catch(function() {
-               return {};
-       });
-}
-
 function getWifiStateBySid(sid) {
        var s = uci.get('wireless', sid);
 
@@ -433,10 +360,15 @@ function maskToPrefix(mask, v6) {
 function initNetworkState(refresh) {
        if (_state == null || refresh) {
                _init = _init || Promise.all([
-                       getInterfaceState(), getDeviceState(), getBoardState(),
-                       getIfaddrState(), getNetdevState(), getWifidevState(),
-                       getHostHints(), getProtocolHandlers(),
-                       uci.load('network'), uci.load('wireless'), uci.load('luci')
+                       L.resolveDefault(callNetworkInterfaceDump(), []),
+                       L.resolveDefault(callNetworkDeviceStatus(), {}),
+                       L.resolveDefault(callLuciBoardJSON(), {}),
+                       L.resolveDefault(callLuciIfaddrs(), []),
+                       L.resolveDefault(callLuciNetworkDevices(), {}),
+                       L.resolveDefault(callLuciWirelessDevices(), {}),
+                       L.resolveDefault(callLuciHostHints(), {}),
+                       getProtocolHandlers(),
+                       uci.load(['network', 'wireless', 'luci'])
                ]).then(function(data) {
                        var netifd_ifaces = data[0],
                            netifd_devs   = data[1],