refactor(backend): phase 1 - unified error handling with thiserror

Introduce AppError enum to replace Result<T, String> pattern across
the codebase, improving error context preservation and type safety.

## Changes

### Core Infrastructure
- Add src/error.rs with AppError enum using thiserror
- Add thiserror dependency to Cargo.toml
- Implement helper functions: io(), json(), toml() for ergonomic error creation
- Implement From<PoisonError> for automatic lock error conversion
- Implement From<AppError> for String to maintain Tauri command compatibility

### Module Migrations (60% complete)
- config.rs: Full migration to AppError
  - read_json_file, write_json_file, atomic_write
  - archive_file, copy_file, delete_file
- claude_mcp.rs: Full migration to AppError
  - get_mcp_status, read_mcp_json, upsert_mcp_server
  - delete_mcp_server, validate_command_in_path
  - set_mcp_servers_map
- codex_config.rs: Full migration to AppError
  - write_codex_live_atomic with rollback support
  - read_and_validate_codex_config_text
  - validate_config_toml
- app_config.rs: Partial migration
  - MultiAppConfig::load, MultiAppConfig::save
- store.rs: Partial migration
  - AppState::save now returns Result<(), AppError>
- commands.rs: Minimal changes
  - Use .map_err(Into::into) for compatibility
- mcp.rs: Minimal changes
  - sync_enabled_to_claude uses Into::into conversion

### Documentation
- Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap

## Benefits
- Type-safe error handling with preserved error chains
- Better error messages with file paths and context
- Reduced boilerplate code (118 Result<T, String> instances to migrate)
- Automatic error conversion for seamless integration

## Testing
- All existing tests pass (4/4)
- Compilation successful with no warnings
- Build time: 0.61s (no performance regression)

## Remaining Work
- claude_plugin.rs (7 functions)
- migration.rs, import_export.rs
- Add unit tests for error.rs
- Complete commands.rs migration after dependent modules

Co-authored-by: Claude <claude@anthropic.com>
This commit is contained in:
Jason
2025-10-27 16:29:11 +08:00
parent bfab1d0ccb
commit c01e495eea
12 changed files with 352 additions and 114 deletions

View File

@@ -315,7 +315,7 @@ pub fn set_enabled_and_sync_for(
/// 将 config.json 中 enabled==true 的项投影写入 ~/.claude.json
pub fn sync_enabled_to_claude(config: &MultiAppConfig) -> Result<(), String> {
let enabled = collect_enabled_servers(&config.mcp.claude);
crate::claude_mcp::set_mcp_servers_map(&enabled)
crate::claude_mcp::set_mcp_servers_map(&enabled).map_err(Into::into)
}
/// 从 ~/.claude.json 导入 mcpServers 到 config.json设为 enabled=true