mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-28 08:20:04 +08:00
refactor(profile-switch): integrate stage-aware panic handling
- src-tauri/src/cmd/profile_switch/workflow/state_machine.rs:1 Defines SwitchStage and SwitchPanicInfo as crate-visible, wraps each transition in with_stage(...) with catch_unwind, and propagates CmdResult<bool> to distinguish validation failures from panics while keeping cancellation semantics. - src-tauri/src/cmd/profile_switch/workflow.rs:25 Updates run_switch_job to return Result<bool, SwitchPanicInfo>, routing timeout, validation, config, and stage panic cases separately. Reuses SwitchPanicInfo for logging/UI notifications; patch_profiles_config maps state-machine panics into user-facing error strings. - src-tauri/src/cmd/profile_switch/driver.rs:1 Adds SwitchJobOutcome to unify workflow results: normal completions carry bool, and panics propagate SwitchPanicInfo. The driver loop now logs panics explicitly and uses AssertUnwindSafe(...).catch_unwind() to guard setup-phase panics.
This commit is contained in:
@@ -19,12 +19,13 @@ use tokio::{fs as tokio_fs, time};
|
||||
|
||||
mod state_machine;
|
||||
|
||||
pub(super) use state_machine::SwitchPanicInfo;
|
||||
use state_machine::SwitchStateMachine;
|
||||
|
||||
pub(super) async fn run_switch_job(
|
||||
manager: &'static SwitchManager,
|
||||
request: SwitchRequest,
|
||||
) -> bool {
|
||||
) -> Result<bool, SwitchPanicInfo> {
|
||||
if request.cancel_token().is_cancelled() {
|
||||
logging!(
|
||||
info,
|
||||
@@ -38,7 +39,7 @@ pub(super) async fn run_switch_job(
|
||||
request.notify(),
|
||||
request.task_id(),
|
||||
);
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let profile_id = request.profile_id().clone();
|
||||
@@ -56,7 +57,7 @@ pub(super) async fn run_switch_job(
|
||||
);
|
||||
handle::Handle::notice_message("config_validate::error", err.clone());
|
||||
handle::Handle::notify_profile_switch_finished(profile_id.clone(), false, notify, task_id);
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
logging!(
|
||||
@@ -107,7 +108,7 @@ pub(super) async fn run_switch_job(
|
||||
notify,
|
||||
task_id,
|
||||
);
|
||||
false
|
||||
Ok(false)
|
||||
}
|
||||
Ok(Err(panic_payload)) => {
|
||||
let panic_message = describe_panic_payload(panic_payload.as_ref());
|
||||
@@ -129,54 +130,86 @@ pub(super) async fn run_switch_job(
|
||||
notify,
|
||||
task_id,
|
||||
);
|
||||
false
|
||||
Err(SwitchPanicInfo::workflow_root(panic_message))
|
||||
}
|
||||
Ok(Ok(result)) => match result {
|
||||
Ok(success) => {
|
||||
handle::Handle::notify_profile_switch_finished(
|
||||
profile_id.clone(),
|
||||
success,
|
||||
notify,
|
||||
task_id,
|
||||
);
|
||||
close_connections_after_switch(&profile_id).await;
|
||||
if notify && success {
|
||||
handle::Handle::notice_message("info", "Profile Switched");
|
||||
Ok(Ok(machine_result)) => match machine_result {
|
||||
Ok(cmd_result) => match cmd_result {
|
||||
Ok(success) => {
|
||||
handle::Handle::notify_profile_switch_finished(
|
||||
profile_id.clone(),
|
||||
success,
|
||||
notify,
|
||||
task_id,
|
||||
);
|
||||
close_connections_after_switch(&profile_id).await;
|
||||
if notify && success {
|
||||
handle::Handle::notice_message("info", "Profile Switched");
|
||||
}
|
||||
logging!(
|
||||
info,
|
||||
Type::Cmd,
|
||||
"Profile switch task finished: {} (success={})",
|
||||
profile_id,
|
||||
success
|
||||
);
|
||||
Ok(success)
|
||||
}
|
||||
logging!(
|
||||
info,
|
||||
Type::Cmd,
|
||||
"Profile switch task finished: {} (success={})",
|
||||
profile_id,
|
||||
success
|
||||
);
|
||||
success
|
||||
}
|
||||
Err(err) => {
|
||||
Err(err) => {
|
||||
logging!(
|
||||
error,
|
||||
Type::Cmd,
|
||||
"Profile switch failed ({}): {}",
|
||||
profile_id,
|
||||
err
|
||||
);
|
||||
handle::Handle::notice_message("config_validate::error", err.clone());
|
||||
handle::Handle::notify_profile_switch_finished(
|
||||
profile_id.clone(),
|
||||
false,
|
||||
notify,
|
||||
task_id,
|
||||
);
|
||||
Ok(false)
|
||||
}
|
||||
},
|
||||
Err(panic_info) => {
|
||||
logging!(
|
||||
error,
|
||||
Type::Cmd,
|
||||
"Profile switch failed ({}): {}",
|
||||
"State machine panic during profile switch task {} ({} {:?}): {}",
|
||||
task_id,
|
||||
profile_id,
|
||||
err
|
||||
panic_info.stage,
|
||||
panic_info.detail
|
||||
);
|
||||
handle::Handle::notice_message(
|
||||
"config_validate::panic",
|
||||
format!("profile switch panic: {}", profile_id),
|
||||
);
|
||||
handle::Handle::notice_message("config_validate::error", err.clone());
|
||||
handle::Handle::notify_profile_switch_finished(
|
||||
profile_id.clone(),
|
||||
false,
|
||||
notify,
|
||||
task_id,
|
||||
);
|
||||
false
|
||||
Err(panic_info)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
||||
SwitchStateMachine::new(manager(), None, profiles)
|
||||
match SwitchStateMachine::new(manager(), None, profiles)
|
||||
.run()
|
||||
.await
|
||||
{
|
||||
Ok(result) => result,
|
||||
Err(panic_info) => Err(format!(
|
||||
"profile switch panic ({:?}): {}",
|
||||
panic_info.stage, panic_info.detail
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn validate_profile_yaml(profile: &SmartString) -> CmdResult<bool> {
|
||||
@@ -336,7 +369,7 @@ async fn close_connections_after_switch(profile_id: &SmartString) {
|
||||
}
|
||||
}
|
||||
|
||||
fn describe_panic_payload(payload: &(dyn Any + Send)) -> String {
|
||||
pub(super) fn describe_panic_payload(payload: &(dyn Any + Send)) -> String {
|
||||
if let Some(message) = payload.downcast_ref::<&str>() {
|
||||
(*message).to_string()
|
||||
} else if let Some(message) = payload.downcast_ref::<std::string::String>() {
|
||||
|
||||
Reference in New Issue
Block a user