1

Is there a way to pass values to Anchor error messages that can be read by Anchor clients? Ex. How would I pass the value of data.data below? If this is not possible, what is the best way to retrieve this value if running on devnet?

#[program]
mod hello_anchor {
    use super::*;
    pub fn set_data(ctx: Context<SetData>, data: MyAccount) -> Result<()> {
        if data.data >= 100 {
            return err!(MyError::DataTooLarge);
        }
        ctx.accounts.my_account.set_inner(data);
        Ok(())
    }
}

#[error_code]
pub enum MyError {
    #[msg("MyAccount may only hold data below 100")]
    DataTooLarge
}

1 Answer 1

1

You can use msg macro inside the if condition:

pub fn set_data(ctx: Context<SetData>, data: MyAccount) -> Result<()> {
    if data.data >= 100 {
        msg!("{}",data.data);
        return err!(MyError::DataTooLarge);
    }
    ctx.accounts.my_account.set_inner(data);
    Ok(())
}

Now data.data will appear in the program log if the size is above 100. However, it won't be shown separately in the error message.

You will need to implement Debug trait if data.data is a custom type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.