From 0020fb1ca21459d5a269dc85b32de3d3727e25b3 Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Wed, 2 Jul 2025 12:08:02 +0200 Subject: [PATCH] create dedicated error for kill_running_server and check_pid use `thiserror` to facilitate creation --- crates/remote_server/src/unix.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/remote_server/src/unix.rs b/crates/remote_server/src/unix.rs index 78833d72ca..e63328094a 100644 --- a/crates/remote_server/src/unix.rs +++ b/crates/remote_server/src/unix.rs @@ -672,12 +672,16 @@ pub fn execute_proxy(identifier: String, is_reconnecting: bool) -> Result<()> { Ok(()) } -fn kill_running_server(pid: u32, paths: &ServerPaths) -> Result<()> { +#[derive(Debug, Error)] +#[error("failed to kill existing server")] +pub(crate) struct KillRunningServerError(std::io::Error); + +fn kill_running_server(pid: u32, paths: &ServerPaths) -> Result<(), KillRunningServerError> { log::info!("killing existing server with PID {}", pid); std::process::Command::new("kill") .arg(pid.to_string()) .output() - .context("failed to kill existing server")?; + .map_err(KillRunningServerError)?; for file in [ &paths.pid_file, @@ -772,7 +776,15 @@ fn spawn_server(paths: &ServerPaths) -> Result<(), SpawnServerError> { Ok(()) } -fn check_pid_file(path: &Path) -> Result> { +#[derive(Debug, Error)] +#[error("Failed to remove PID file for missing process (pid `{pid}`")] +pub(crate) struct CheckPidError { + #[source] + source: std::io::Error, + pid: u32, +} + +fn check_pid_file(path: &Path) -> Result, CheckPidError> { let Some(pid) = std::fs::read_to_string(&path) .ok() .and_then(|contents| contents.parse::().ok()) @@ -797,7 +809,7 @@ fn check_pid_file(path: &Path) -> Result> { log::debug!( "Found PID file, but process with that PID does not exist. Removing PID file." ); - std::fs::remove_file(&path).context("Failed to remove PID file")?; + std::fs::remove_file(&path).map_err(|source| CheckPidError { source, pid })?; Ok(None) } }