Skip to main content

brainwires_agent_network/
error.rs

1use brainwires_mcp::JsonRpcError;
2
3/// Errors that can occur in the agent network layer.
4#[derive(Debug, thiserror::Error)]
5pub enum AgentNetworkError {
6    /// JSON-RPC parse error.
7    #[error("Parse error: {0}")]
8    ParseError(String),
9    /// Requested method does not exist.
10    #[error("Method not found: {0}")]
11    MethodNotFound(String),
12    /// Invalid parameters supplied.
13    #[error("Invalid params: {0}")]
14    InvalidParams(String),
15    /// Internal server error.
16    #[error("Internal error: {0}")]
17    Internal(#[from] anyhow::Error),
18    /// Transport-level error.
19    #[error("Transport error: {0}")]
20    Transport(String),
21    /// Requested tool does not exist.
22    #[error("Tool not found: {0}")]
23    ToolNotFound(String),
24    /// Request was rate-limited.
25    #[error("Rate limited")]
26    RateLimited,
27    /// Request was not authorized.
28    #[error("Unauthorized")]
29    Unauthorized,
30}
31
32impl AgentNetworkError {
33    /// Convert to a JSON-RPC error with the appropriate code.
34    pub fn to_json_rpc_error(&self) -> JsonRpcError {
35        match self {
36            AgentNetworkError::ParseError(msg) => JsonRpcError {
37                code: -32700,
38                message: msg.clone(),
39                data: None,
40            },
41            AgentNetworkError::MethodNotFound(method) => JsonRpcError {
42                code: -32601,
43                message: format!("Method not found: {method}"),
44                data: None,
45            },
46            AgentNetworkError::InvalidParams(msg) => JsonRpcError {
47                code: -32602,
48                message: msg.clone(),
49                data: None,
50            },
51            AgentNetworkError::Internal(err) => JsonRpcError {
52                code: -32603,
53                message: err.to_string(),
54                data: None,
55            },
56            AgentNetworkError::Transport(msg) => JsonRpcError {
57                code: -32000,
58                message: format!("Transport error: {msg}"),
59                data: None,
60            },
61            AgentNetworkError::ToolNotFound(name) => JsonRpcError {
62                code: -32001,
63                message: format!("Tool not found: {name}"),
64                data: None,
65            },
66            AgentNetworkError::RateLimited => JsonRpcError {
67                code: -32002,
68                message: "Rate limited".to_string(),
69                data: None,
70            },
71            AgentNetworkError::Unauthorized => JsonRpcError {
72                code: -32003,
73                message: "Unauthorized".to_string(),
74                data: None,
75            },
76        }
77    }
78}