Trait FunctionError

Source
pub trait FunctionError {
    // Required method
    fn panic(&self) -> !;
}
Expand description

Enables contract runtime to panic with the given type. Any error type used in conjunction with #[handle_result] has to implement this trait.

Example:

use near_sdk::{FunctionError, near};

enum Error {
    NotFound,
    Unexpected { message: String },
}

impl FunctionError for Error {
    fn panic(&self) -> ! {
        match self {
            Error::NotFound =>
                near_sdk::env::panic_str("not found"),
            Error::Unexpected { message } =>
                near_sdk::env::panic_str(&format!("unexpected error: {}", message))
        }
    }
}

#[near(contract_state)]
#[derive(Default)]
pub struct Contract;

#[near]
impl Contract {
    // if the Error does not implement FunctionError, the following will not compile with #[handle_result]
    #[handle_result]
    pub fn set(&self, value: String) -> Result<String, Error> {
        Err(Error::NotFound)
    }
}

Required Methods§

Source

fn panic(&self) -> !

Implementors§