Loading...
In this smartbook we will discuss about some error hangling functions that solidity has.
Solidity handles issues with state-reverting exceptions. These exceptions revert all modifications done to the state in the current call, including all sub-calls and shows an error to the caller.
Syntax : assert(bool condition)
The assert function creates an error of type Panic(uint256) and thus state change reversion if the condition is not met . Assert should only be used to test for internal errors, and to check invariants. Properly functioning code should never create a Panic, not even on invalid external input. If this happens, then there is a bug in your contract which you should fix. Language analysis tools can evaluate your contract to identify the conditions and function calls which will cause a Panic.
Panic error using assert is created by the compiler in certain situations as listed below as error code and cause :
Code Example :
// Assert on 100 as a input
function store(uint num) public {
assert( num == 100);
number = num;
}
Syntax : require(bool condition, string memory message)
The require function reverts if the condition is not met and creates an error without any data or an error of type Error(string). It should be used to ensure valid conditions that cannot be detected until execution time. It is to be used for errors in inputs or external components. This includes conditions on inputs or return values from calls to external contracts.
You can optionally provide a message string for require, but not for assert.
An Error(string) exception (or an exception without data) is generated by the compiler in the following situations:
For the following cases, the error data from the external call (if provided) is forwarded. This mean that it can either cause an Error or a Panic (or whatever else was given):
Code Example :
// Only numbers below 100 are allowed
function store(uint num) public {
require( num < 100 , 'Number is above 100');
number = num;
}
Syntax : revert CustomError(arg1, arg2);
A direct revert can be triggered using the revert statement and the revert function. It aborts the execution and revert state changes. The revert statement takes a custom error as an argument
For backards-compatibility reasons, there is also the revert() function, which uses parentheses and accepts a string:
Syntax2 : revert(); revert(“description”);
The error data will be passed back to the caller and can be caught there. Using revert() causes a revert without any error data while revert("description") will create an Error(string) error.
Using a custom error instance will usually be much cheaper than a string description, because you can use the name of the error to describe it, which is encoded in only four bytes. A longer description can be supplied via NatSpec which does not incur any costs.
Code Example A :
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract VendingMachine {
address owner;
function buy(uint amount) public payable {
if (amount > msg.value / 2 ether)
revert("Not enough Ether provided.");
// Alternative way to do it:
require( amount <= msg.value / 2 ether, "Not enough Ether provided.");
}
}
Code Example B :
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract VendingMachine {
address owner;
error Unauthorized();
function withdraw() public {
if (msg.sender != owner)
revert Unauthorized();
payable(msg.sender).transfer(address(this).balance);
}
}