repr(u32) enum from/into an Error.
Includes the type in the contract spec so that clients can generate bindings for the type.
Syntax
Parameters
Optional path to the Soroban SDK crate. Use this if you’ve renamed the crate in your
Cargo.toml.Whether to include this type in the contract specification. If not specified, types are exported if they are
pub.Optional library name for organizing types in the contract specification.
Constraints
There are several constraints on the types that are supported:- The enum must derive
Copy - All enum variants must have an explicit integer literal
- Enum variants must have a value convertible to
u32 - The enum must be marked with
#[repr(u32)]
Example
Defining an error and capturing errors using the try_ variant
Testing invocations that cause errors with should_panic
Alternatively, you can test error cases using the #[should_panic] attribute:
Error Handling
When a contract function returns aResult<T, E> where E is an error type marked with #[contracterror]:
- If the function returns
Ok(value), the value is returned to the caller - If the function returns
Err(error), the contract panics with the error code - Client functions have
try_variants that returnResult<T, Result<E, InvokeError>>, allowing you to capture and inspect errors in tests
See Also
#[contracttype]- Define general contract types- Error Handling Guide - Complete error handling guide