Solana合约的错误处理
文章主要介绍了 Solana 合约开发中的错误处理,包括使用 Rust 语言开发的相关机制,列举了多种错误类型如 ProgramError 及其子类型,如自定义错误、参数无效等,还阐述了错误处理的好处,如明确性、可维护性和优化用户体验等。
错误处理
use {
std::{
result::Result as ResultGeneric,
},
};
pub type ProgramResult = ResultGeneric<(), ProgramError>;错误类型
/// Reasons the program may fail
#[derive(Clone, Debug, Deserialize, Eq, Error, PartialEq, Serialize)]
pub enum ProgramError {
/// Allows on-chain programs to implement program-specific error types and see them returned
/// by the Solana runtime. A program-specific error may be any type that is represented as
/// or serialized to a u32 integer.
#[error("Custom program error: {0:#x}")]
Custom(u32),
#[error("The arguments provided to a program instruction were invalid")]
InvalidArgument,
#[error("An instruction's data contents was invalid")]
InvalidInstructionData,
#[error("An account's data contents was invalid")]
InvalidAccountData,
#[error("An account's data was too small")]
AccountDataTooSmall,
#[error("An account's balance was too small to complete the instruction")]
InsufficientFunds,
#[error("The account did not have the expected program id")]
IncorrectProgramId,
#[error("A signature was required but not found")]
MissingRequiredSignature,
#[error("An initialize instruction was sent to an account that has already been initialized")]
AccountAlreadyInitialized,
#[error("An attempt to operate on an account that hasn't been initialized")]
UninitializedAccount,
#[error("The instruction expected additional account keys")]
NotEnoughAccountKeys,
#[error("Failed to borrow a reference to account data, already borrowed")]
AccountBorrowFailed,
#[error("Length of the seed is too long for address generation")]
MaxSeedLengthExceeded,
#[error("Provided seeds do not result in a valid address")]
InvalidSeeds,
#[error("IO Error: {0}")]
BorshIoError(String),
#[error("An account does not have enough lamports to be rent-exempt")]
AccountNotRentExempt,
#[error("Unsupported sysvar")]
UnsupportedSysvar,
#[error("Provided owner is not allowed")]
IllegalOwner,
#[error("Accounts data allocations exceeded the maximum allowed per transaction")]
MaxAccountsDataAllocationsExceeded,
#[error("Account data reallocation was invalid")]
InvalidRealloc,
#[error("Instruction trace length exceeded the maximum allowed per transaction")]
MaxInstructionTraceLengthExceeded,
#[error("Builtin programs must consume compute units")]
BuiltinProgramsMustConsumeComputeUnits,
}Custom(u32) :
Custom(u32) :InvalidArgument:
InvalidArgument:InvalidInstructionData:
InvalidInstructionData:InvalidAccountData:
InvalidAccountData:IncorrectProgramId:
IncorrectProgramId:MissingRequiredSignature:
MissingRequiredSignature:AccountAlreadyInitialized:
AccountAlreadyInitialized:UninitializedAccount:
UninitializedAccount:NotEnoughAccountKeys:
NotEnoughAccountKeys:AccountBorrowFailed:
AccountBorrowFailed:MaxSeedLengthExceeded:
MaxSeedLengthExceeded:InvalidSeeds:
InvalidSeeds:BorshIoError(String) :
BorshIoError(String) :AccountNotRentExempt:
AccountNotRentExempt:UnsupportedSysvar:
UnsupportedSysvar:IllegalOwner:
IllegalOwner:MaxAccountsDataAllocationsExceeded:
MaxAccountsDataAllocationsExceeded:InvalidRealloc:
InvalidRealloc:MaxInstructionTraceLengthExceeded:
MaxInstructionTraceLengthExceeded:BuiltinProgramsMustConsumeComputeUnits:
BuiltinProgramsMustConsumeComputeUnits:错误处理的好处
最后更新于