Skip to main content

nichlink_plugin_host/
error.rs

1//! Failure vocabulary returned at the plugin-host seam.
2//! 插件宿主接口返回的失败词汇。
3
4use std::{fmt, io};
5
6/// Failure returned at the plugin-host seam.
7/// 插件宿主接口返回的失败。
8#[derive(Debug)]
9pub enum HostError {
10    /// The artifact failed structural checks such as checksum or Wasm validation.
11    /// 工件未通过校验和、Wasm 格式等结构检查。
12    InvalidArtifact(String),
13    /// The operation name is not a valid plugin identifier.
14    /// 操作名不是合法的插件标识符。
15    InvalidOperation(String),
16    /// The plugin's exports or ABI version contradict the host contract.
17    /// 插件的导出或 ABI 版本与宿主契约不符。
18    Abi(String),
19    /// A declared resource budget (fuel, memory, input, or output) was exceeded.
20    /// 超出声明的资源预算(燃料、内存、输入或输出)。
21    Limit(String),
22    /// The child process could not start, exited non-zero, or broke its pipes.
23    /// 子进程无法启动、非零退出或管道中断。
24    Process(String),
25    /// The call exceeded its hard deadline and the host killed it.
26    /// 调用超出硬超时,宿主已将其中止。
27    Timeout,
28    /// The health probe answered something other than `ok`.
29    /// 健康探针的回答不是 `ok`。
30    Health(String),
31    /// The slot is unknown, malformed, or carries no installed generation.
32    /// 插件槽未知、定义非法,或没有已安装的代际。
33    Slot(String),
34    /// The slot's trust lane or manifest policy rejected the artifact.
35    /// 插件槽的信任通道或清单策略拒绝了该工件。
36    Policy(String),
37    /// The artifact's flow contract does not match the slot's contract.
38    /// 工件的数据流合同与插件槽的合同不匹配。
39    Contract(String),
40    /// Host bookkeeping state is unusable, typically a poisoned lock.
41    /// 宿主簿记状态不可用,通常是锁被毒化。
42    State(String),
43    /// The registry graft needed to publish a deployment failed.
44    /// 发布部署所需的注册树嫁接失败。
45    Registry(String),
46    /// Underlying I/O failure; the only variant that exposes a source error.
47    /// 底层 I/O 失败;唯一会暴露源错误的变体。
48    Io(io::Error),
49}
50
51impl fmt::Display for HostError {
52    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Self::InvalidArtifact(message) => {
55                write!(formatter, "invalid plugin artifact: {message}")
56            }
57            Self::InvalidOperation(message) => {
58                write!(formatter, "invalid plugin operation: {message}")
59            }
60            Self::Abi(message) => write!(formatter, "plugin ABI error: {message}"),
61            Self::Limit(message) => write!(formatter, "plugin limit exceeded: {message}"),
62            Self::Process(message) => write!(formatter, "plugin process failed: {message}"),
63            Self::Timeout => formatter.write_str("plugin call timed out"),
64            Self::Health(message) => write!(formatter, "plugin health check failed: {message}"),
65            Self::Slot(message) => write!(formatter, "plugin slot error: {message}"),
66            Self::Policy(message) => {
67                write!(formatter, "plugin policy rejected artifact: {message}")
68            }
69            Self::Contract(message) => write!(formatter, "plugin contract mismatch: {message}"),
70            Self::State(message) => write!(formatter, "plugin host state failed: {message}"),
71            Self::Registry(message) => write!(formatter, "registry graft failed: {message}"),
72            Self::Io(error) => write!(formatter, "plugin I/O failed: {error}"),
73        }
74    }
75}
76
77impl std::error::Error for HostError {
78    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
79        match self {
80            Self::Io(error) => Some(error),
81            _ => None,
82        }
83    }
84}
85
86impl From<io::Error> for HostError {
87    fn from(error: io::Error) -> Self {
88        Self::Io(error)
89    }
90}