luci-base: rpc.js: revamp error handling, add interceptor support
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / rpc.js
1 'use strict';
2
3 var rpcRequestID = 1,
4 rpcSessionID = L.env.sessionid || '00000000000000000000000000000000',
5 rpcBaseURL = L.url('admin/ubus'),
6 rpcInterceptorFns = [];
7
8 return L.Class.extend({
9 call: function(req, cb) {
10 var q = '';
11
12 if (Array.isArray(req)) {
13 if (req.length == 0)
14 return Promise.resolve([]);
15
16 for (var i = 0; i < req.length; i++)
17 q += '%s%s.%s'.format(
18 q ? ';' : '/',
19 req[i].params[1],
20 req[i].params[2]
21 );
22 }
23 else {
24 q += '/%s.%s'.format(req.params[1], req.params[2]);
25 }
26
27 return L.Request.post(rpcBaseURL + q, req, {
28 timeout: (L.env.rpctimeout || 5) * 1000,
29 credentials: true
30 }).then(cb);
31 },
32
33 handleListReply: function(req, msg) {
34 var list = msg.result;
35
36 /* verify message frame */
37 if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !Array.isArray(list))
38 list = [ ];
39
40 req.resolve(list);
41 },
42
43 parseCallReply: function(req, res) {
44 var msg = null;
45
46 try {
47 if (!res.ok)
48 L.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
49 req.object, req.method, res.status, res.statusText || '?');
50
51 msg = res.json();
52 }
53 catch (e) {
54 return req.reject(e);
55 }
56
57 /*
58 * The interceptor args are intentionally swapped.
59 * Response is passed as first arg to align with Request class interceptors
60 */
61 Promise.all(rpcInterceptorFns.map(function(fn) { return fn(msg, req) }))
62 .then(this.handleCallReply.bind(this, req, msg))
63 .catch(req.reject);
64 },
65
66 handleCallReply: function(req, msg) {
67 var type = Object.prototype.toString,
68 ret = null;
69
70 try {
71 /* verify message frame */
72 if (!L.isObject(msg) || msg.jsonrpc != '2.0')
73 L.raise('RPCError', 'RPC call to %s/%s returned invalid message frame',
74 req.object, req.method);
75
76 /* check error condition */
77 if (L.isObject(msg.error) && msg.error.code && msg.error.message)
78 L.raise('RPCError', 'RPC call to %s/%s failed with error %d: %s',
79 req.object, req.method, msg.error.code, msg.error.message || '?');
80 }
81 catch (e) {
82 return req.reject(e);
83 }
84
85 if (Array.isArray(msg.result)) {
86 ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0];
87 }
88
89 if (req.expect) {
90 for (var key in req.expect) {
91 if (ret != null && key != '')
92 ret = ret[key];
93
94 if (ret == null || type.call(ret) != type.call(req.expect[key]))
95 ret = req.expect[key];
96
97 break;
98 }
99 }
100
101 /* apply filter */
102 if (typeof(req.filter) == 'function') {
103 req.priv[0] = ret;
104 req.priv[1] = req.params;
105 ret = req.filter.apply(this, req.priv);
106 }
107
108 req.resolve(ret);
109 },
110
111 list: function() {
112 var msg = {
113 jsonrpc: '2.0',
114 id: rpcRequestID++,
115 method: 'list',
116 params: arguments.length ? this.varargs(arguments) : undefined
117 };
118
119 return this.call(msg, this.handleListReply);
120 },
121
122 declare: function(options) {
123 return Function.prototype.bind.call(function(rpc, options) {
124 var args = this.varargs(arguments, 2);
125 return new Promise(function(resolveFn, rejectFn) {
126 /* build parameter object */
127 var p_off = 0;
128 var params = { };
129 if (Array.isArray(options.params))
130 for (p_off = 0; p_off < options.params.length; p_off++)
131 params[options.params[p_off]] = args[p_off];
132
133 /* all remaining arguments are private args */
134 var priv = [ undefined, undefined ];
135 for (; p_off < args.length; p_off++)
136 priv.push(args[p_off]);
137
138 /* store request info */
139 var req = {
140 expect: options.expect,
141 filter: options.filter,
142 resolve: resolveFn,
143 reject: rejectFn,
144 params: params,
145 priv: priv,
146 object: options.object,
147 method: options.method
148 };
149
150 /* build message object */
151 var msg = {
152 jsonrpc: '2.0',
153 id: rpcRequestID++,
154 method: 'call',
155 params: [
156 rpcSessionID,
157 options.object,
158 options.method,
159 params
160 ]
161 };
162
163 /* call rpc */
164 rpc.call(msg, rpc.parseCallReply.bind(rpc, req));
165 });
166 }, this, this, options);
167 },
168
169 getSessionID: function() {
170 return rpcSessionID;
171 },
172
173 setSessionID: function(sid) {
174 rpcSessionID = sid;
175 },
176
177 getBaseURL: function() {
178 return rpcBaseURL;
179 },
180
181 setBaseURL: function(url) {
182 rpcBaseURL = url;
183 },
184
185 getStatusText: function(statusCode) {
186 switch (statusCode) {
187 case 0: return _('Command OK');
188 case 1: return _('Invalid command');
189 case 2: return _('Invalid argument');
190 case 3: return _('Method not found');
191 case 4: return _('Resource not found');
192 case 5: return _('No data received');
193 case 6: return _('Permission denied');
194 case 7: return _('Request timeout');
195 case 8: return _('Not supported');
196 case 9: return _('Unspecified error');
197 case 10: return _('Connection lost');
198 default: return _('Unknown error code');
199 }
200 },
201
202 addInterceptor: function(interceptorFn) {
203 if (typeof(interceptorFn) == 'function')
204 rpcInterceptorFns.push(interceptorFn);
205 return interceptorFn;
206 },
207
208 removeInterceptor: function(interceptorFn) {
209 var oldlen = rpcInterceptorFns.length, i = oldlen;
210 while (i--)
211 if (rpcInterceptorFns[i] === interceptorFn)
212 rpcInterceptorFns.splice(i, 1);
213 return (rpcInterceptorFns.length < oldlen);
214 }
215 });