Repository navigation
Expand file tree
/
Copy patherror.rs
More file actions
88 lines (79 loc) · 3.22 KB
/
Copy patherror.rs
File metadata and controls
88 lines (79 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Structured error type shared by every Tauri command.
//!
//! The golden rule for this app: the frontend must never receive a stringified
//! panic or an opaque blob. Every failure that crosses the `invoke` boundary is
//! one of these variants, and it serializes to a small, typed JSON object that
//! the UI can render specifically ("server failed to start" vs. "request timed
//! out" vs. "no such server").
use serde::Serialize;
/// The category of failure. The frontend switches on `kind` to pick an icon,
/// colour and phrasing, so keep these stable and meaningful.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ErrorKind {
/// The requested server id isn't in the registry (never connected, or the
/// connection was already torn down).
NotConnected,
/// We failed to spawn the local process or open the HTTP transport.
ConnectionFailed,
/// The MCP `initialize` handshake failed or the server spoke nonsense.
HandshakeFailed,
/// A request was sent but the server didn't answer within the timeout.
Timeout,
/// The server answered, but with a protocol-level error / bad payload.
Protocol,
/// Local IO / persistence problems (reading or writing the saved server list).
Storage,
/// A caller passed something we can't use (e.g. malformed JSON arguments).
BadRequest,
}
/// The wire shape the frontend sees. `kind` drives UX; `message` is the human
/// detail; `detail` optionally carries extra context (e.g. the raw SDK error).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AppError {
pub kind: ErrorKind,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
}
impl AppError {
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self { kind, message: message.into(), detail: None }
}
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
self.detail = Some(detail.into());
self
}
// Convenience constructors keep the call sites readable.
pub fn not_connected(id: &str) -> Self {
Self::new(ErrorKind::NotConnected, format!("Not connected to server \"{id}\"."))
}
pub fn connection_failed(msg: impl Into<String>) -> Self {
Self::new(ErrorKind::ConnectionFailed, msg)
}
pub fn handshake_failed(msg: impl Into<String>) -> Self {
Self::new(ErrorKind::HandshakeFailed, msg)
}
pub fn timeout(msg: impl Into<String>) -> Self {
Self::new(ErrorKind::Timeout, msg)
}
pub fn protocol(msg: impl Into<String>) -> Self {
Self::new(ErrorKind::Protocol, msg)
}
pub fn storage(msg: impl Into<String>) -> Self {
Self::new(ErrorKind::Storage, msg)
}
pub fn bad_request(msg: impl Into<String>) -> Self {
Self::new(ErrorKind::BadRequest, msg)
}
}
impl std::fmt::Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}: {}", self.kind, self.message)
}
}
impl std::error::Error for AppError {}
/// Every command returns this, so `?` and `.into()` conversions land on a typed
/// error the UI can trust.
pub type CommandResult<T> = std::result::Result<T, AppError>;