Skip to main content

GaiaError

Struct GaiaError 

Source
pub struct GaiaError { /* private fields */ }
Expand description

Gaia 错误类型,包装了具体的错误类 GaiaErrorKind

这里使用 Box 来减少枚举的大小,提高性能

Implementations§

Source§

impl GaiaError

Source

pub fn syntax_error(message: impl ToString, location: SourceLocation) -> Self

创建一个语法错误

当源代码解析过程中发现语法问题时使用此函数创建错误

§参数
  • message - 错误消息,描述具体的语法问题
  • location - 错误发生的源代码位置信息
§返回值

返回一个包含语法错误信息的GaiaError实例

§示例
let location = SourceLocation::default();
let error = GaiaError::syntax_error("缺少分号", location);
Source

pub fn io_error(io_error: Error, url: Url) -> Self

创建一个IO错误

当文件读写、网络请求等IO操作失败时使用此函数创建错误

§参数
  • io_error - 底层的IO错误
  • url - 与IO操作相关的URL(如文件路径或网络地址)
§返回值

返回一个包含IO错误信息的GaiaError实例

§示例
use gaia_types::GaiaError;
use url::Url;
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "文件不存在");
let url = Url::from_file_path("/path/to/file")
    .ok()
    .and_then(|x| Some(x))
    .unwrap_or_else(|| Url::parse("file:///path/to/file").unwrap());
let error = GaiaError::io_error(io_err, url);
Source

pub fn invalid_instruction( instruction: impl ToString, architecture: Architecture, ) -> Self

创建一个无效指令错误

当解析到未知或不支持的指令时使用此函数创建错误

§参数
  • instruction - 无效的指令字符串
  • architecture - 指令所属的架构
§返回值

返回一个包含无效指令错误信息的GaiaError实例

§示例
use gaia_types::{helpers::Architecture, GaiaError};
let error = GaiaError::invalid_instruction("未知指令", Architecture::X86);
Source

pub fn unsupported_architecture(architecture: Architecture) -> Self

创建一个不支持的架构错误

当尝试在不支持的架构上执行操作时使用此函数创建错误

§参数
  • architecture - 不支持的架构
§返回值

返回一个包含不支持的架构错误信息的GaiaError实例

§示例
use gaia_types::{helpers::Architecture, GaiaError};
let error = GaiaError::unsupported_architecture(Architecture::ARM32);
Source

pub fn invalid_range(length: usize, expect: usize) -> Self

创建一个无效范围错误

当实际数据长度与期望长度不匹配时使用此函数创建错误

§参数
  • length - 实际长度
  • expect - 期望长度
§返回值

返回一个包含无效范围错误信息的GaiaError实例

§示例
use gaia_types::GaiaError;
let error = GaiaError::invalid_range(1024, 2048);
Source

pub fn invalid_data(data: impl ToString) -> Self

创建一个无效数据错误。

§参数
  • data - 无效数据的描述。
§返回值

返回一个包含无效数据错误信息的GaiaError实例。

Source

pub fn invalid_magic_head(head: Vec<u8>, expect: Vec<u8>) -> Self

创建一个无效魔术头错误。

§参数
  • head - 实际的魔术头。
  • expect - 期望的魔术头。
§返回值

返回一个包含无效魔术头错误信息的GaiaError实例。

Source

pub fn kind(&self) -> &GaiaErrorKind

返回错误的种类。

Source

pub fn level(&self) -> &Level

返回错误的级别。

Source

pub fn not_implemented(feature: impl ToString) -> Self

创建一个功能未实现错误

当调用尚未实现的功能时使用此函数创建错误

§参数
  • feature - 未实现功能的描述
§返回值

返回一个包含功能未实现错误信息的GaiaError实例

§示例
let error = GaiaError::not_implemented("PE context creation");
Source

pub fn adapter_error( adapter_name: impl ToString, message: impl ToString, source: Option<Box<GaiaError>>, ) -> Self

创建适配器错误

§参数
  • adapter_name - 适配器名称
  • message - 错误消息
  • source - 可选的源错误
§示例
let error = GaiaError::adapter_error("PeExportAdapter", "导出失败", None);
Source

pub fn platform_unsupported( platform: impl ToString, operation: impl ToString, ) -> Self

创建平台不支持错误

§参数
  • platform - 平台名称
  • operation - 不支持的操作描述
§示例
let error = GaiaError::platform_unsupported("WASI", "内联汇编");
Source

pub fn config_error( config_path: Option<impl ToString>, message: impl ToString, ) -> Self

创建配置错误

§参数
  • config_path - 可选的配置文件路径
  • message - 错误消息
§示例
let error = GaiaError::config_error(Some("config.toml"), "配置文件格式错误");
Source

pub fn unsupported_target(target: CompilationTarget) -> Self

创建不支持的编译目标错误

§参数
  • target - 不支持的编译目标
§示例
let target = CompilationTarget {
    build: Architecture::X86_64,
    host: AbiCompatible::ELF,
    target: ApiCompatible::Gnu,
};
let error = GaiaError::unsupported_target(target);
Source

pub fn compilation_failed( target: CompilationTarget, message: impl ToString, ) -> Self

创建编译失败错误

§参数
  • target - 编译目标
  • message - 错误消息
§示例
let target = CompilationTarget {
    build: Architecture::X86_64,
    host: AbiCompatible::ELF,
    target: ApiCompatible::Gnu,
};
let error = GaiaError::compilation_failed(target, "无法生成字节码");
Source

pub fn save_error(format: impl ToString, message: impl ToString) -> Self

创建一个保存错误。

§参数
  • format - 保存格式。
  • message - 错误消息。
§返回值

返回一个包含保存错误信息的GaiaError实例。

Source

pub fn unsupported_feature( feature: impl ToString, location: SourceLocation, ) -> Self

创建一个不支持的功能错误。

§参数
  • p0 - 不支持的功能描述。
  • p1 - 错误发生的源代码位置信息。
§返回值

返回一个包含不支持的功能错误信息的GaiaError实例。

Source

pub fn custom_error(message: impl ToString) -> GaiaError

创建一个自定义错误。

§参数
  • message - 错误消息。
§返回值

返回一个包含自定义错误信息的GaiaError实例。

Source

pub fn unreachable() -> Self

创建一个不可达错误。

当程序执行到理论上不可达的代码路径时调用此函数。它会自动捕获调用位置。

§返回值

返回一个包含不可达错误信息的 GaiaError 实例。

§示例
fn example_unreachable() -> Result<(), GaiaError> {
    let value = 1;
    match value {
        1 => Ok(()),
        _ => Err(GaiaError::unreachable()), // 此处理论上不可达
    }
}
let _ = example_unreachable();

Trait Implementations§

Source§

impl Debug for GaiaError

为GaiaError实现Debug trait

使用Debug格式输出时,会委托给内部的GaiaErrorKind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for GaiaError

为GaiaError实现Display trait

使用Display格式输出时,会委托给内部的GaiaErrorKind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for GaiaError

为GaiaError实现标准库Error trait

这使得GaiaError可以作为标准错误类型使用

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Error> for GaiaError

Source§

fn from(error: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for GaiaError

Source§

fn from(error: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for GaiaError

Source§

fn from(value: Error) -> Self

Converts to this type from the input type.
Source§

impl From<GaiaErrorKind> for GaiaError

将GaiaErrorKind转换为GaiaError的实现

这个转换会自动将错误种类包装到Box中, 这是GaiaError结构体所要求的,可以减少内存占用

Source§

fn from(error: GaiaErrorKind) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,