3

Using require helps in validating user inputs before an action can be taken. However, it seems not many options exist to handle the scenario when require indeed returns false. The example mentioned in this answer uses Events that are fired after testing an if condition. This answer bluntly says that it is not possible to handle revert (even though the question was about require) from the front-end.

So, is it possible to consume a require false outcome at all? Or, can it be piped into an Event that can be consumed in a web3.js client?

cogitoergosum
  • 299
  • 3
  • 12
  • 1
    From a web3,js client, you can very easily invoke and handle a function-call which triggers require(<false condition>), if that's what you mean. I do that all the time (as part of testing and verification). – goodvibration May 02 '18 at 10:43

2 Answers2

2

Error reason strings for revert and require has been introduced in solidity 0.4.22. You can define the reason of revert and require. With some bug fixes, latest version of solidity is 0.4.23.

For more details: https://medium.com/secureblocks/solidity-0-4-22-enhancements-94d2b9b8b6fe

Aniket
  • 3,545
  • 2
  • 20
  • 42
  • 1
    How can you access these errors from web3 though? – Makaronodentro Jul 09 '18 at 17:48
  • @Makaronodentro this is not posssible yet, work is in progress for this. Although on Remix you can see the error reason string. – Aniket Jul 17 '18 at 10:53
  • is it possible to access there errors from web3 now? @Aniket – deju Apr 24 '19 at 11:54
  • @deju It is supported by truffle too but web3 looks to support it in 1.0 stable release targetting on 30th june. Follow this for updates: https://github.com/ethereum/web3.js/issues/1707 – Aniket Apr 24 '19 at 12:25
1

When you require / assert / revert everything that was done or would be done in the function is rolled back, that includes Events as well.

So, no, if a require / revert / assert happens there's no way to inform or return anything useful to handle the error.

As I mentioned in another similar question, you will need to check for this potential errors client-side as well so you can prevent the contract from reaching such a state. (E. G.: if a function requires that msg.sender == owner, in the front-end if the logged user is not the owner, disable/hide the button that calls this contract function)

pabloruiz55
  • 7,686
  • 2
  • 17
  • 38
  • Ok. Suppose a contract depended on another contract that had a require failure. This cannot be handled by front end. How does one deal with such a scenario? – cogitoergosum Nov 07 '17 at 11:58
  • 1
    You just can't. The execution will fail and the user will not know why. The best you can do is to explain as well as possible under what conditions a certain function might fail. – pabloruiz55 Nov 07 '17 at 12:39