Refactor: Migrate unwrap calls to AppError in MCP handlers

This commit is contained in:
Riz Ashraf committed 2026-09-30 21:02:50 +01:00
1 parent 4e1a633dbd
commit 0e866f2465
12 files changed
+276 -326

No files matched your search

+10 -10
View File
@@ -24,9 +24,9 @@ impl McpTool for WriteClipboardHandler {
)
}
async fn execute(&self, args: Value, _state: Arc<MemoryState>) -> Result<String, String> {
async fn execute(&self, args: Value, _state: Arc<MemoryState>) -> crate::error::Result<String> {
let tool_args: WriteClipboardTool =
serde_json::from_value(args).map_err(|e| format!("Invalid args: {}", e))?;
serde_json::from_value(args).map_err(|e| crate::error::AppError::Internal(format!("Invalid args: {}", e)))?;
tokio::task::spawn_blocking(move || {
let mut msgs = Vec::new();
@@ -63,11 +63,11 @@ impl McpTool for WriteClipboardHandler {
if clipboard.set_image(img_data).is_ok() {
msgs.push("Wrote image");
} else {
return Err("Failed to write image to clipboard".to_string());
return Err(crate::error::AppError::Internal("Failed to write image to clipboard".to_string()));
}
}
},
Err(e) => return Err(format!("Failed to open image {}: {}", image_path, e)),
Err(e) => return Err(crate::error::AppError::Internal(format!("Failed to open image {}: {}", image_path, e))),
}
}
@@ -78,7 +78,7 @@ impl McpTool for WriteClipboardHandler {
}
})
.await
.map_err(|e| format!("Task panic: {}", e))?
.map_err(|e| crate::error::AppError::Internal(format!("Task panic: {}", e)))?
}
}
@@ -97,8 +97,8 @@ impl McpTool for ReadClipboardHandler {
)
}
async fn execute(&self, _args: Value, _state: Arc<MemoryState>) -> Result<String, String> {
let result = tokio::task::spawn_blocking(move || -> Result<Value, String> {
async fn execute(&self, _args: Value, _state: Arc<MemoryState>) -> crate::error::Result<String> {
let result = tokio::task::spawn_blocking(move || -> crate::error::Result<serde_json::Value> {
let mut out = serde_json::Map::new();
if let Ok(_clip) = Clipboard::new_attempts(3) {
@@ -149,9 +149,9 @@ impl McpTool for ReadClipboardHandler {
Ok(Value::Object(out))
})
.await
.map_err(|e| format!("Task panic: {}", e))??;
.map_err(|e| crate::error::AppError::Internal(format!("Task panic: {}", e)))??;
Ok(serde_json::to_string_pretty(&result).unwrap())
Ok::<String, crate::error::AppError>(serde_json::to_string_pretty(&result)?)
}
}
use crate::tools::ToggleClipboardWatchModeTool;
@@ -171,7 +171,7 @@ impl McpTool for ToggleClipboardWatchModeHandler {
)
}
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String> {
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
let tool_args: ToggleClipboardWatchModeTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let mut watch_mode = state.clipboard_watch_mode.write().await;