-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.rs
More file actions
289 lines (239 loc) · 8.71 KB
/
error.rs
File metadata and controls
289 lines (239 loc) · 8.71 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// Error codes that can be communicated across the IPC boundary.
/// These are serialized as SCREAMING_SNAKE_CASE for TypeScript consumption.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
/// File system I/O error
Io,
/// JSON serialization/deserialization error
Serialization,
/// Error processing a .modpkg file
Modpkg,
/// League of Legends installation not found
LeagueNotFound,
/// Invalid file or directory path
InvalidPath,
/// Requested mod was not found
ModNotFound,
/// Validation failed (e.g., invalid settings)
ValidationFailed,
/// Internal state error (e.g., mutex poisoned)
InternalState,
/// Mutex lock failed (poisoned)
MutexLockFailed,
/// Unknown/unclassified error
Unknown,
/// Workshop directory not configured
WorkshopNotConfigured,
/// Workshop project not found
ProjectNotFound,
/// Workshop project already exists
ProjectAlreadyExists,
/// Failed to pack workshop project
PackFailed,
/// Error processing a .fantome file
Fantome,
/// WAD file error
Wad,
/// Operation blocked because the patcher is running
PatcherRunning,
/// ZIP error
Zip,
}
/// Structured error response sent over IPC.
/// This provides rich error information to the frontend.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AppErrorResponse {
/// Machine-readable error code for pattern matching
pub code: ErrorCode,
/// Human-readable error message
pub message: String,
/// Optional contextual data (e.g., the invalid path, missing mod ID)
#[serde(skip_serializing_if = "Option::is_none")]
pub context: Option<serde_json::Value>,
}
impl AppErrorResponse {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
context: None,
}
}
pub fn with_context(mut self, context: impl Serialize) -> Self {
self.context = serde_json::to_value(context).ok();
self
}
}
/// Result type for IPC commands.
///
/// ```rust
/// #[tauri::command]
/// pub fn my_command() -> IpcResult<String> {
/// my_command_inner().into()
/// }
///
/// fn my_command_inner() -> AppResult<String> {
/// Ok("value".to_string())
/// }
/// ```
///
/// Serializes to: `{ "ok": true, "value": T }` or `{ "ok": false, "error": ... }`
#[derive(Debug, Clone)]
pub enum IpcResult<T> {
Ok { value: T },
Err { error: AppErrorResponse },
}
// Custom serialization to use actual boolean values for the `ok` field
impl<T: Serialize> Serialize for IpcResult<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
match self {
IpcResult::Ok { value } => {
let mut state = serializer.serialize_struct("IpcResult", 2)?;
state.serialize_field("ok", &true)?;
state.serialize_field("value", value)?;
state.end()
}
IpcResult::Err { error } => {
let mut state = serializer.serialize_struct("IpcResult", 2)?;
state.serialize_field("ok", &false)?;
state.serialize_field("error", error)?;
state.end()
}
}
}
}
impl<T> IpcResult<T> {
pub fn ok(value: T) -> Self {
IpcResult::Ok { value }
}
#[allow(dead_code)]
pub fn err(error: impl Into<AppErrorResponse>) -> Self {
IpcResult::Err {
error: error.into(),
}
}
}
impl<T, E: Into<AppErrorResponse>> From<Result<T, E>> for IpcResult<T> {
fn from(result: Result<T, E>) -> Self {
match result {
Ok(value) => IpcResult::Ok { value },
Err(e) => IpcResult::Err { error: e.into() },
}
}
}
/// Internal application error type with rich error information.
/// This is converted to `AppErrorResponse` when crossing the IPC boundary.
#[derive(Debug, Error)]
#[allow(dead_code)]
pub enum AppError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Modpkg error: {0}")]
Modpkg(String),
#[error("League installation not found")]
LeagueNotFound,
#[error("Invalid path: {0}")]
InvalidPath(String),
#[error("Mod not found: {0}")]
ModNotFound(String),
#[error("Validation failed: {0}")]
ValidationFailed(String),
#[error("Internal state error: {0}")]
InternalState(String),
#[error("Failed to acquire mutex lock")]
MutexLockFailed,
#[error("{0}")]
Other(String),
#[error("Workshop directory not configured")]
WorkshopNotConfigured,
#[error("Project not found: {0}")]
ProjectNotFound(String),
#[error("Project already exists: {0}")]
ProjectAlreadyExists(String),
#[error("Failed to pack project: {0}")]
PackFailed(String),
#[error("Fantome error: {0}")]
Fantome(String),
#[error("WAD error: {0}")]
WadError(#[from] ltk_wad::WadError),
#[error("WAD builder error: {0}")]
WadBuilderError(#[from] ltk_wad::WadBuilderError),
#[error("Cannot modify mods while the patcher is running")]
PatcherRunning,
#[error("ZIP error: {0}")]
ZipError(#[from] zip::result::ZipError),
}
impl From<AppError> for AppErrorResponse {
fn from(error: AppError) -> Self {
match error {
AppError::Io(e) => AppErrorResponse::new(ErrorCode::Io, e.to_string()),
AppError::Serialization(e) => {
AppErrorResponse::new(ErrorCode::Serialization, e.to_string())
}
AppError::Modpkg(msg) => AppErrorResponse::new(ErrorCode::Modpkg, msg),
AppError::LeagueNotFound => {
AppErrorResponse::new(ErrorCode::LeagueNotFound, "League installation not found")
}
AppError::InvalidPath(path) => {
AppErrorResponse::new(ErrorCode::InvalidPath, format!("Invalid path: {}", path))
.with_context(serde_json::json!({ "path": path }))
}
AppError::ModNotFound(id) => {
AppErrorResponse::new(ErrorCode::ModNotFound, format!("Mod not found: {}", id))
.with_context(serde_json::json!({ "modId": id }))
}
AppError::ValidationFailed(msg) => {
AppErrorResponse::new(ErrorCode::ValidationFailed, msg)
}
AppError::InternalState(msg) => AppErrorResponse::new(ErrorCode::InternalState, msg),
AppError::MutexLockFailed => {
AppErrorResponse::new(ErrorCode::MutexLockFailed, "Failed to acquire mutex lock")
}
AppError::Other(msg) => AppErrorResponse::new(ErrorCode::Unknown, msg),
AppError::WorkshopNotConfigured => AppErrorResponse::new(
ErrorCode::WorkshopNotConfigured,
"Workshop directory not configured",
),
AppError::ProjectNotFound(name) => AppErrorResponse::new(
ErrorCode::ProjectNotFound,
format!("Project not found: {}", name),
)
.with_context(serde_json::json!({ "projectName": name })),
AppError::ProjectAlreadyExists(name) => AppErrorResponse::new(
ErrorCode::ProjectAlreadyExists,
format!("Project already exists: {}", name),
)
.with_context(serde_json::json!({ "projectName": name })),
AppError::PackFailed(msg) => AppErrorResponse::new(ErrorCode::PackFailed, msg),
AppError::Fantome(msg) => AppErrorResponse::new(ErrorCode::Fantome, msg),
AppError::WadError(e) => AppErrorResponse::new(ErrorCode::Wad, e.to_string()),
AppError::WadBuilderError(e) => AppErrorResponse::new(ErrorCode::Wad, e.to_string()),
AppError::PatcherRunning => AppErrorResponse::new(
ErrorCode::PatcherRunning,
"Stop the patcher before modifying mods",
),
AppError::ZipError(e) => AppErrorResponse::new(ErrorCode::Zip, e.to_string()),
}
}
}
/// Convenience type alias for internal Result usage
pub type AppResult<T> = Result<T, AppError>;
/// Extension trait for converting `Result<T, PoisonError>` to `AppResult<T>`.
pub trait MutexResultExt<T> {
fn mutex_err(self) -> AppResult<T>;
}
impl<T, E> MutexResultExt<T> for Result<T, std::sync::PoisonError<E>> {
fn mutex_err(self) -> AppResult<T> {
self.map_err(|_| AppError::MutexLockFailed)
}
}