summaryrefslogtreecommitdiff
path: root/luci2
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2014-01-31 21:05:12 +0000
committerJo-Philipp Wich <jow@openwrt.org>2014-01-31 21:05:12 +0000
commitdd7878e8d48e2a3606a97265b9b83ac0d80cda72 (patch)
treee8beb240615ee6ba5d4a036f1e5e87f939a5eb9f /luci2
parent5e77a7735b0594f50b5ce6e4f3d2b2cb6bf5d546 (diff)
downloadluci2-ui-dd7878e8d48e2a3606a97265b9b83ac0d80cda72.tar.gz
luci2: properly handle failed ubus calls in LuCI2.rpc to ensure that chained then() callbacks are invoked with the expected default values
Diffstat (limited to 'luci2')
-rw-r--r--luci2/htdocs/luci2/luci2.js35
1 files changed, 20 insertions, 15 deletions
diff --git a/luci2/htdocs/luci2/luci2.js b/luci2/htdocs/luci2/luci2.js
index 6c7db67..4e87218 100644
--- a/luci2/htdocs/luci2/luci2.js
+++ b/luci2/htdocs/luci2/luci2.js
@@ -505,43 +505,48 @@ function LuCI2()
data: JSON.stringify(req),
dataType: 'json',
type: 'POST',
- timeout: _luci2.globals.timeout
- }).then(cb);
+ timeout: _luci2.globals.timeout,
+ _rpc_req: req
+ }).then(cb, cb);
},
_list_cb: function(msg)
{
+ var list = msg.result;
+
/* verify message frame */
- if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id)
- throw 'Invalid JSON response';
+ if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !$.isArray(list))
+ list = [ ];
- return msg.result;
+ return $.Deferred().resolveWith(this, [ list ]);
},
_call_cb: function(msg)
{
var data = [ ];
var type = Object.prototype.toString;
+ var reqs = this._rpc_req;
- if (!$.isArray(msg))
+ if (!$.isArray(reqs))
+ {
msg = [ msg ];
+ reqs = [ reqs ];
+ }
for (var i = 0; i < msg.length; i++)
{
- /* verify message frame */
- if (typeof(msg[i]) != 'object' || msg[i].jsonrpc != '2.0' || !msg[i].id)
- throw 'Invalid JSON response';
-
/* fetch related request info */
- var req = _luci2.rpc._requests[msg[i].id];
+ var req = _luci2.rpc._requests[reqs[i].id];
if (typeof(req) != 'object')
throw 'No related request for JSON response';
/* fetch response attribute and verify returned type */
var ret = undefined;
- if ($.isArray(msg[i].result) && msg[i].result[0] == 0)
- ret = (msg[i].result.length > 1) ? msg[i].result[1] : msg[i].result[0];
+ /* verify message frame */
+ if (typeof(msg[i]) == 'object' && msg[i].jsonrpc == '2.0')
+ if ($.isArray(msg[i].result) && msg[i].result[0] == 0)
+ ret = (msg[i].result.length > 1) ? msg[i].result[1] : msg[i].result[0];
if (req.expect)
{
@@ -572,10 +577,10 @@ function LuCI2()
data = ret;
/* delete request object */
- delete _luci2.rpc._requests[msg[i].id];
+ delete _luci2.rpc._requests[reqs[i].id];
}
- return data;
+ return $.Deferred().resolveWith(this, [ data ]);
},
list: function()