create dedicated error for kill_running_server and check_pid

use `thiserror` to facilitate creation
This commit is contained in:
Gwen Lg 2025-07-02 12:08:02 +02:00
parent 45ba5e2c71
commit 0020fb1ca2

View file

@ -672,12 +672,16 @@ pub fn execute_proxy(identifier: String, is_reconnecting: bool) -> Result<()> {
Ok(()) 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); log::info!("killing existing server with PID {}", pid);
std::process::Command::new("kill") std::process::Command::new("kill")
.arg(pid.to_string()) .arg(pid.to_string())
.output() .output()
.context("failed to kill existing server")?; .map_err(KillRunningServerError)?;
for file in [ for file in [
&paths.pid_file, &paths.pid_file,
@ -772,7 +776,15 @@ fn spawn_server(paths: &ServerPaths) -> Result<(), SpawnServerError> {
Ok(()) Ok(())
} }
fn check_pid_file(path: &Path) -> Result<Option<u32>> { #[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<Option<u32>, CheckPidError> {
let Some(pid) = std::fs::read_to_string(&path) let Some(pid) = std::fs::read_to_string(&path)
.ok() .ok()
.and_then(|contents| contents.parse::<u32>().ok()) .and_then(|contents| contents.parse::<u32>().ok())
@ -797,7 +809,7 @@ fn check_pid_file(path: &Path) -> Result<Option<u32>> {
log::debug!( log::debug!(
"Found PID file, but process with that PID does not exist. Removing PID file." "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) Ok(None)
} }
} }