Skip to content

Commit c982733

Browse files
authored
feat(core): enhance readDir error message, closes #7379 (#7416)
1 parent 2eab150 commit c982733

4 files changed

Lines changed: 17 additions & 19 deletions

File tree

.changes/enhance-read-dir-error.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Enhance `readDir` API error with path information.

core/tauri/src/api/dir.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,10 @@ pub fn is_dir<P: AsRef<Path>>(path: P) -> crate::api::Result<bool> {
3232
}
3333

3434
fn is_symlink<P: AsRef<Path>>(path: P) -> crate::api::Result<bool> {
35-
// TODO: remove the different implementation once we raise tauri's MSRV to at least 1.58
36-
#[cfg(windows)]
37-
let ret = symlink_metadata(path)
35+
let path = path.as_ref();
36+
symlink_metadata(path)
3837
.map(|md| md.is_symlink())
39-
.map_err(Into::into);
40-
41-
#[cfg(not(windows))]
42-
let ret = symlink_metadata(path)
43-
.map(|md| md.file_type().is_symlink())
44-
.map_err(Into::into);
45-
46-
ret
38+
.map_err(|e| crate::api::Error::PathIo(e, path.to_path_buf()))
4739
}
4840

4941
/// Reads a directory. Can perform recursive operations.
@@ -62,20 +54,19 @@ pub(crate) fn read_dir_with_options<P: AsRef<Path>>(
6254
options: ReadDirOptions<'_>,
6355
) -> crate::api::Result<Vec<DiskEntry>> {
6456
let mut files_and_dirs: Vec<DiskEntry> = vec![];
65-
for entry in fs::read_dir(path)? {
57+
let path = path.as_ref();
58+
for entry in fs::read_dir(path).map_err(|e| crate::api::Error::PathIo(e, path.to_path_buf()))? {
6659
let path = entry?.path();
67-
let path_as_string = path.display().to_string();
6860

69-
if let Ok(flag) = is_dir(&path_as_string) {
61+
if let Ok(flag) = is_dir(&path) {
7062
files_and_dirs.push(DiskEntry {
7163
path: path.clone(),
7264
children: if flag {
7365
Some(
7466
if recursive
75-
&& (!is_symlink(&path_as_string)?
76-
|| options.scope.map(|s| s.is_allowed(&path)).unwrap_or(true))
67+
&& (!is_symlink(&path)? || options.scope.map(|s| s.is_allowed(&path)).unwrap_or(true))
7768
{
78-
read_dir_with_options(&path_as_string, true, options)?
69+
read_dir_with_options(&path, true, options)?
7970
} else {
8071
vec![]
8172
},

core/tauri/src/api/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub enum Error {
4949
/// IO error.
5050
#[error(transparent)]
5151
Io(#[from] std::io::Error),
52+
/// IO error at the given path.
53+
#[error("{0}, path {1}")]
54+
PathIo(std::io::Error, std::path::PathBuf),
5255
/// Ignore error.
5356
#[error("failed to walkdir: {0}")]
5457
Ignore(#[from] ignore::Error),

core/tauri/src/endpoints/file_system.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,12 @@ impl Cmd {
198198
dir,
199199
)?;
200200
dir::read_dir_with_options(
201-
&resolved_path,
201+
resolved_path,
202202
recursive,
203203
dir::ReadDirOptions {
204204
scope: Some(&context.window.state::<Scopes>().fs),
205205
},
206206
)
207-
.with_context(|| format!("path: {}", resolved_path.display()))
208207
.map_err(Into::into)
209208
}
210209

0 commit comments

Comments
 (0)