sv::error attribute

Use the sv::error attribute to specify custom error types for the contract.

💡

Custom error type has to implement From<StdError> for the macro to compile. You can quickly provide this logic using, for example, thiserror (opens in a new tab).

Macros

List of macros supporting the sv::error attribute:

💡

The interface macro supports custom error types via the Error associated type.

Usage

use thiserror::Error;
 
pub struct CounterContract;
 
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
    #[error("{0}")]
    Std(#[from] StdError),
}
 
#[cfg_attr(feature = "library", sylvia::entry_points)]
#[contract]
#[sv::error(ContractError)]
impl CounterContract {
    pub const fn new() -> Self {
        Self
    }
 
    #[sv::msg(instantiate)]
    fn instantiate(&self, ctx: InstantiateCtx) -> Result<Response, ContractError> {
        Ok(Response::new())
    }
}
💡
Each method has to use the error type defined in the sv::error attribute.