Run the Example
First go through the Setup process to get your development environment configured, then clone thev23.0.0 tag of soroban-examples repository:
errors directory, and use cargo test.
Code
errors/src/lib.rs
How it Works
Open theerrors/src/lib.rs file to follow along.
Defining an Error
Contract errors are Rust u32 enums where every variant of the enum is assigned an integer. The#[contracterror] attribute is used to set the error up so it can be used in the return value of contract functions.
The enum has some constraints:
- It must have the
#[repr(u32)]attribute. - It must have the
#[derive(Copy)]attribute. - Every variant must have an explicit integer value assigned.
Returning an Error
Errors can be returned from contract functions by returningResult<_, E>.
The increment function returns a Result<u32, Error>, which means it returns Ok(u32) in the successful case, and Err(Error) in the error case.
Panicking with an Error
Errors can also be panicked instead of being returned from the function. The increment function could also be written as follows with au32 return value. The error can be passed to the environment using the panic_with_error! macro.
Tests
Open theerrors/src/test.rs file to follow along.
errors/src/test.rs
Env, which is the Soroban environment that the contract will run in.
impl block that is annotated with the #[contractimpl] attribute have a corresponding function generated in a generated client type. The client type will be named the same as the contract type with Client appended. For example, in our contract the contract type is IncrementContract, and the client is named IncrementContractClient.
Result<>, and the other that does not handle errors and panics if an error occurs.
try_increment
In the first test the try_increment function is called and returns Result<Result<u32, _>, Result<Error, InvokeError>>.
- If the function call is successful,
Ok(Ok(u32))is returned. - If the function call is successful but returns a value that is not a
u32,Ok(Err(_))is returned. - If the function call is unsuccessful and fails with an error in the
Errorenum,Err(Ok(Error))is returned. - If the function call is unsuccessful but returns an error code not in the
Errorenum, or returns a system error code,Err(Err(InvokeError))is returned and theInvokeErrorcan be inspected.
increment
In the second test the increment function is called and returns u32. When the last call is made the function panics.
- If the function call is successful,
u32is returned. - If the function call is successful but returns a value that is not a
u32, a panic occurs. - If the function call is unsuccessful, a panic occurs.
Build the Contract
To build the contract, use thestellar contract build command.
.wasm file should be outputted in the target directory:
Run the Contract
Let’s deploy the contract to Testnet so we can run it. The value provided as--source-account was set up in our Getting Started guide; please change accordingly if you created a different identity.
- macOS/Linux
- Windows (PowerShell)
CA4KWO3HL6M5F5MZ5ITVFLCQ6ZM2GDSP2NOTNXOT4GIRECQOVX3I6CXL.
Now that we’ve deployed the contract, we can invoke it.
- macOS/Linux
- Windows (PowerShell)
stellar contract read.
- macOS/Linux
- Windows (PowerShell)