Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnhandledPromiseRejectionWarning when sending a transaction and the error event handler throws #3708

Closed
gabmontes opened this issue Sep 3, 2020 · 1 comment · Fixed by #3709

Comments

@gabmontes
Copy link
Contributor

Expected behavior

Send a transaction that will fail i.e. because the gas amount is not specified, and use the events interface of the promi-event. If the error event handler throws, that error should be handled exclusively in "userland". If the promises interface is used as well, the no-gas-specified error should be seen.

Actual behavior

An unhandled promise rejection is thrown within web3. On top of that, the error coming into the .catch() is not the original gas error but the error within the buggy error event handler. This is misleading because the issue is not within the library but in the user code and the error obtained shadows the actual send transaction error.

Spoiler alert

The following line must be wrapped in a try-catch block to prevent the error to bubble up and wreak havoc:

https://github.com/ethereum/web3.js/blob/87e668275ac7d9b8af7c909137fc0626c3866929/packages/web3-core-method/src/index.js#L732

Steps to reproduce the behavior

const Web3 = require('web3')
const web3 = new Web3()

web3.eth.accounts.wallet.create(0).add(process.env.PRIVATE_KEY)
const from = web3.eth.accounts.wallet[0].address

const emitter = web3.eth.sendTransaction({ from, to: from, value: '1' }) // no gas was specified!
emitter.on('error', function () { throw new Error('UNHANDLED') })
emitter.catch(console.error)

Logs

$ node bug.js                                                                          
(node:21330) UnhandledPromiseRejectionWarning: Error: UNHANDLED
    at Promise.<anonymous> (./bug.js:8:41)
    at Promise.emit (./packages/web3-core-promievent/node_modules/eventemitter3/index.js:181:35)
    at ./packages/web3-core-method/src/index.js:720:60
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:21330) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:21330) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Environment

web3@1.2.11

gabmontes added a commit to bloq/web3.js that referenced this issue Sep 3, 2020
Since the event handlers are run syncronously, if an `error` event handler thows, the error will return to the lib and keep the promise chain broken, rising an unhandled promise rejection error.

Resolves web3#3708
gabmontes added a commit to bloq/web3.js that referenced this issue Sep 3, 2020
Since the event handlers are run syncronously, if an `error` event handler thows, the error will return to the lib and keep the promise chain broken, rising an unhandled promise rejection error.

Resolves web3#3708
@GregTheGreek
Copy link
Contributor

Thanks for reporting this and opening a pr! I'll take a look at this later today 😊

gabmontes added a commit to bloq/web3.js that referenced this issue Oct 15, 2020
Since the event handlers are run syncronously, if an `error` event handler thows, the error will return to the lib and keep the promise chain broken, rising an unhandled promise rejection error.

Resolves web3#3708
gabmontes added a commit to bloq/web3.js that referenced this issue Oct 16, 2020
Since the event handlers are run syncronously, if an `error` event handler thows, the error will return to the lib and keep the promise chain broken, rising an unhandled promise rejection error.

Resolves web3#3708
gabmontes added a commit to bloq/web3.js that referenced this issue Oct 19, 2020
Since the event handlers are run syncronously, if an `error` event handler thows, the error will return to the lib and keep the promise chain broken, rising an unhandled promise rejection error.

Resolves web3#3708
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@gabmontes @GregTheGreek and others