From 25e63c2aaeebfcf683ca6038c12c8d69bb636b0e Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 8 May 2023 00:43:42 +0200 Subject: [PATCH] fix: remove unwraps and add some error handling. --- src/main.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8a94ad1..9d6a1c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,13 @@ fn main() { .collect::>(); info!("Found {} file(s) to be processed.", input_files.len()); + let mut success_count = 0; + for file in input_files { + let mut fail = |job: &TranscodeJob, remarks: &str| { + error!("Failed to process file {}: {}", job.input, remarks); + }; + let mut output_path = file.clone(); output_path.set_extension(&config.ffmpeg.output.format); let output_path = PathBuf::from(&config.files.output_path).join(output_path.file_name().unwrap()); // TODO: This is a bit of a mess. @@ -43,15 +49,34 @@ fn main() { } info!("Processing file {}.", job.input); - let mut child = job.run(&config.ffmpeg).unwrap(); + let mut child = match job.run(&config.ffmpeg) { + Ok(child) => child, + Err(e) => { + fail(&job, &format!("Failed to start ffmpeg process: {}", e)); + continue; + } + }; if (config.ffmpeg.process.niceness.unwrap_or(0)) > 0 { - renice(child.id(), config.ffmpeg.process.niceness.unwrap_or(0)) - .expect("Failed to renice process."); + match renice(child.id(), config.ffmpeg.process.niceness.unwrap()) { + Ok(_) => {}, + Err(e) => { + error!("Failed to renice ffmpeg process: {}", e); + } + } + } + + if let Err(e) = child.wait() { + fail(&job, &format!("Failed to wait for ffmpeg process: {}", e)); + continue; } - child.wait().expect("Failed to wait for process."); // TODO: Cleanup + + info!("Finished processing file {}.", job.input); + success_count += 1; } + + info!("Finished processing {} file(s).", success_count); } fn setup_logger(config: &configuration::Config) {