2021-02-03 00:35:18 -08:00
|
|
|
//! This example demonstrates how custom errors and associated error messsages
|
|
|
|
//! can be defined and transparently propagated to clients.
|
|
|
|
|
2021-01-15 23:05:26 -08:00
|
|
|
use anchor_lang::prelude::*;
|
|
|
|
|
2021-09-07 13:06:15 -07:00
|
|
|
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
|
|
|
|
|
2021-01-15 23:05:26 -08:00
|
|
|
#[program]
|
|
|
|
mod errors {
|
|
|
|
use super::*;
|
2021-06-09 13:02:50 -07:00
|
|
|
|
2021-02-03 00:35:18 -08:00
|
|
|
pub fn hello(_ctx: Context<Hello>) -> Result<()> {
|
2021-01-15 23:05:26 -08:00
|
|
|
Err(MyError::Hello.into())
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:35:18 -08:00
|
|
|
pub fn hello_no_msg(_ctx: Context<Hello>) -> Result<()> {
|
2021-01-15 23:05:26 -08:00
|
|
|
Err(MyError::HelloNoMsg.into())
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:35:18 -08:00
|
|
|
pub fn hello_next(_ctx: Context<Hello>) -> Result<()> {
|
2021-01-15 23:05:26 -08:00
|
|
|
Err(MyError::HelloNext.into())
|
|
|
|
}
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
pub fn mut_error(_ctx: Context<MutError>) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-03 11:58:23 -07:00
|
|
|
pub fn has_one_error(_ctx: Context<HasOneError>) -> Result<()> {
|
2021-06-09 13:02:50 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn signer_error(_ctx: Context<SignerError>) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-21 15:51:20 -07:00
|
|
|
|
|
|
|
pub fn raw_custom_error(_ctx: Context<RawCustomError>) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-18 07:42:07 -08:00
|
|
|
|
|
|
|
pub fn account_not_initialized_error(_ctx: Context<AccountNotInitializedError>) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-15 23:05:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct Hello {}
|
|
|
|
|
2021-06-09 13:02:50 -07:00
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct MutError<'info> {
|
|
|
|
#[account(mut)]
|
|
|
|
my_account: AccountInfo<'info>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
2021-07-03 11:58:23 -07:00
|
|
|
pub struct HasOneError<'info> {
|
2021-08-25 10:27:27 -07:00
|
|
|
#[account(zero, has_one = owner)]
|
2021-09-07 13:06:15 -07:00
|
|
|
my_account: Account<'info, HasOneAccount>,
|
2021-06-09 13:02:50 -07:00
|
|
|
owner: AccountInfo<'info>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct SignerError<'info> {
|
2022-02-08 09:34:18 -08:00
|
|
|
my_account: Signer<'info>,
|
2021-06-09 13:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[account]
|
2021-07-03 11:58:23 -07:00
|
|
|
pub struct HasOneAccount {
|
2021-06-09 13:02:50 -07:00
|
|
|
owner: Pubkey,
|
|
|
|
}
|
|
|
|
|
2021-10-21 15:51:20 -07:00
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct RawCustomError<'info> {
|
|
|
|
#[account(constraint = *my_account.key == ID @ MyError::HelloCustom)]
|
2021-11-18 07:42:07 -08:00
|
|
|
my_account: AccountInfo<'info>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[account]
|
|
|
|
pub struct AnyAccount {}
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct AccountNotInitializedError<'info> {
|
|
|
|
not_initialized_account: Account<'info, AnyAccount>,
|
2021-10-21 15:51:20 -07:00
|
|
|
}
|
|
|
|
|
2021-01-15 23:05:26 -08:00
|
|
|
#[error]
|
|
|
|
pub enum MyError {
|
|
|
|
#[msg("This is an error message clients will automatically display")]
|
|
|
|
Hello,
|
|
|
|
HelloNoMsg = 123,
|
|
|
|
HelloNext,
|
2021-10-21 15:51:20 -07:00
|
|
|
HelloCustom,
|
2021-01-15 23:05:26 -08:00
|
|
|
}
|