ruimarinho / bitcoin-core

A modern Bitcoin Core REST and RPC client.
480 stars 188 forks source link

Can not catch RpcError in promise. #27

Closed joemphilips closed 7 years ago

joemphilips commented 7 years ago

To reproduce

const BitcoindClient = require("bitcoin-core");
const config =  {
    network: 'testnet',
    username: 'testuser',
    password: 'this_is_pass',
    host: "127.0.0.1" 
};

const bitcoindclient = new BitcoindClient(config);
bitcoindclient.sendToAddress("foo", 0.01, "This is message", "and comment.")
  .catch((err) => console.log(err))

What I expect to happen is that function inside .catch() to be triggered since the first argument to sendToAddress is invalid. It will cause RpcError as expected, but can not catch the error. It will shows following stacktrace.

Unhandled rejection RpcError: Invalid amount for send
    at get (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bitcoin-core/dist/src/parser.js:38:11)
    at Client.rpc (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bitcoin-core/dist/src/parser.js:81:14)
    at Client.tryCatcher (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/promise.js:693:18)
    at Promise._fulfill (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/promise.js:638:18)
    at Request._callback (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/bluebird/js/release/nodeback.js:45:21)
    at Request.self.callback (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/request/request.js:188:22)
    at emitTwo (events.js:111:20)
    at Request.emit (events.js:194:7)
    at Request.<anonymous> (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:191:7)
    at IncomingMessage.<anonymous> (/Users/miyamotojou/working/blockchain/BTC/okimochi/node_modules/request/request.js:1091:12)
    at Object.onceWrapper (events.js:293:19)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

So what should I do to catch the error? Promise feature is not supported for this library?

ruimarinho commented 7 years ago

@joemphilips if you assign the promise chain to a variable and inspect it, you'll see that the returned value is a Promise. It is fully supported and is the preferred method of interacting with the library.

const promise = bitcoindclient.sendToAddress("foo", 0.01, "This is message", "and comment.")
  .catch((err) => console.log(err))
console.log(promise);

I'm unable to reproduce this. In fact, the error that I receive is Invalid Bitcoin address and not Invalid amount for send, which appears to be the expected one as the first argument to be validated is the bitcoin address.

What happens if you use:

bitcoindclient.sendToAddress("mqs2cd6T2GdkqczAHK3kcnXTnrqVLEuR5o", "foo", "This is message", "and comment.")

Also, which node and bitcoind versions are you using? Closing issue, but will re-open if bug is confirmed.