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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use serde::Serialize;
use std::fmt;
/// Structured error type for Tauri commands.
///
/// Serialized as `{"kind": "Database", "message": "..."}` so the frontend
/// can branch on `kind` while still showing a human-readable `message`.
///
/// `details` carries machine-readable specifics for the few kinds where the
/// caller has to do more than print the message. It is omitted from the wire
/// format when absent, so every existing consumer is unaffected.
#[derive(Debug, Serialize)]
pub struct AppError {
pub kind: ErrorKind,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<ErrorDetails>,
}
/// The paths a deployment refused to write, because they belong to someone
/// else (#363). Nothing at them was touched. Carried only by
/// `ErrorKind::TargetConflict`, which is what says how to read them — the ways
/// out are documented once, in the `agent-hub-cli` skill, not repeated here on
/// every error.
#[derive(Debug, Serialize)]
pub struct ErrorDetails {
pub conflicts: Vec<TargetConflictDetail>,
}
#[derive(Debug, Serialize)]
pub struct TargetConflictDetail {
pub path: String,
pub reason: String,
/// The target path already contains a valid skill (has SKILL.md).
pub is_skill: bool,
}
#[derive(Debug, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorKind {
Database,
Io,
Network,
Git,
NotFound,
InvalidInput,
Cancelled,
Internal,
/// Project content requires explicit consent before a center restore.
ProjectRestoreRequired,
/// A write was refused because the target is not ours to replace. Always
/// carries `ErrorDetails::TargetConflict`.
TargetConflict,
}
impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl AppError {
pub fn not_found(msg: impl Into<String>) -> Self {
Self {
kind: ErrorKind::NotFound,
message: msg.into(),
details: None,
}
}
pub fn project_restore_required(msg: impl Into<String>) -> Self {
Self {
kind: ErrorKind::ProjectRestoreRequired,
message: msg.into(),
details: None,
}
}
pub fn invalid_input(msg: impl Into<String>) -> Self {
Self {
kind: ErrorKind::InvalidInput,
message: msg.into(),
details: None,
}
}
#[allow(dead_code)]
pub fn cancelled(msg: impl Into<String>) -> Self {
Self {
kind: ErrorKind::Cancelled,
message: msg.into(),
details: None,
}
}
/// Convert an `anyhow::Error` originating from database operations.
pub fn db(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Database,
message: e.to_string(),
details: None,
}
}
/// Convert an `anyhow::Error` originating from git operations.
pub fn git(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Git,
message: e.to_string(),
details: None,
}
}
/// Classify a git operation error into cancellation, network, or generic git error.
pub fn classify_git_error(e: impl fmt::Display) -> Self {
let message = e.to_string();
let lower = message.to_ascii_lowercase();
if lower.contains("cancelled") || lower.contains("canceled") {
Self {
kind: ErrorKind::Cancelled,
message,
details: None,
}
} else if lower.contains("connection refused")
|| lower.contains("could not resolve host")
|| lower.contains("failed to connect")
|| lower.contains("connection timed out")
|| lower.contains("network is unreachable")
{
Self {
kind: ErrorKind::Network,
message,
details: None,
}
} else {
Self {
kind: ErrorKind::Git,
message,
details: None,
}
}
}
/// Convert an `anyhow::Error` originating from network operations.
pub fn network(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Network,
message: e.to_string(),
details: None,
}
}
/// Convert an `anyhow::Error` originating from IO operations.
pub fn io(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Io,
message: e.to_string(),
details: None,
}
}
/// Preserve the cause chain of a contextual filesystem failure for the UI.
pub fn io_with_context(error: anyhow::Error) -> Self {
Self::io(super::log_sanitize::sanitize(&format!("{error:#}")))
}
/// A deployment refusal, with the paths that stopped it. Callers that
/// drive this programmatically (the CLI, and the frontend toast) need the
/// paths, not a sentence containing them.
pub fn target_conflict(
message: impl Into<String>,
conflicts: Vec<TargetConflictDetail>,
) -> Self {
Self {
kind: ErrorKind::TargetConflict,
message: message.into(),
details: Some(ErrorDetails { conflicts }),
}
}
pub fn internal(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Internal,
message: e.to_string(),
details: None,
}
}
}
impl std::error::Error for AppError {}
impl From<std::io::Error> for AppError {
fn from(e: std::io::Error) -> Self {
Self {
kind: ErrorKind::Io,
message: e.to_string(),
details: None,
}
}
}
impl From<tokio::task::JoinError> for AppError {
fn from(e: tokio::task::JoinError) -> Self {
Self {
kind: ErrorKind::Internal,
message: e.to_string(),
details: None,
}
}
}
impl From<tauri::Error> for AppError {
fn from(e: tauri::Error) -> Self {
Self {
kind: ErrorKind::Internal,
message: e.to_string(),
details: None,
}
}
}
#[cfg(test)]
mod tests;