From 060538331c138473159cf8fee0fcb7904ca33d3b Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 16 Feb 2022 21:45:40 -0300 Subject: [PATCH] feat(core): add context to the filesystem APIs errors, closes #3457 (#3480) --- .changes/fs-endpoints-context.md | 5 ++ core/tauri/src/api/path.rs | 2 +- core/tauri/src/endpoints/file_system.rs | 72 +++++++++++++++---------- core/tauri/src/hooks.rs | 2 +- 4 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 .changes/fs-endpoints-context.md diff --git a/.changes/fs-endpoints-context.md b/.changes/fs-endpoints-context.md new file mode 100644 index 000000000..20d6743a7 --- /dev/null +++ b/.changes/fs-endpoints-context.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Added context to the file system endpoint errors. diff --git a/core/tauri/src/api/path.rs b/core/tauri/src/api/path.rs index 030c36afb..7be3f594a 100644 --- a/core/tauri/src/api/path.rs +++ b/core/tauri/src/api/path.rs @@ -16,7 +16,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr}; /// If informed by the API call, all paths will be relative to the path of the given directory. /// /// For more information, check the [`dirs_next` documentation](https://docs.rs/dirs_next/). -#[derive(Serialize_repr, Deserialize_repr, Clone, Debug)] +#[derive(Serialize_repr, Deserialize_repr, Clone, Copy, Debug)] #[repr(u16)] #[non_exhaustive] pub enum BaseDirectory { diff --git a/core/tauri/src/endpoints/file_system.rs b/core/tauri/src/endpoints/file_system.rs index 2e9d226c5..d0a2693b9 100644 --- a/core/tauri/src/endpoints/file_system.rs +++ b/core/tauri/src/endpoints/file_system.rs @@ -9,6 +9,8 @@ use crate::{ }; use super::InvokeContext; +#[allow(unused_imports)] +use anyhow::Context; use serde::{ de::{Deserializer, Error as DeError}, Deserialize, Serialize, @@ -128,14 +130,16 @@ impl Cmd { path: SafePathBuf, options: Option, ) -> super::Result> { - file::read_binary(resolve_path( + let resolved_path = resolve_path( &context.config, &context.package_info, &context.window, path, options.and_then(|o| o.dir), - )?) - .map_err(Into::into) + )?; + file::read_binary(&resolved_path) + .with_context(|| format!("path: {}", resolved_path.0.display())) + .map_err(Into::into) } #[module_command_handler(fs_write_file, "fs > writeFile")] @@ -145,15 +149,17 @@ impl Cmd { contents: Vec, options: Option, ) -> super::Result<()> { - File::create(resolve_path( + let resolved_path = resolve_path( &context.config, &context.package_info, &context.window, path, options.and_then(|o| o.dir), - )?) - .map_err(Into::into) - .and_then(|mut f| f.write_all(&contents).map_err(|err| err.into())) + )?; + File::create(&resolved_path) + .with_context(|| format!("path: {}", resolved_path.0.display())) + .map_err(Into::into) + .and_then(|mut f| f.write_all(&contents).map_err(|err| err.into())) } #[module_command_handler(fs_read_dir, "fs > readDir")] @@ -167,17 +173,16 @@ impl Cmd { } else { (false, None) }; - dir::read_dir( - resolve_path( - &context.config, - &context.package_info, - &context.window, - path, - dir, - )?, - recursive, - ) - .map_err(Into::into) + let resolved_path = resolve_path( + &context.config, + &context.package_info, + &context.window, + path, + dir, + )?; + dir::read_dir(&resolved_path, recursive) + .with_context(|| format!("path: {}", resolved_path.0.display())) + .map_err(Into::into) } #[module_command_handler(fs_copy_file, "fs > copyFile")] @@ -194,7 +199,7 @@ impl Cmd { &context.package_info, &context.window, source, - Some(dir.clone()), + Some(dir), )?, resolve_path( &context.config, @@ -206,7 +211,8 @@ impl Cmd { ), None => (source, destination), }; - fs::copy(src, dest)?; + fs::copy(src.clone(), dest.clone()) + .with_context(|| format!("source: {}, dest: {}", src.0.display(), dest.0.display()))?; Ok(()) } @@ -229,9 +235,11 @@ impl Cmd { dir, )?; if recursive { - fs::create_dir_all(resolved_path)?; + fs::create_dir_all(&resolved_path) + .with_context(|| format!("path: {}", resolved_path.0.display()))?; } else { - fs::create_dir(resolved_path)?; + fs::create_dir(&resolved_path) + .with_context(|| format!("path: {} (non recursive)", resolved_path.0.display()))?; } Ok(()) @@ -256,9 +264,11 @@ impl Cmd { dir, )?; if recursive { - fs::remove_dir_all(resolved_path)?; + fs::remove_dir_all(&resolved_path) + .with_context(|| format!("path: {}", resolved_path.0.display()))?; } else { - fs::remove_dir(resolved_path)?; + fs::remove_dir(&resolved_path) + .with_context(|| format!("path: {} (non recursive)", resolved_path.0.display()))?; } Ok(()) @@ -277,7 +287,8 @@ impl Cmd { path, options.and_then(|o| o.dir), )?; - fs::remove_file(resolved_path)?; + fs::remove_file(&resolved_path) + .with_context(|| format!("path: {}", resolved_path.0.display()))?; Ok(()) } @@ -295,7 +306,7 @@ impl Cmd { &context.package_info, &context.window, old_path, - Some(dir.clone()), + Some(dir), )?, resolve_path( &context.config, @@ -307,7 +318,9 @@ impl Cmd { ), None => (old_path, new_path), }; - fs::rename(old, new).map_err(Into::into) + fs::rename(&old, &new) + .with_context(|| format!("old: {}, new: {}", old.0.display(), new.0.display())) + .map_err(Into::into) } } @@ -320,7 +333,7 @@ fn resolve_path( dir: Option, ) -> super::Result { let env = window.state::().inner(); - match crate::api::path::resolve_path(config, package_info, env, path, dir) { + match crate::api::path::resolve_path(config, package_info, env, &path, dir) { Ok(path) => { if window.state::().fs.is_allowed(&path) { Ok(SafePathBuf(path)) @@ -330,7 +343,8 @@ fn resolve_path( )) } } - Err(e) => Err(e.into()), + Err(e) => super::Result::::Err(e.into()) + .with_context(|| format!("path: {}, base dir: {:?}", path.0.display(), dir)), } } diff --git a/core/tauri/src/hooks.rs b/core/tauri/src/hooks.rs index 900c7e037..73bfd8a88 100644 --- a/core/tauri/src/hooks.rs +++ b/core/tauri/src/hooks.rs @@ -99,7 +99,7 @@ impl InvokeError { /// Create an [`InvokeError`] as a string of the [`anyhow::Error`] message. #[inline(always)] pub fn from_anyhow(error: anyhow::Error) -> Self { - Self(JsonValue::String(error.to_string())) + Self(JsonValue::String(format!("{:#}", error))) } }