0

I have been working on error handling recently using the event emitter. If I call a function directly from a contract (using contract.methods.myMethod().send{from: Ganache supplied account}) with an input that violates a require statement, then the .error part of the event emitter is invoked. That works as I would expect.

However, if I call a function through a proxy contract with an input that violates a require statement, the .error part of the event emitter is not invoked. I would note that the ‘transactionHash’, 'receipt, and ‘confirmation’ events of the event emitter work when going through proxy contracts. However, I do receive a notification of an error through the console even though I do not have any console.log() statements in my code. See below:

web3.min.js:20 Uncaught (in promise) Error: Returned error: VM Exception while processing transaction: revert Number of offices can't be less than one
    at Object.ErrorResponse (web3.min.js:20)
    at Object.callback (web3.min.js:20)
    at web3.min.js:30
    at Array.forEach (<anonymous>)
    at s._onMessage (web3.min.js:30)
ErrorResponse @ web3.min.js:20
(anonymous) @ web3.min.js:20
(anonymous) @ web3.min.js:30
s._onMessage @ web3.min.js:30
async function (async)
initializeElectionProxy @ playing.js:743
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3

OR

Uncaught (in promise) Error: Returned error: VM Exception while processing transaction: revert Number of offices can't be less than one
    at Object.ErrorResponse (errors.js:28)
    at Object.callback (index.js:303)
    at index.js:114
    at Array.forEach (<anonymous>)
    at s._onMessage (index.js:102)

I tried to encapsulate my function call in try catch blocks, but that didn’t do anything.

I believe that this issue is related to the differences between the Truffle contract instance and the web3.js contract object. I’ve put together some files that illustrate the issue on github. There are instructions in the readme file on how to get it running, how to run the functions, and what to look for. Hopefully, that’s enough to show you the problem I’m experiencing. If not, please let me know and I’ll clarify. If someone wouldn’t mind taking a look and letting me know what you think, that would be excellent.

The link to the github repository is https://github.com/cjd9s/ProxyErrorWeb3jsDemo.

I should mention that I found a post on here that might have been addressing the same issue. The link and quotes that caught my attention are below:

Handling require/assert failure errors correctly from a Node.JS app?

"The challenge is accessing this message further up the stack in Web3 and Node. In the meantime, you can use try and .catch() in promisified code such as a truffle contract abstraction."

In my case, .catch does work with the Truffle contract abstraction, but not with the web3.js object. Does this shed light on the problem?

Again, thanks so much for your time; I’ve very grateful. Cheers.

Sincerely, Craig

0

I'm embarrassed to report that I found my problem. It had nothing to do with the Truffle contract abstraction or web3.js contract object. The problem was in the web3.js function estimateGas. This function doesn't just give you a gas estimate, it simulates the transaction and so throws any errors that the function call would throw when used in anger. So, when it estimated a function call that violated a require statement for the contract, it threw the appropriate revert error.

The following link gives a brief explanation. https://medium.com/@suyashsumaroo/go-ethereum-mistakes-and-solutions-854c172ebfcb

Anyway, I appreciate you listening and apologize for wasting your time. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.