Skip to main content

graphrt_plugin/
error.rs

1//! Error types.
2
3use std::path::PathBuf;
4
5use graphrt_core::NodeError;
6
7/// Error loading, installing or registering plugins.
8#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
9#[non_exhaustive]
10pub enum PluginError {
11    #[error("invalid plugin manifest: {0}")]
12    Manifest(String),
13    #[error("{}: {message}", path.display())]
14    Io { path: PathBuf, message: String },
15    #[error("plugin `{plugin}`: node kind `{kind}` is already registered")]
16    KindConflict { plugin: String, kind: String },
17    #[error("plugin `{0}` is loaded twice")]
18    DuplicatePlugin(String),
19    #[error("plugin `{plugin}`: no loader registered for runtime `{runtime}`")]
20    NoLoader { plugin: String, runtime: String },
21    #[error("plugin `{plugin}`: runtime `{runtime}` is not enabled in this build")]
22    RuntimeDisabled { plugin: String, runtime: String },
23    #[error("plugin `{plugin}`: checksum mismatch for `{file}`")]
24    Checksum { plugin: String, file: String },
25    #[error("plugin `{plugin}`: {message}")]
26    Runtime { plugin: String, message: String },
27    #[error("install failed: {0}")]
28    Install(String),
29}
30
31/// Failure of one protocol call.
32#[derive(Debug, Clone, PartialEq, thiserror::Error)]
33#[non_exhaustive]
34pub enum CallError {
35    /// The plugin answered with a JSON-RPC error.
36    #[error("{message}")]
37    Remote { code: i64, message: String, data: Option<serde_json::Value> },
38    /// The plugin process exited or the connection broke.
39    #[error("plugin crashed: {0}")]
40    Crashed(String),
41    /// The plugin could not be started.
42    #[error("plugin failed to start: {0}")]
43    Start(String),
44    /// The plugin violated the protocol.
45    #[error("protocol error: {0}")]
46    Protocol(String),
47    /// The request timed out.
48    #[error("plugin request timed out after {0} ms")]
49    Timeout(u64),
50    /// Transport-level failure (HTTP).
51    #[error("transport error: {0}")]
52    Transport(String),
53}
54
55/// JSON-RPC "method not found".
56pub const METHOD_NOT_FOUND: i64 = -32601;
57
58impl CallError {
59    /// Whether this is a JSON-RPC "method not found" error.
60    pub fn is_method_not_found(&self) -> bool {
61        matches!(self, CallError::Remote { code, .. } if *code == METHOD_NOT_FOUND)
62    }
63
64    /// Convert to a node error.
65    pub fn into_node_error(self, plugin: &str) -> NodeError {
66        match self {
67            CallError::Remote { code, message, data } => {
68                let data = data.unwrap_or(serde_json::Value::Null);
69                // Standard JSON-RPC codes (parse error, invalid request, method not
70                // found, invalid params, internal error) signal protocol misuse.
71                let standard = matches!(code, -32700 | -32600 | -32601 | -32602 | -32603);
72                if standard && data.get("kind").is_none() {
73                    return NodeError::fatal("plugin_protocol", format!("plugin `{plugin}`: {message} (code {code})"));
74                }
75                let kind = data.get("kind").and_then(|k| k.as_str()).unwrap_or("plugin_error").to_owned();
76                let retryable = data.get("retryable").and_then(serde_json::Value::as_bool).unwrap_or(false);
77                let retry_after = data.get("retry_after_ms").and_then(serde_json::Value::as_u64);
78                let mut err = if retryable { NodeError::new(kind, message) } else { NodeError::fatal(kind, message) };
79                err = err.with_retry_after(retry_after);
80                err
81            }
82            CallError::Crashed(m) => NodeError::new("plugin_crashed", format!("plugin `{plugin}` crashed: {m}")),
83            CallError::Start(m) => NodeError::new("plugin_start", format!("plugin `{plugin}` failed to start: {m}")),
84            CallError::Protocol(m) => NodeError::fatal("plugin_protocol", format!("plugin `{plugin}`: {m}")),
85            CallError::Timeout(ms) => NodeError::new("timeout", format!("plugin `{plugin}` timed out after {ms} ms")),
86            CallError::Transport(m) => NodeError::new("plugin_transport", format!("plugin `{plugin}`: {m}")),
87        }
88    }
89}