Skip to main content

fidius_host/
error.rs

1// Copyright 2026 Colliery, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Error types for fidius-host plugin loading and calling.
16
17use fidius_core::PluginError;
18
19/// Errors that can occur when loading a plugin.
20#[derive(Debug, thiserror::Error)]
21pub enum LoadError {
22    #[error("library not found: {path}")]
23    LibraryNotFound { path: String },
24
25    #[error("symbol 'fidius_get_registry' not found in {path}")]
26    SymbolNotFound { path: String },
27
28    #[error("invalid magic bytes (expected FIDIUS\\0\\0)")]
29    InvalidMagic,
30
31    #[error("incompatible registry version: got {got}, expected {expected}")]
32    IncompatibleRegistryVersion { got: u32, expected: u32 },
33
34    #[error("incompatible ABI version: got {got}, expected {expected}")]
35    IncompatibleAbiVersion { got: u32, expected: u32 },
36
37    #[error("interface hash mismatch: got {got:#x}, expected {expected:#x}")]
38    InterfaceHashMismatch { got: u64, expected: u64 },
39
40    #[error("buffer strategy mismatch: plugin uses {got}, host expects {expected}")]
41    BufferStrategyMismatch {
42        got: fidius_core::descriptor::BufferStrategyKind,
43        expected: fidius_core::descriptor::BufferStrategyKind,
44    },
45
46    #[error("architecture mismatch: expected {expected}, got {got}")]
47    ArchitectureMismatch { expected: String, got: String },
48
49    #[error("unknown buffer strategy discriminant: {value}")]
50    UnknownBufferStrategy { value: u8 },
51
52    #[error("signature verification failed for {path}")]
53    SignatureInvalid { path: String },
54
55    #[error("signature required but no .sig file found for {path}")]
56    SignatureRequired { path: String },
57
58    #[error("plugin '{name}' not found")]
59    PluginNotFound { name: String },
60
61    #[error("libloading error: {0}")]
62    LibLoading(#[from] libloading::Error),
63
64    #[error("io error: {0}")]
65    Io(#[from] std::io::Error),
66
67    /// Python loader failed (only produced with the `python` feature on).
68    /// Wraps `fidius_python::PythonLoadError` as a string to keep the
69    /// fidius-host public error enum type-clean across feature gates.
70    #[error("python load failed: {0}")]
71    PythonLoad(String),
72
73    /// WASM component loader failed (only produced with the `wasm` feature on).
74    /// Wraps wasmtime/instantiation errors as a string to keep the public enum
75    /// type-clean across feature gates.
76    #[error("wasm load failed: {0}")]
77    WasmLoad(String),
78}
79
80/// Errors that can occur when calling a plugin method.
81#[derive(Debug, thiserror::Error)]
82pub enum CallError {
83    #[error("serialization error: {0}")]
84    Serialization(String),
85
86    #[error("deserialization error: {0}")]
87    Deserialization(String),
88
89    #[error("plugin error: {0}")]
90    Plugin(PluginError),
91
92    #[error("plugin panicked: {0}")]
93    Panic(String),
94
95    #[error("buffer too small")]
96    BufferTooSmall,
97
98    /// Optional method is not implemented by this plugin — its capability bit is unset.
99    /// Returned when a method marked `#[optional]` is called on a plugin that chose not
100    /// to implement it. Not returned for out-of-range method indices; see `InvalidMethodIndex`.
101    #[error("method not implemented (capability bit {bit} not set)")]
102    NotImplemented { bit: u32 },
103
104    #[error("invalid method index {index} (plugin has {count} method(s))")]
105    InvalidMethodIndex { index: usize, count: u32 },
106
107    #[error("unknown FFI status code: {code}")]
108    UnknownStatus { code: i32 },
109
110    /// A method was dispatched through the wrong wire path — a `#[wire(raw)]`
111    /// method called via the typed path, or vice versa. Backend-agnostic: the
112    /// Python and (future) WASM executors both enforce the raw/typed split.
113    #[error(
114        "wire-mode mismatch on method '{method}': declared wire_raw={declared}, dispatcher used wire_raw={attempted}"
115    )]
116    WireModeMismatch {
117        method: String,
118        declared: bool,
119        attempted: bool,
120    },
121
122    /// A runtime-level fault originating inside an execution backend that is
123    /// *not* a plugin-raised [`PluginError`] — e.g. a future WASM trap
124    /// (unreachable, out-of-bounds) or an interpreter-level failure. Carries
125    /// the backend's runtime name and a message. Plugin-raised errors (Python
126    /// exceptions included) stay in [`CallError::Plugin`] so their structured
127    /// `code`/`message`/`details` (including tracebacks) round-trip.
128    #[error("{runtime} backend error: {message}")]
129    Backend { runtime: String, message: String },
130
131    /// A streaming backend produced bytes that did not decode as a valid
132    /// [`fidius_core::frame::Frame`] (bad tag, truncated, malformed payload).
133    /// Distinct from [`CallError::Deserialization`], which is about an item's
134    /// *contents*; this is about the framing around items. (FIDIUS-I-0026.)
135    #[error("malformed stream frame: {0}")]
136    MalformedFrame(String),
137
138    /// A stream ended without a terminal `END`/`ERROR` frame — the producer
139    /// went away mid-stream (e.g. a dropped backend task, a crashed
140    /// interpreter, a closed channel). (FIDIUS-I-0026.)
141    #[error("stream aborted before end-of-stream")]
142    StreamAborted,
143}
144
145/// Fold the Python backend's call error into the unified [`CallError`].
146///
147/// `fidius-python` deliberately does not depend on `fidius-host` (that would
148/// cycle — the host optionally depends on it), so the conversion lives here,
149/// behind the `python` feature, where both types are visible. Plugin-raised
150/// Python exceptions map to [`CallError::Plugin`] with the traceback preserved
151/// in `PluginError.details` (built by `fidius_python::pyerr_to_plugin_error`).
152#[cfg(feature = "python")]
153impl From<fidius_python::PythonCallError> for CallError {
154    fn from(e: fidius_python::PythonCallError) -> Self {
155        use fidius_python::PythonCallError as P;
156        match e {
157            P::InvalidMethodIndex { index, count } => CallError::InvalidMethodIndex {
158                index,
159                count: count as u32,
160            },
161            P::WireModeMismatch {
162                method,
163                declared,
164                attempted,
165            } => CallError::WireModeMismatch {
166                method: method.to_string(),
167                declared,
168                attempted,
169            },
170            P::InputDecode(msg) => CallError::Deserialization(msg),
171            P::OutputEncode(msg) => CallError::Serialization(msg),
172            P::Plugin(err) => CallError::Plugin(err),
173        }
174    }
175}