json-rpc error handling (#128)
* json-rpc error handling * partial revert
This commit is contained in:
parent
c3535e6ed3
commit
82726fd9c1
2 changed files with 39 additions and 16 deletions
18
dev/json-rpc.js
Normal file
18
dev/json-rpc.js
Normal file
|
|
@ -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))
|
||||||
|
|
@ -2,32 +2,37 @@
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const uuid = require('uuid')
|
const uuid = require('uuid')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
const _ = require('lodash/fp')
|
||||||
|
|
||||||
module.exports = {fetch, parseConf}
|
module.exports = {fetch, parseConf}
|
||||||
|
|
||||||
function fetch (account, method, params) {
|
function fetch (account, method, params) {
|
||||||
const data = {
|
return Promise.resolve(true)
|
||||||
method,
|
.then(() => {
|
||||||
params,
|
const data = {
|
||||||
id: uuid.v4()
|
method,
|
||||||
}
|
params,
|
||||||
|
id: uuid.v4()
|
||||||
|
}
|
||||||
|
const url = _.defaultTo(`http://localhost:${account.port}`, params.url)
|
||||||
|
|
||||||
return axios({
|
return axios({
|
||||||
method: 'post',
|
method: 'post',
|
||||||
auth: {username: account.username, password: account.password},
|
auth: {username: account.username, password: account.password},
|
||||||
url: `http://localhost:${account.port}`,
|
url,
|
||||||
data
|
data
|
||||||
})
|
})
|
||||||
|
})
|
||||||
.then(r => {
|
.then(r => {
|
||||||
if (r.error) throw r.error
|
if (r.error) throw r.error
|
||||||
return r.data.result
|
return r.data.result
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err.message)
|
throw new Error(_.join(' ', [
|
||||||
try {
|
'json-rpc::axios error:',
|
||||||
console.log(err.response.data.error)
|
_.get('message', err, ''),
|
||||||
} catch (__) {}
|
_.get('response.data.error', err, '')
|
||||||
throw err
|
]))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue