110 lines
2.5 KiB
Rust
110 lines
2.5 KiB
Rust
//! This example demonstrates how custom errors and associated error messsages
|
|
//! can be defined and transparently propagated to clients.
|
|
|
|
use anchor_lang::prelude::*;
|
|
|
|
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
|
|
|
|
#[program]
|
|
mod errors {
|
|
use super::*;
|
|
|
|
pub fn hello(_ctx: Context<Hello>) -> Result<()> {
|
|
err!(MyError::Hello)
|
|
}
|
|
|
|
pub fn hello_no_msg(_ctx: Context<Hello>) -> Result<()> {
|
|
err!(MyError::HelloNoMsg)
|
|
}
|
|
|
|
pub fn hello_next(_ctx: Context<Hello>) -> Result<()> {
|
|
err!(MyError::HelloNext)
|
|
}
|
|
|
|
pub fn test_require(_ctx: Context<Hello>) -> Result<()> {
|
|
require!(false, MyError::Hello);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn test_err(_ctx: Context<Hello>) -> Result<()> {
|
|
err!(MyError::Hello)
|
|
}
|
|
|
|
pub fn test_program_error(_ctx: Context<Hello>) -> Result<()> {
|
|
Err(ProgramError::InvalidAccountData.into())
|
|
}
|
|
|
|
pub fn test_program_error_with_source(_ctx: Context<Hello>) -> Result<()> {
|
|
Err(Error::from(ProgramError::InvalidAccountData).with_source(source!()))
|
|
}
|
|
|
|
pub fn mut_error(_ctx: Context<MutError>) -> Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
pub fn has_one_error(_ctx: Context<HasOneError>) -> Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
pub fn signer_error(_ctx: Context<SignerError>) -> Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
pub fn raw_custom_error(_ctx: Context<RawCustomError>) -> Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
pub fn account_not_initialized_error(_ctx: Context<AccountNotInitializedError>) -> Result<()> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct Hello {}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct MutError<'info> {
|
|
#[account(mut)]
|
|
my_account: AccountInfo<'info>,
|
|
}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct HasOneError<'info> {
|
|
#[account(zero, has_one = owner)]
|
|
my_account: Account<'info, HasOneAccount>,
|
|
owner: AccountInfo<'info>,
|
|
}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct SignerError<'info> {
|
|
my_account: Signer<'info>,
|
|
}
|
|
|
|
#[account]
|
|
pub struct HasOneAccount {
|
|
owner: Pubkey,
|
|
}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct RawCustomError<'info> {
|
|
#[account(constraint = *my_account.key == ID @ MyError::HelloCustom)]
|
|
my_account: AccountInfo<'info>,
|
|
}
|
|
|
|
#[account]
|
|
pub struct AnyAccount {}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct AccountNotInitializedError<'info> {
|
|
not_initialized_account: Account<'info, AnyAccount>,
|
|
}
|
|
|
|
#[error_code]
|
|
pub enum MyError {
|
|
#[msg("This is an error message clients will automatically display")]
|
|
Hello,
|
|
HelloNoMsg = 123,
|
|
HelloNext,
|
|
HelloCustom,
|
|
}
|