Web3 1.2.5: Revert reason strings

How to use the new feature

A new Web3 version was just released and it comes with a new feature that should make your life easier. With the latest version 1.2.5, you can now see the the revert reason if you use the new handleRevert option.

You can activate it easily by using web3.eth.handleRevert = true . Now when you use call or send functions, precisely one of the following:

  • web3.eth.call()
  • web3.eth.sendTransaction()
  • contract.methods.myMethod(…).send(…)
  • contract.methods.myMethod(…).call(…)

you will see a new message like

Error: Your request got reverted with the following reason string: This is the revert reason!\

If you have to use sendSignedTransaction, that is not yet implemented. However, you can work around that by catching a reverted transaction and then calling it.

try {
    const result = await sendTxWithSendSignedTransaction()
    console.log({ result })
} catch (error) {
    }).then(result => { throw Error('unlikely to happen') })
    .catch(revertReason => console.log({ revertReason }))

This will hopefully make it a lot easier trying to find out why transactions reverted in a live system. Incorporate this into your error logging system to make the most use of it.

Markus Waas

Solidity Developer

More great blog posts from Markus Waas

© 2024 Solidity Dev Studio. All rights reserved.

This website is powered by Scrivito, the next generation React CMS.