luci-base: rpc.js: add getStatusText() call

The new function allows translating an ubus return code into a human
readable error message.

Also report the called object and method on ubus rpc errors.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-09-03 19:17:20 +02:00
parent 93af8a3af8
commit aef4bc3a23

View file

@ -44,8 +44,8 @@ return L.Class.extend({
msg = null; msg = null;
if (!res.ok) if (!res.ok)
L.error('RPCError', 'RPC call failed with HTTP error %d: %s', L.error('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
res.status, res.statusText || '?'); req.object, req.method, res.status, res.statusText || '?');
msg = res.json(); msg = res.json();
@ -55,9 +55,9 @@ return L.Class.extend({
/* verify message frame */ /* verify message frame */
if (typeof(msg) == 'object' && msg.jsonrpc == '2.0') { if (typeof(msg) == 'object' && msg.jsonrpc == '2.0') {
if (typeof(msg.error) == 'object' && msg.error.code && msg.error.message) if (typeof(msg.error) == 'object' && msg.error.code && msg.error.message)
req.reject(new Error('RPC call failed with error %d: %s' req.reject(new Error('RPC call to %s/%s failed with error %d: %s'
.format(msg.error.code, msg.error.message || '?'))); .format(req.object, req.method, msg.error.code, msg.error.message || '?')));
else if (Array.isArray(msg.result) && msg.result[0] == 0) else if (Array.isArray(msg.result))
ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0]; ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0];
} }
else { else {
@ -120,7 +120,9 @@ return L.Class.extend({
resolve: resolveFn, resolve: resolveFn,
reject: rejectFn, reject: rejectFn,
params: params, params: params,
priv: priv priv: priv,
object: options.object,
method: options.method
}; };
/* build message object */ /* build message object */
@ -156,5 +158,22 @@ return L.Class.extend({
setBaseURL: function(url) { setBaseURL: function(url) {
rpcBaseURL = url; rpcBaseURL = url;
},
getStatusText: function(statusCode) {
switch (statusCode) {
case 0: return _('Command OK');
case 1: return _('Invalid command');
case 2: return _('Invalid argument');
case 3: return _('Method not found');
case 4: return _('Resource not found');
case 5: return _('No data received');
case 6: return _('Permission denied');
case 7: return _('Request timeout');
case 8: return _('Not supported');
case 9: return _('Unspecified error');
case 10: return _('Connection lost');
default: return _('Unknown error code');
}
} }
}); });