Skip to main content
A convenient way to manage and meaningfully communicate contract errors is to collect them into an enum struct. These errors are a special type of enum integer type that are stored on the ledger as Error values containing a u32 code. First, create the Error struct in your smart contract.
Smart contracts can fail with error enums in two different ways. They can either return a Result with their intended return value and the #[contracterror] struct as the error, or just invoke panic_with_error! with the appropriate Error enum value whenever an error condition is reached. By default, most ecosystem standards assume that contract functions do not return a Result, so using panic_with_error! is recommended. However, both styles behave in the same way. If an error is returned or panic_with_error! is invoked, the transaction will fail. Contracts making cross contract calls have the ability to catch and handle these failures with try_ functions.
When converted to XDR, the value becomes an ScVal, containing a ScError, containing the integer value of the error as contract error.