Skip to content

Commit

Permalink
feat(core): add context to the filesystem APIs errors, closes #3457 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Feb 17, 2022
1 parent 72ca197 commit 0605383
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changes/fs-endpoints-context.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Added context to the file system endpoint errors.
2 changes: 1 addition & 1 deletion core/tauri/src/api/path.rs
Expand Up @@ -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 {
Expand Down
72 changes: 43 additions & 29 deletions core/tauri/src/endpoints/file_system.rs
Expand Up @@ -9,6 +9,8 @@ use crate::{
};

use super::InvokeContext;
#[allow(unused_imports)]
use anyhow::Context;
use serde::{
de::{Deserializer, Error as DeError},
Deserialize, Serialize,
Expand Down Expand Up @@ -128,14 +130,16 @@ impl Cmd {
path: SafePathBuf,
options: Option<FileOperationOptions>,
) -> super::Result<Vec<u8>> {
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")]
Expand All @@ -145,15 +149,17 @@ impl Cmd {
contents: Vec<u8>,
options: Option<FileOperationOptions>,
) -> 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")]
Expand All @@ -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")]
Expand All @@ -194,7 +199,7 @@ impl Cmd {
&context.package_info,
&context.window,
source,
Some(dir.clone()),
Some(dir),
)?,
resolve_path(
&context.config,
Expand All @@ -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(())
}

Expand All @@ -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(())
Expand All @@ -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(())
Expand All @@ -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(())
}

Expand All @@ -295,7 +306,7 @@ impl Cmd {
&context.package_info,
&context.window,
old_path,
Some(dir.clone()),
Some(dir),
)?,
resolve_path(
&context.config,
Expand All @@ -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)
}
}

Expand All @@ -320,7 +333,7 @@ fn resolve_path<R: Runtime>(
dir: Option<BaseDirectory>,
) -> super::Result<SafePathBuf> {
let env = window.state::<Env>().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::<Scopes>().fs.is_allowed(&path) {
Ok(SafePathBuf(path))
Expand All @@ -330,7 +343,8 @@ fn resolve_path<R: Runtime>(
))
}
}
Err(e) => Err(e.into()),
Err(e) => super::Result::<SafePathBuf>::Err(e.into())
.with_context(|| format!("path: {}, base dir: {:?}", path.0.display(), dir)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/hooks.rs
Expand Up @@ -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)))
}
}

Expand Down

0 comments on commit 0605383

Please sign in to comment.