Skip to main content

Module error

Module error 

Source
Expand description

Error handling types and utilities for contract execution.

This module provides a lightweight error type based on integer error codes, suitable for no_std environments. Contracts return error codes to the runtime, which can be interpreted by clients.

§Error Codes

Error codes are i32 values where:

  • 0 = Success (OK)
  • 1-99 = SDK/runtime errors
  • 100+ = Contract-specific errors

§Custom Errors

Use the #[error_code] macro to define custom error enums:

#[error_code(base = 1000)]
enum TokenError {
    InsufficientBalance,  // 1000
    Unauthorized,         // 1001
    InvalidAmount,        // 1002
}

§Guards

Use the #[require] macro for precondition checks:

#[require(amount > 0, TokenError::InvalidAmount)]
fn transfer(amount: u64) -> Result<()> {
    // amount is guaranteed to be > 0
}

Structs§

Error
Lightweight error type wrapping an i32 error code.

Constants§

ERR_REQUIRE
Error code for failed #[require] guards without custom error.

Type Aliases§

Result
Result type alias using Error as the error type.