diff options
author | Canop <cano.petrole@gmail.com> | 2024-07-07 13:07:29 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2024-07-07 13:07:29 +0200 |
commit | c37b8d290e934345ce92e86581de1e918bca044c (patch) | |
tree | a5581571012c04b15eb159ac69ed7613a78020fa | |
parent | e15a8e91d7b12eed5a44736dba29505836b37d70 (diff) |
better handle preview transformer failingpreview-converter
-> we use the source file when the transformation failed
-rw-r--r-- | src/errors.rs | 6 | ||||
-rw-r--r-- | src/preview/preview_transformer.rs | 25 |
2 files changed, 22 insertions, 9 deletions
diff --git a/src/errors.rs b/src/errors.rs index 789b091..b66630a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -127,3 +127,9 @@ custom_error! {pub SvgError Internal { message: &'static str } = "Internal error : {message}", Svg {source: resvg::usvg::Error} = "SVG Error: {source}", } + +custom_error! {pub PreviewTransformerError + Io {source: io::Error} = "IO Error : {source}", + ProcessInterrupted = "Process interrupted", + ProcessFailed { code: i32 } = "Execution failed with code {code}", +} diff --git a/src/preview/preview_transformer.rs b/src/preview/preview_transformer.rs index 1a828cc..72f144a 100644 --- a/src/preview/preview_transformer.rs +++ b/src/preview/preview_transformer.rs @@ -118,7 +118,7 @@ impl PreviewTransformer { &self, input_path: &Path, temp_dir: &Path, - ) -> Result<PathBuf, ProgramError> { + ) -> Result<PathBuf, PreviewTransformerError> { let hash = { let mut hasher = DefaultHasher::new(); input_path.hash(&mut hasher); @@ -149,14 +149,21 @@ impl PreviewTransformer { } else { process.stdout(std::fs::File::create(&output_path)?); } - let res = process + let exit_status = process .spawn() - .and_then(|mut p| p.wait()) - .map_err(|source| ProgramError::LaunchError { - program: self.command[0].clone(), - source, - }); - info!("conversion result: {:?}", res); - Ok(output_path) + .and_then(|mut p| p.wait())?; + if exit_status.success() { + Ok(output_path) + } else { + // we remove the output file if the process failed, so that + // it's not returned on the next call + let _ = std::fs::remove_file(&output_path); + match exit_status.code() { + Some(code) => Err(PreviewTransformerError::ProcessFailed { + code, + }), + None => Err(PreviewTransformerError::ProcessInterrupted), + } + } } } |