feat(core): add context to the filesystem APIs errors, closes #3457 (#3480)

This commit is contained in:
Lucas Fernandes Nogueira
2022-02-16 21:45:40 -03:00
committed by GitHub
parent 72ca1970c6
commit 060538331c
4 changed files with 50 additions and 31 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Added context to the file system endpoint errors.

View File

@@ -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 {

View File

@@ -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<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")]
@@ -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")]
@@ -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<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))
@@ -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)),
}
}

View File

@@ -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)))
}
}