rpcclient: refactor error handling.
This commit is contained in:
parent
89160bdfa8
commit
47c43ea39a
10
bin/cli
10
bin/cli
@ -476,7 +476,15 @@ CLI.prototype.rpc = co(function* rpc() {
|
|||||||
params.push(param);
|
params.push(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = yield this.client.rpc.call(method, params);
|
try {
|
||||||
|
result = yield this.client.rpc.execute(method, params);
|
||||||
|
} catch (e) {
|
||||||
|
if (e.type === 'RPCError') {
|
||||||
|
this.log(e.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
this.log(result);
|
this.log(result);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
var Network = require('../protocol/network');
|
var Network = require('../protocol/network');
|
||||||
var request = require('./request');
|
var request = require('./request');
|
||||||
|
var util = require('../utils/util');
|
||||||
var co = require('../utils/co');
|
var co = require('../utils/co');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,7 +45,7 @@ function RPCClient(options) {
|
|||||||
* @returns {Promise} - Returns Object?.
|
* @returns {Promise} - Returns Object?.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
RPCClient.prototype.call = co(function* call(method, params) {
|
RPCClient.prototype.execute = co(function* execute(method, params) {
|
||||||
var res = yield request({
|
var res = yield request({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
uri: this.uri,
|
uri: this.uri,
|
||||||
@ -62,20 +63,34 @@ RPCClient.prototype.call = co(function* call(method, params) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!res.body)
|
if (!res.body)
|
||||||
return;
|
throw new Error('No body for JSON-RPC response.');
|
||||||
|
|
||||||
if (res.statusCode === 400)
|
if (res.body.error)
|
||||||
return res.body.result;
|
throw new RPCError(res.body.error.message, res.body.error.code);
|
||||||
|
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200)
|
||||||
if (res.body.error)
|
|
||||||
throw new Error(res.body.error.message);
|
|
||||||
throw new Error('Status code: ' + res.statusCode);
|
throw new Error('Status code: ' + res.statusCode);
|
||||||
}
|
|
||||||
|
|
||||||
return res.body.result;
|
return res.body.result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Helpers
|
||||||
|
*/
|
||||||
|
|
||||||
|
function RPCError(msg, code) {
|
||||||
|
Error.call(this);
|
||||||
|
|
||||||
|
if (Error.captureStackTrace)
|
||||||
|
Error.captureStackTrace(this, RPCError);
|
||||||
|
|
||||||
|
this.type = 'RPCError';
|
||||||
|
this.message = msg + '';
|
||||||
|
this.code = code >>> 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
util.inherits(RPCError, Error);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -580,8 +580,6 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
try {
|
try {
|
||||||
json = yield this.rpc.execute(cmd);
|
json = yield this.rpc.execute(cmd);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(err);
|
|
||||||
|
|
||||||
if (err.type === 'RPCError') {
|
if (err.type === 'RPCError') {
|
||||||
out.push({
|
out.push({
|
||||||
result: null,
|
result: null,
|
||||||
@ -594,6 +592,8 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.error(err);
|
||||||
|
|
||||||
out.push({
|
out.push({
|
||||||
result: null,
|
result: null,
|
||||||
error: {
|
error: {
|
||||||
|
|||||||
@ -146,7 +146,7 @@ describe('HTTP', function() {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
it('should execute an rpc call', co(function* () {
|
it('should execute an rpc call', co(function* () {
|
||||||
var info = yield wallet.client.rpc.call('getblockchaininfo', []);
|
var info = yield wallet.client.rpc.execute('getblockchaininfo', []);
|
||||||
assert.equal(info.blocks, 0);
|
assert.equal(info.blocks, 0);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user