You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Rust/Anchor函数返回时如何将自定义错误转为ProgramError类型?

在Anchor框架中自定义ErrorCode无法转换为ProgramError的解决方法

问题描述

使用Rust和Anchor开发Solana程序时,通过#[error_code]宏定义了自定义错误枚举ErrorCode,但在#[program]代码块中尝试用.into()将自定义错误转为ProgramError时,触发如下报错:

the trait bound anchor_lang::prelude::ProgramError: From<ErrorCode> is not satisfied the following other types implement trait From<T>:
<anchor_lang::prelude::ProgramError as From>
<anchor_lang::prelude::ProgramError as From<anchor_lang::error::Error>>
<anchor_lang::prelude::ProgramError as Fromstd::io::Error>
<anchor_lang::prelude::ProgramError as From> required for ErrorCode to implement Into<anchor_lang::prelude::ProgramError>

相关代码片段

程序主逻辑

#[program]
pub mod solana_twitter {
    use super::*;
    pub fn send_tweet(
        ctx: Context<SendTweet>,
        topic: String,
        content: String,
    ) -> ProgramResult {
        let tweet: &mut Account<Tweet> = &mut ctx.accounts.tweet;
        let author: &Signer = &ctx.accounts.author;
        let clock: Clock = Clock::get().unwrap();

        if topic.chars().count() > 50 {
            return Err(ErrorCode::TopicTooLong.into());
        }
        if content.chars().count() > 280 {
            return Err(ErrorCode::ContentTooLong.into());
        }

        tweet.author = *author.key;
        tweet.timestamp = clock.unix_timestamp;
        tweet.topic = topic;
        tweet.content = content;

        Ok(())
    }
}

自定义错误枚举

#[error_code]
pub enum ErrorCode {
    #[msg("The provided topic should be 50 characters long maximum.")]
    TopicTooLong,
    #[msg("The provided content should be 280 characters long maximum.")]
    ContentTooLong,
}

解决方案

Anchor的#[error_code]宏并未为自定义错误实现Into<ProgramError> trait,可通过以下两种方式完成转换:

方式1:用ProgramError::Custom包裹错误码

Anchor会自动为标注#[error_code]的枚举分配唯一u32错误码,直接将变体转为u32后传入ProgramError::Custom即可:

if topic.chars().count() > 50 {
    return Err(ProgramError::Custom(ErrorCode::TopicTooLong as u32));
}
if content.chars().count() > 280 {
    return Err(ProgramError::Custom(ErrorCode::ContentTooLong as u32));
}

方式2:使用Anchor的IntoProgramError trait

  1. 先在代码顶部导入该trait:
use anchor_lang::error::IntoProgramError;
  1. 修改错误返回逻辑:
if topic.chars().count() > 50 {
    return Err(ErrorCode::TopicTooLong.into_program_error());
}
if content.chars().count() > 280 {
    return Err(ErrorCode::ContentTooLong.into_program_error());
}

原理说明

Anchor的#[error_code]宏会为枚举实现AnchorError trait,处理错误码和错误信息逻辑,但未直接实现Into<ProgramError>。IntoProgramError是Anchor专门提供的转换trait,而ProgramError::Custom是Solana SDK承载自定义错误码的标准变体,两种方式都能完成正确转换。

内容的提问来源于stack exchange,提问作者Rafael Moreira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 21:47:28