From 82726fd9c1524f300440a62676ea205da5dd7b01 Mon Sep 17 00:00:00 2001 From: Fabio Cigliano Date: Sat, 16 Jun 2018 22:55:58 +1200 Subject: [PATCH] json-rpc error handling (#128) * json-rpc error handling * partial revert --- dev/json-rpc.js | 18 +++++++++++++++++ lib/plugins/common/json-rpc.js | 37 +++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 dev/json-rpc.js diff --git a/dev/json-rpc.js b/dev/json-rpc.js new file mode 100644 index 00000000..6bc9f47b --- /dev/null +++ b/dev/json-rpc.js @@ -0,0 +1,18 @@ +const rpc = require('../lib/plugins/common/json-rpc') + +const account = { + username: 'test', + password: 'test' +} + +const method = {} + +const params = { + url: 'https://httpstat.us/500' + // url: 'https://httpstat.us/400' + // url: 'https://httpstat.us/200' +} + +rpc.fetch(account, method, params) + .then(res => console.log('got result', res)) + .catch(err => console.error('gor error', err)) diff --git a/lib/plugins/common/json-rpc.js b/lib/plugins/common/json-rpc.js index 3ec96248..0636b73c 100644 --- a/lib/plugins/common/json-rpc.js +++ b/lib/plugins/common/json-rpc.js @@ -2,32 +2,37 @@ const axios = require('axios') const uuid = require('uuid') const fs = require('fs') +const _ = require('lodash/fp') module.exports = {fetch, parseConf} function fetch (account, method, params) { - const data = { - method, - params, - id: uuid.v4() - } + return Promise.resolve(true) + .then(() => { + const data = { + method, + params, + id: uuid.v4() + } + const url = _.defaultTo(`http://localhost:${account.port}`, params.url) - return axios({ - method: 'post', - auth: {username: account.username, password: account.password}, - url: `http://localhost:${account.port}`, - data - }) + return axios({ + method: 'post', + auth: {username: account.username, password: account.password}, + url, + data + }) + }) .then(r => { if (r.error) throw r.error return r.data.result }) .catch(err => { - console.log(err.message) - try { - console.log(err.response.data.error) - } catch (__) {} - throw err + throw new Error(_.join(' ', [ + 'json-rpc::axios error:', + _.get('message', err, ''), + _.get('response.data.error', err, '') + ])) }) }