Refactor spawn_process fn to handle spawn error

This commit modifies the spawn_process function to explicitly return a Result type, handling the error of the spawn() method. This change improves the readability of the error handling and makes it more idiomatic in Rust. As a secondary correction, it fixes a typo in a print statement.
This commit is contained in:
Ian Wijma 2023-11-21 00:31:37 +11:00
parent 8e6950270e
commit 563938e51a
1 changed files with 6 additions and 3 deletions

View File

@ -84,7 +84,7 @@ fn check_history(retry: &Retry) -> bool {
return retry.history.len().lt(&usize::from(retry.max_retries)) return retry.history.len().lt(&usize::from(retry.max_retries))
} }
fn spawn_process(retry: &Retry) -> std::io::Result<Child> { fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
let mut command_parts: Vec<String> = retry let mut command_parts: Vec<String> = retry
.command .command
.split_whitespace() .split_whitespace()
@ -95,7 +95,10 @@ fn spawn_process(retry: &Retry) -> std::io::Result<Child> {
command_parts.remove(0); command_parts.remove(0);
command.args(command_parts); command.args(command_parts);
return command.spawn(); match command.spawn() {
Ok(child) => Ok(child),
Err(err) => Err(err),
}
} }
fn run_command(retry: &mut Retry) { fn run_command(retry: &mut Retry) {
@ -120,7 +123,7 @@ fn run_command(retry: &mut Retry) {
println!("Restarting..."); println!("Restarting...");
restart(retry); restart(retry);
} else { } else {
print!("The process has crashed more then {} times in the past {}, stop restarting\n", retry.max_retries, retry.restart_name); println!("The process has crashed more than {} times in the past {}, stop restarting\n", retry.max_retries, retry.restart_name);
} }
} }
} }