diff --git a/package.json b/package.json index 36f46d7..14a87f5 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test:integration:free": "bash test/integration/test-free-tier.sh", "test:integration:free:bchn": "bash test/integration/test-free-tier-bchn.sh", "test:integration:free:testnet": "bash test/integration/testnet/test-free-tier.sh", - "test:temp": "mocha --timeout 30000 -g '#validateTxid2' test/unit/slp-utils.js", + "test:temp": "mocha --timeout 30000 -g '#blockHeader' test/unit/electrumx.js", "test:integration:api": "RESTURL=https://api.fullstack.cash/v3/ mocha --timeout 30000 test/integration", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "nyc --reporter=html mocha --timeout 25000 test/unit/", diff --git a/src/electrumx.js b/src/electrumx.js index 0c68d15..e2b5ea8 100644 --- a/src/electrumx.js +++ b/src/electrumx.js @@ -441,8 +441,14 @@ class ElectrumX { ) return response.data } catch (error) { - if (error.response && error.response.data) throw error.response.data - else throw error + // console.log("error: ", error) + if (error.response && error.response.data) { + if (error.response && error.response.data) + throw new Error(error.response.data.error) + else throw error.response.data + } else { + throw error + } } } @@ -515,7 +521,7 @@ class ElectrumX { * } * ] * } - */ + */ async txData(txid) { try { // Handle single transaction. @@ -564,7 +570,7 @@ class ElectrumX { * "success": true, * "txid": "..." * } - */ + */ async broadcast(txHex) { try { if (typeof txHex === "string") { diff --git a/test/unit/electrumx.js b/test/unit/electrumx.js index a203c21..479d77b 100644 --- a/test/unit/electrumx.js +++ b/test/unit/electrumx.js @@ -264,17 +264,38 @@ describe(`#ElectrumX`, () => { describe(`#blockHeader`, () => { it(`should throw an error for improper height input`, async () => { try { + // Mock network calls. + sandbox.stub(axios, "get").rejects({ + response: { + data: { + success: false, + error: "height must be a positive number" + } + } + }) + const height = -10 await bchjs.Electrumx.blockHeader(height) + assert.equal(true, false, "Unexpected result!") } catch (err) { // console.log(`err: `, err) - assert.include(err.error, `height must be a positive number`) + assert.include(err.message, `height must be a positive number`) } }) it(`should throw an error for improper count input`, async () => { try { + // Mock network calls. + sandbox.stub(axios, "get").rejects({ + response: { + data: { + success: false, + error: "count must be a positive number" + } + } + }) + const height = 42 const count = -10 @@ -282,7 +303,7 @@ describe(`#ElectrumX`, () => { assert.equal(true, false, "Unexpected result!") } catch (err) { // console.log(`err: `, err) - assert.include(err.error, `count must be a positive number`) + assert.include(err.message, `count must be a positive number`) } }) @@ -319,7 +340,8 @@ describe(`#ElectrumX`, () => { // Stub the network call. sandbox.stub(axios, "get").resolves({ data: mockData.details }) - const txid = "4db095f34d632a4daf942142c291f1f2abb5ba2e1ccac919d85bdc2f671fb251" + const txid = + "4db095f34d632a4daf942142c291f1f2abb5ba2e1ccac919d85bdc2f671fb251" const result = await bchjs.Electrumx.txData(txid) // console.log(`result: ${JSON.stringify(result, null, 2)}`) @@ -377,10 +399,7 @@ describe(`#ElectrumX`, () => { assert.equal(true, false, "Unexpected result!") } catch (err) { // console.log(`err: `, err) - assert.include( - err.message, - `Input txHex must be a string.` - ) + assert.include(err.message, `Input txHex must be a string.`) } }) it(`should broadcast a single transaction`, async () => {