I'm wondering if a modifier function can use events while still throwing afterwards?
I'm looking for a way to have a "catch error" events that would trigger if it fails for X or Z modifier.
Anyone knows a way to do this ?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
No, neither modifiers nor the body of the code can use events if they throw errors. Throwing an error reverts everything about the transaction except the gas usage, so it doesn't leave anything written to the event log.
The alternative is to use return codes and events to track whether something went wrong instead of throwing errors, but that means you need to be a bit more careful about your control logic, because you need to detect the error conditions you're interested in before you start writing data or updating balances.
You may wish to consider if you need to throw.
For reference, a throw reverts every change in the transaction so far. Solidity helpfully continues the reversion down the stack (although there are ways around this). All gas is consumed in the process.
Considering all this, a throw may be overkill if the transaction can be reversed manually. If the modifier is to prevent everyone except the owner from withdrawing money, there's not much need to undo the transaction. If the modifier is to prevent people from depositing money, then it makes perfect sense to throw.