[Ethereum] Force function execution

explorersremixrevert-opcodesolidity

I have an approve method.

 function approve(address spender, uint value) public returns(bool){
        if (msg.sender==_owner){
           allowance[msg.sender][spender] = value;
           emit Approval(msg.sender, spender, value);
        }
        else if (msg.sender!=_owner){
            revert('not owner');
        }
        return true;
}

i want prevent from approving if it called not by me.

when i call approve function on remix website using not owners adrees i always see an error

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Internal JSON-RPC error. { "code": 3, "message": "execution reverted: not owner", "data":"longbytecode"}

and then i can press Send transaction and force execution or cancel it.

Is this possible to always force execution of this function and hardcode it in smartcontract? So i can see my revert message on blockchain explorer?

Are there any other ways to always show my error message in chain explorer without forcing function execution?

Best Answer

Remix (and most web3 providers such as metamask) simulate the transaction beforehand ( this is useful, it allows to estimate the gas spent for example) so if they find out that the transaction will revert, they will warn the user about it. Something you could do is get rid of the revert statement. Like this for exemple


function approve(address spender, uint value) public returns(bool){
        if (msg.sender==_owner){
           allowance[msg.sender][spender] = value;
           emit Approval(msg.sender, spender, value);
 return true
        }
        else { // sufficient check, dont need to explicitely check for  (msg.sender != _owner) 
            return 0;
        }