From b0b0205280a17273184a2a14f4d3c64baa08b2e3 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 18 Jul 2024 04:32:37 +0300 Subject: [PATCH] return error when resource origin path doesn't exist --- core/tauri-utils/src/lib.rs | 4 ++++ core/tauri-utils/src/resources.rs | 14 ++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/tauri-utils/src/lib.rs b/core/tauri-utils/src/lib.rs index c8e363391..d85db0b1a 100644 --- a/core/tauri-utils/src/lib.rs +++ b/core/tauri-utils/src/lib.rs @@ -371,6 +371,10 @@ pub enum Error { #[cfg(feature = "resources")] #[error("could not walk directory `{0}`, try changing `allow_walk` to true on the `ResourcePaths` constructor.")] NotAllowedToWalkDir(std::path::PathBuf), + /// Resourece path doesn't exist + #[cfg(feature = "resources")] + #[error("resource path `{0}` doesn't exist")] + ResourcePathNotFound(std::path::PathBuf), } /// Reconstructs a path from its components using the platform separator then converts it to String and removes UNC prefixes on Windows if it exists. diff --git a/core/tauri-utils/src/resources.rs b/core/tauri-utils/src/resources.rs index 04f0bc60a..fc0235ca4 100644 --- a/core/tauri-utils/src/resources.rs +++ b/core/tauri-utils/src/resources.rs @@ -167,8 +167,12 @@ impl<'a> ResourcePathsIter<'a> { self.next_current_path() } - fn resource_from_path(&mut self, path: &Path) -> Resource { - Resource { + fn resource_from_path(&mut self, path: &Path) -> crate::Result { + if !path.exists() { + return Err(crate::Error::ResourcePathNotFound(path.to_path_buf())); + } + + Ok(Resource { path: path.to_path_buf(), target: self .current_dest @@ -192,7 +196,7 @@ impl<'a> ResourcePathsIter<'a> { } }) .unwrap_or_else(|| resource_relpath(path)), - } + }) } fn next_current_path(&mut self) -> Option> { @@ -223,7 +227,7 @@ impl<'a> ResourcePathsIter<'a> { } } } else { - Some(Ok(self.resource_from_path(&path))) + Some(self.resource_from_path(&path)) } } @@ -470,6 +474,8 @@ mod tests { ("../src/tiles/**/*".into(), "tiles".into()), ("*.toml".into(), "".into()), ("*.conf.json".into(), "json".into()), + ("../non-existent-file".into(), "asd".into()), // invalid case + ("../non/*".into(), "asd".into()), // invalid case ]), true, )