How to use handleRevert in web3 for fetching transaction reverting reason

Hi Community,

I'm trying to fetch the transaction reverting reason.

https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html?highlight=handlerevert

I'm not able to use it correctly. If someone can help me in this query.

Thanks

Check this out https://web3js.readthedocs.io/en/v1.2.11/callbacks-promises-events.html

Use .on('error', function(error){ ... })

Hey, i ran into the same issue of wanting to use handleRevert, but could not find any solid info on it.
So here is how I got around to doing it.
First step is to enable {handleRevert: true} as a option in your Contract constructor. After passing the ABI and the Address arguments to the Contract constructor you should add a {handleRevert: true} as option.
contract
After that I started getting the ( 'invalid arrayify value', code: 'INVALID_ARGUMENT', argument: 'value' ) error that I have found on the github as well, and other people had it before. I followed the error stack and made it to \node_modules\web3-core-method\lib\index.js where i found the next piece of code:
originalCode
After a little bit of investigation i made some changes and found this solution, that worked for me.
reasonStringOnly
This will return a reason string only, type String.
Also this is how you would return the first part of the error ('RuntimeError: VM Exception while processing transaction') part and also to get the whole error stack you could just do err.data.stack.
I am not 100% sure that it is a solid fix that will work everytime, but in my case it worked just fine.
Cheers.

In the end I setteled for this solution as it properly returns the revert string only, cutting of the stack info.

Be sure to set it at web3.eth like:

web3.eth.handleRevert = true;

And also it might not be visible if you call console.log for the error on the browser. To console.log it in the browser use error.reason or JSON.stringify(error, null, 2). Because the browser mighjt not show the properties of the error object but only the error message, if you just called console.log(error).