agent_hub_runtime/core/
error.rs1use serde::Serialize;
2use std::fmt;
3
4#[derive(Debug, Serialize)]
13pub struct AppError {
14 pub kind: ErrorKind,
15 pub message: String,
16 #[serde(skip_serializing_if = "Option::is_none")]
17 pub details: Option<ErrorDetails>,
18}
19
20#[derive(Debug, Serialize)]
26pub struct ErrorDetails {
27 pub conflicts: Vec<TargetConflictDetail>,
28}
29
30#[derive(Debug, Serialize)]
31pub struct TargetConflictDetail {
32 pub path: String,
33 pub reason: String,
34 pub is_skill: bool,
36}
37
38#[derive(Debug, PartialEq, Serialize)]
39#[serde(rename_all = "snake_case")]
40pub enum ErrorKind {
41 Database,
42 Io,
43 Network,
44 Git,
45 NotFound,
46 InvalidInput,
47 Cancelled,
48 Internal,
49 ProjectRestoreRequired,
51 TargetConflict,
54}
55
56impl fmt::Display for AppError {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "{}", self.message)
59 }
60}
61
62impl AppError {
63 pub fn not_found(msg: impl Into<String>) -> Self {
64 Self {
65 kind: ErrorKind::NotFound,
66 message: msg.into(),
67 details: None,
68 }
69 }
70
71 pub fn project_restore_required(msg: impl Into<String>) -> Self {
72 Self {
73 kind: ErrorKind::ProjectRestoreRequired,
74 message: msg.into(),
75 details: None,
76 }
77 }
78
79 pub fn invalid_input(msg: impl Into<String>) -> Self {
80 Self {
81 kind: ErrorKind::InvalidInput,
82 message: msg.into(),
83 details: None,
84 }
85 }
86
87 #[allow(dead_code)]
88 pub fn cancelled(msg: impl Into<String>) -> Self {
89 Self {
90 kind: ErrorKind::Cancelled,
91 message: msg.into(),
92 details: None,
93 }
94 }
95
96 pub fn db(e: impl fmt::Display) -> Self {
98 Self {
99 kind: ErrorKind::Database,
100 message: e.to_string(),
101 details: None,
102 }
103 }
104
105 pub fn git(e: impl fmt::Display) -> Self {
107 Self {
108 kind: ErrorKind::Git,
109 message: e.to_string(),
110 details: None,
111 }
112 }
113
114 pub fn classify_git_error(e: impl fmt::Display) -> Self {
116 let message = e.to_string();
117 let lower = message.to_ascii_lowercase();
118 if lower.contains("cancelled") || lower.contains("canceled") {
119 Self {
120 kind: ErrorKind::Cancelled,
121 message,
122 details: None,
123 }
124 } else if lower.contains("connection refused")
125 || lower.contains("could not resolve host")
126 || lower.contains("failed to connect")
127 || lower.contains("connection timed out")
128 || lower.contains("network is unreachable")
129 {
130 Self {
131 kind: ErrorKind::Network,
132 message,
133 details: None,
134 }
135 } else {
136 Self {
137 kind: ErrorKind::Git,
138 message,
139 details: None,
140 }
141 }
142 }
143
144 pub fn network(e: impl fmt::Display) -> Self {
146 Self {
147 kind: ErrorKind::Network,
148 message: e.to_string(),
149 details: None,
150 }
151 }
152
153 pub fn io(e: impl fmt::Display) -> Self {
155 Self {
156 kind: ErrorKind::Io,
157 message: e.to_string(),
158 details: None,
159 }
160 }
161
162 pub fn io_with_context(error: anyhow::Error) -> Self {
164 Self::io(super::log_sanitize::sanitize(&format!("{error:#}")))
165 }
166
167 pub fn target_conflict(
171 message: impl Into<String>,
172 conflicts: Vec<TargetConflictDetail>,
173 ) -> Self {
174 Self {
175 kind: ErrorKind::TargetConflict,
176 message: message.into(),
177 details: Some(ErrorDetails { conflicts }),
178 }
179 }
180
181 pub fn internal(e: impl fmt::Display) -> Self {
182 Self {
183 kind: ErrorKind::Internal,
184 message: e.to_string(),
185 details: None,
186 }
187 }
188}
189
190impl std::error::Error for AppError {}
191
192impl From<std::io::Error> for AppError {
193 fn from(e: std::io::Error) -> Self {
194 Self {
195 kind: ErrorKind::Io,
196 message: e.to_string(),
197 details: None,
198 }
199 }
200}
201
202impl From<tokio::task::JoinError> for AppError {
203 fn from(e: tokio::task::JoinError) -> Self {
204 Self {
205 kind: ErrorKind::Internal,
206 message: e.to_string(),
207 details: None,
208 }
209 }
210}
211
212impl From<tauri::Error> for AppError {
213 fn from(e: tauri::Error) -> Self {
214 Self {
215 kind: ErrorKind::Internal,
216 message: e.to_string(),
217 details: None,
218 }
219 }
220}
221
222#[cfg(test)]
223mod tests;