EOSIO / eosjs

General purpose library for the EOSIO blockchain.
http://eosio.github.io/eosjs
MIT License
1.43k stars 463 forks source link

Add exception handling code #111

Closed yepp4you closed 6 years ago

yepp4you commented 6 years ago

Exception Handling If you make the following request to test, callback chain breaks, and nodejs can not handle exceptions. So I would like to add the following exception handling.

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection: TypeError: A name can be up to 13 characters long newaccount.name = "23413414166666" action.data = {"creator":"eosio","name":"23413414166666","owner":{"threshold":1,"keys":[],"accounts":[{"permission":{"actor":"","permission":"active"},"weight":1}],"waits":[]},"active":{"threshold":1,"keys":[],"accounts":[{"permission":{"actor":"","permission":"active"},"weight":1}],"waits":[]}} transaction.actions = [{"account":"eosio","name":"newaccount","authorization":[{"actor":"eosio","permission":"active"}],"data":{"creator":"eosio","name":"23413414166666","owner":{"threshold":1,"keys":[],"accounts":[{"permission":{"actor":"","permission":"active"},"weight":1}],"waits":[]},"active":{"threshold":1,"keys":[],"accounts":[{"permission":{"actor":"","permission":"active"},"weight":1}],"waits":[]}}},{"account":"eosio","name":"buyram","authorization":[{"actor":"eosio","permission":"active"}],"data":{"payer":"eosio","receiver":"23413414166666","quant":"50.0000 EOS"}},{"account":"eosio","name":"delegatebw","authorization":[{"actor":"eosio","permission":"active"}],"data":{"from":"eosio","receiver":"23413414166666","stake_net_quantity":"100.0000 EOS","stake_cpu_quantity":"100.0000 EOS","transfer":0}}] at encodeName (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/eosjs/lib/format.js:86:31) at Object.appendByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/eosjs/lib/structs.js:153:21) at Object.appendByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/fcbuffer/lib/struct.js:110:20) at actionDataAppendByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/eosjs/lib/structs.js:582:13) at Object.appendByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/fcbuffer/lib/struct.js:108:15) at Object.appendByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/fcbuffer/lib/types.js:353:16) at Object.appendByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/fcbuffer/lib/struct.js:110:20) at toByteBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/fcbuffer/lib/fcbuffer.js:284:8) at Function.toBuffer (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/fcbuffer/lib/fcbuffer.js:271:22) at _callee2$ (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/eosjs/lib/write-api.js:629:30) at tryCatch (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:65:40) at Generator.invoke [as _invoke] (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:303:22) at Generator.prototype.(anonymous function) [as next] (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:117:21) at tryCatch (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:65:40) at invoke (/Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:155:20) at /Users/yepp4you/Work/blockchain/portal/portal_server/node_modules/babel-polyfill/node_modules/regenerator-runtime/runtime.js:165:13 at at process._tickCallback (internal/process/next_tick.js:182:7)

 try {
  const buf = Fcbuffer.toBuffer(Transaction, txObject)
  const tr = Transaction.toObject(txObject)

  const transactionId  = createHash('sha256').update(buf).digest().toString('hex')

  let sigs = []
  if(options.sign){
    const chainIdBuf = new Buffer(config.chainId, 'hex')
    const signBuf = Buffer.concat([chainIdBuf, buf, new Buffer(new Uint8Array(32))])
    sigs = config.signProvider({transaction: tr, buf: signBuf, sign})
    if(!Array.isArray(sigs)) {
      sigs = [sigs]
    }
  }
} catch (err) {
    callback(err)
}

https://github.com/EOSIO/eosjs/blob/b5488002775f036c15cd386815a216becad1f604/src/write-api.js#L401

jcalfee commented 6 years ago

It is caught in checkError above and passed to the callback already:

headers(options.expireInSeconds, checkError(callback, async function(rawTx) {

3 days ago I did make a fix to catch a 2nd level error (in the callback): https://github.com/EOSIO/eosjs/commit/3362dcbb5f829880ccc3c2f43a6171c9971ae26c#diff-cf54ab65a7bd0259fd51ac1e0d3841bdR553

I'll make a release today. That might be what your seeing.

jcalfee commented 6 years ago

fixed in npm version eosjs@12.0.2

yepp4you commented 6 years ago

It seems that the issue has been resolved at eosjs@12.0.2. Thank you.