Skip to content

Commit 0605383

Browse files
authored
feat(core): add context to the filesystem APIs errors, closes #3457 (#3480)
1 parent 72ca197 commit 0605383

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

.changes/fs-endpoints-context.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Added context to the file system endpoint errors.

core/tauri/src/api/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
1616
/// If informed by the API call, all paths will be relative to the path of the given directory.
1717
///
1818
/// For more information, check the [`dirs_next` documentation](https://docs.rs/dirs_next/).
19-
#[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
19+
#[derive(Serialize_repr, Deserialize_repr, Clone, Copy, Debug)]
2020
#[repr(u16)]
2121
#[non_exhaustive]
2222
pub enum BaseDirectory {

core/tauri/src/endpoints/file_system.rs

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::{
99
};
1010

1111
use super::InvokeContext;
12+
#[allow(unused_imports)]
13+
use anyhow::Context;
1214
use serde::{
1315
de::{Deserializer, Error as DeError},
1416
Deserialize, Serialize,
@@ -128,14 +130,16 @@ impl Cmd {
128130
path: SafePathBuf,
129131
options: Option<FileOperationOptions>,
130132
) -> super::Result<Vec<u8>> {
131-
file::read_binary(resolve_path(
133+
let resolved_path = resolve_path(
132134
&context.config,
133135
&context.package_info,
134136
&context.window,
135137
path,
136138
options.and_then(|o| o.dir),
137-
)?)
138-
.map_err(Into::into)
139+
)?;
140+
file::read_binary(&resolved_path)
141+
.with_context(|| format!("path: {}", resolved_path.0.display()))
142+
.map_err(Into::into)
139143
}
140144

141145
#[module_command_handler(fs_write_file, "fs > writeFile")]
@@ -145,15 +149,17 @@ impl Cmd {
145149
contents: Vec<u8>,
146150
options: Option<FileOperationOptions>,
147151
) -> super::Result<()> {
148-
File::create(resolve_path(
152+
let resolved_path = resolve_path(
149153
&context.config,
150154
&context.package_info,
151155
&context.window,
152156
path,
153157
options.and_then(|o| o.dir),
154-
)?)
155-
.map_err(Into::into)
156-
.and_then(|mut f| f.write_all(&contents).map_err(|err| err.into()))
158+
)?;
159+
File::create(&resolved_path)
160+
.with_context(|| format!("path: {}", resolved_path.0.display()))
161+
.map_err(Into::into)
162+
.and_then(|mut f| f.write_all(&contents).map_err(|err| err.into()))
157163
}
158164

159165
#[module_command_handler(fs_read_dir, "fs > readDir")]
@@ -167,17 +173,16 @@ impl Cmd {
167173
} else {
168174
(false, None)
169175
};
170-
dir::read_dir(
171-
resolve_path(
172-
&context.config,
173-
&context.package_info,
174-
&context.window,
175-
path,
176-
dir,
177-
)?,
178-
recursive,
179-
)
180-
.map_err(Into::into)
176+
let resolved_path = resolve_path(
177+
&context.config,
178+
&context.package_info,
179+
&context.window,
180+
path,
181+
dir,
182+
)?;
183+
dir::read_dir(&resolved_path, recursive)
184+
.with_context(|| format!("path: {}", resolved_path.0.display()))
185+
.map_err(Into::into)
181186
}
182187

183188
#[module_command_handler(fs_copy_file, "fs > copyFile")]
@@ -194,7 +199,7 @@ impl Cmd {
194199
&context.package_info,
195200
&context.window,
196201
source,
197-
Some(dir.clone()),
202+
Some(dir),
198203
)?,
199204
resolve_path(
200205
&context.config,
@@ -206,7 +211,8 @@ impl Cmd {
206211
),
207212
None => (source, destination),
208213
};
209-
fs::copy(src, dest)?;
214+
fs::copy(src.clone(), dest.clone())
215+
.with_context(|| format!("source: {}, dest: {}", src.0.display(), dest.0.display()))?;
210216
Ok(())
211217
}
212218

@@ -229,9 +235,11 @@ impl Cmd {
229235
dir,
230236
)?;
231237
if recursive {
232-
fs::create_dir_all(resolved_path)?;
238+
fs::create_dir_all(&resolved_path)
239+
.with_context(|| format!("path: {}", resolved_path.0.display()))?;
233240
} else {
234-
fs::create_dir(resolved_path)?;
241+
fs::create_dir(&resolved_path)
242+
.with_context(|| format!("path: {} (non recursive)", resolved_path.0.display()))?;
235243
}
236244

237245
Ok(())
@@ -256,9 +264,11 @@ impl Cmd {
256264
dir,
257265
)?;
258266
if recursive {
259-
fs::remove_dir_all(resolved_path)?;
267+
fs::remove_dir_all(&resolved_path)
268+
.with_context(|| format!("path: {}", resolved_path.0.display()))?;
260269
} else {
261-
fs::remove_dir(resolved_path)?;
270+
fs::remove_dir(&resolved_path)
271+
.with_context(|| format!("path: {} (non recursive)", resolved_path.0.display()))?;
262272
}
263273

264274
Ok(())
@@ -277,7 +287,8 @@ impl Cmd {
277287
path,
278288
options.and_then(|o| o.dir),
279289
)?;
280-
fs::remove_file(resolved_path)?;
290+
fs::remove_file(&resolved_path)
291+
.with_context(|| format!("path: {}", resolved_path.0.display()))?;
281292
Ok(())
282293
}
283294

@@ -295,7 +306,7 @@ impl Cmd {
295306
&context.package_info,
296307
&context.window,
297308
old_path,
298-
Some(dir.clone()),
309+
Some(dir),
299310
)?,
300311
resolve_path(
301312
&context.config,
@@ -307,7 +318,9 @@ impl Cmd {
307318
),
308319
None => (old_path, new_path),
309320
};
310-
fs::rename(old, new).map_err(Into::into)
321+
fs::rename(&old, &new)
322+
.with_context(|| format!("old: {}, new: {}", old.0.display(), new.0.display()))
323+
.map_err(Into::into)
311324
}
312325
}
313326

@@ -320,7 +333,7 @@ fn resolve_path<R: Runtime>(
320333
dir: Option<BaseDirectory>,
321334
) -> super::Result<SafePathBuf> {
322335
let env = window.state::<Env>().inner();
323-
match crate::api::path::resolve_path(config, package_info, env, path, dir) {
336+
match crate::api::path::resolve_path(config, package_info, env, &path, dir) {
324337
Ok(path) => {
325338
if window.state::<Scopes>().fs.is_allowed(&path) {
326339
Ok(SafePathBuf(path))
@@ -330,7 +343,8 @@ fn resolve_path<R: Runtime>(
330343
))
331344
}
332345
}
333-
Err(e) => Err(e.into()),
346+
Err(e) => super::Result::<SafePathBuf>::Err(e.into())
347+
.with_context(|| format!("path: {}, base dir: {:?}", path.0.display(), dir)),
334348
}
335349
}
336350

core/tauri/src/hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl InvokeError {
9999
/// Create an [`InvokeError`] as a string of the [`anyhow::Error`] message.
100100
#[inline(always)]
101101
pub fn from_anyhow(error: anyhow::Error) -> Self {
102-
Self(JsonValue::String(error.to_string()))
102+
Self(JsonValue::String(format!("{:#}", error)))
103103
}
104104
}
105105

0 commit comments

Comments
 (0)