From 47c43ea39aed7577dd9cab1a79247d5c84c154a3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 21 Feb 2017 22:33:54 -0800 Subject: [PATCH] rpcclient: refactor error handling. --- bin/cli | 10 +++++++++- lib/http/rpcclient.js | 31 +++++++++++++++++++++++-------- lib/http/server.js | 4 ++-- test/http-test.js | 2 +- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/bin/cli b/bin/cli index d501d50b..cff7fe00 100755 --- a/bin/cli +++ b/bin/cli @@ -476,7 +476,15 @@ CLI.prototype.rpc = co(function* rpc() { 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); }); diff --git a/lib/http/rpcclient.js b/lib/http/rpcclient.js index c1e5835c..fc15b93b 100644 --- a/lib/http/rpcclient.js +++ b/lib/http/rpcclient.js @@ -8,6 +8,7 @@ var Network = require('../protocol/network'); var request = require('./request'); +var util = require('../utils/util'); var co = require('../utils/co'); /** @@ -44,7 +45,7 @@ function RPCClient(options) { * @returns {Promise} - Returns Object?. */ -RPCClient.prototype.call = co(function* call(method, params) { +RPCClient.prototype.execute = co(function* execute(method, params) { var res = yield request({ method: 'POST', uri: this.uri, @@ -62,20 +63,34 @@ RPCClient.prototype.call = co(function* call(method, params) { }); if (!res.body) - return; + throw new Error('No body for JSON-RPC response.'); - if (res.statusCode === 400) - return res.body.result; + if (res.body.error) + throw new RPCError(res.body.error.message, res.body.error.code); - if (res.statusCode !== 200) { - if (res.body.error) - throw new Error(res.body.error.message); + if (res.statusCode !== 200) throw new Error('Status code: ' + res.statusCode); - } 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 */ diff --git a/lib/http/server.js b/lib/http/server.js index 4a4375d7..d1d8ee0f 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -580,8 +580,6 @@ HTTPServer.prototype._init = function _init() { try { json = yield this.rpc.execute(cmd); } catch (err) { - this.logger.error(err); - if (err.type === 'RPCError') { out.push({ result: null, @@ -594,6 +592,8 @@ HTTPServer.prototype._init = function _init() { continue; } + this.logger.error(err); + out.push({ result: null, error: { diff --git a/test/http-test.js b/test/http-test.js index c3e7a7db..0a4e5103 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -146,7 +146,7 @@ describe('HTTP', 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); }));