We are learning Cairo
, and writing WTF Cairo Tutorials
for Starknet newbies. The tutorials are based on Cairo 2.2.0
.
Twitter: @0xAA_Science|@WTFAcademy_
WTF Academy Community:Discord|Wechat|Website
All codes and tutorials are open-sourced on GitHub: github.com/WTFAcademy/WTF-Cairo
In this chapter, we will explore error handling in Cairo. With these techniques, you will be able to anticipate and respond to potential errors, providing useful feedback via error messages in your code.
Cairo offers several methods to handle potential errors in your code. In this chapter, we'll examine two of these, namely assert()
(recommended) and panic()
.
The assert()
function is the recommended method for handling errors in Cairo. It functions similarly to the require()
function in Solidity and takes two arguments:
condition
: aboolean
operation expected to be true at runtime.error message
: a short string that displays when thecondition
is false.
assert()
verifies whether a given condition is true
, and if it isn't, it throws an error message.
In the example below, if input
is not equal to 0
, the call will be reverted, and the error message 'Error: Input not 0!'
will be displayed.
// throw error if input is 0 with assert (recommended)
#[external(v0)]
fn assert_example(self: @ContractState, input: u128){
assert( input == 0_u128, 'Error: Input not 0!');
}
The panic()
function is another method Cairo provides for error handling. Unlike assert()
, panic()
halts the function abruptly without verifying any condition. It takes a felt252
array as arguments, which serves as an error message.
We can modify the assert_example()
function to utilize panic()
as shown below.
use array::ArrayTrait;
use traits::Into;
// Throw error if input is 0 with panic
// panic() accepts felt252 arrary as parameter
#[external(v0)]
fn panic_example(self: @ContractState, input: u128){
if input == 0_u128 {
let mut error_data = ArrayTrait::new();
error_data.append(input.into());
panic(error_data);
}
}
Additionally, Cairo provides a panic_with_felt252()
function. The only distinction between panic_with_felt252()
and panic
is that panic_with_felt252()
takes a felt252
as its argument.
Let's revise the assert_example()
to use panic_with_felt252()
.
// Throw error if input is 0 with panic_with_felt252
// panic_with_felt252() accepts felt252 as parameter
#[external(v0)]
fn panic_with_felt252_example(self: @ContractState, input: u128){
if input == 0_u128 {
panic_with_felt252('Error: Input not 0!');
}
}
In this chapter, we explored various techniques available in Cairo for handling errors. The recommended approach is to use assert()
, which operates in a way similar to require()
in Solidity. This function helps ensure your code runs as expected by verifying critical conditions and alerting you when they're not met.