Skip to content

Commit c33bbf4

Browse files
authored
enhance: include the path in ACL I/O errors (#11575)
1 parent 129414f commit c33bbf4

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

.changes/utils-acl-path-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": minor:enhance
3+
"tauri-utils": minor:enhance
4+
---
5+
6+
Include the path in ACL I/O errors.

crates/tauri-utils/src/acl/build.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ const CORE_PLUGIN_PERMISSIONS_TOKEN: &str = "__CORE_PLUGIN__";
4444
fn parse_permissions(paths: Vec<PathBuf>) -> Result<Vec<PermissionFile>, Error> {
4545
let mut permissions = Vec::new();
4646
for path in paths {
47-
let permission_file = fs::read_to_string(&path).map_err(Error::ReadFile)?;
4847
let ext = path.extension().unwrap().to_string_lossy().to_string();
48+
let permission_file = fs::read_to_string(&path).map_err(|e| Error::ReadFile(e, path))?;
4949
let permission: PermissionFile = match ext.as_str() {
5050
"toml" => toml::from_str(&permission_file)?,
5151
"json" => serde_json::from_str(&permission_file)?,
@@ -81,7 +81,8 @@ pub fn define_permissions<F: Fn(&Path) -> bool>(
8181
let pkg_name_valid_path = pkg_name.replace(':', "-");
8282
let permission_files_path = out_dir.join(format!("{}-permission-files", pkg_name_valid_path));
8383
let permission_files_json = serde_json::to_string(&permission_files)?;
84-
fs::write(&permission_files_path, permission_files_json).map_err(Error::WriteFile)?;
84+
fs::write(&permission_files_path, permission_files_json)
85+
.map_err(|e| Error::WriteFile(e, permission_files_path.clone()))?;
8586

8687
if let Some(plugin_name) = pkg_name.strip_prefix("tauri:") {
8788
println!(
@@ -115,7 +116,8 @@ pub fn read_permissions() -> Result<HashMap<String, Vec<PermissionFile>>, Error>
115116
})
116117
{
117118
let permissions_path = PathBuf::from(value);
118-
let permissions_str = fs::read_to_string(&permissions_path).map_err(Error::ReadFile)?;
119+
let permissions_str =
120+
fs::read_to_string(&permissions_path).map_err(|e| Error::ReadFile(e, permissions_path))?;
119121
let permissions: Vec<PathBuf> = serde_json::from_str(&permissions_str)?;
120122
let permissions = parse_permissions(permissions)?;
121123

@@ -139,7 +141,7 @@ pub fn define_global_scope_schema(
139141
out_dir: &Path,
140142
) -> Result<(), Error> {
141143
let path = out_dir.join("global-scope.json");
142-
fs::write(&path, serde_json::to_vec(&schema)?).map_err(Error::WriteFile)?;
144+
fs::write(&path, serde_json::to_vec(&schema)?).map_err(|e| Error::WriteFile(e, path.clone()))?;
143145

144146
if let Some(plugin_name) = pkg_name.strip_prefix("tauri:") {
145147
println!(
@@ -170,7 +172,7 @@ pub fn read_global_scope_schemas() -> Result<HashMap<String, serde_json::Value>,
170172
})
171173
{
172174
let path = PathBuf::from(value);
173-
let json = fs::read_to_string(&path).map_err(Error::ReadFile)?;
175+
let json = fs::read_to_string(&path).map_err(|e| Error::ReadFile(e, path))?;
174176
let schema: serde_json::Value = serde_json::from_str(&json)?;
175177

176178
let plugin_crate_name = plugin_crate_name_var.to_lowercase().replace('_', "-");
@@ -368,7 +370,7 @@ pub fn generate_docs(
368370
format!("{default_permission}\n{PERMISSION_TABLE_HEADER}\n{permission_table}</table>\n");
369371

370372
let reference_path = out_dir.join(PERMISSION_DOCS_FILE_NAME);
371-
write_if_changed(reference_path, docs).map_err(Error::WriteFile)?;
373+
write_if_changed(&reference_path, docs).map_err(|e| Error::WriteFile(e, reference_path))?;
372374

373375
Ok(())
374376
}

crates/tauri-utils/src/acl/capability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ impl CapabilityFile {
261261
/// Load the given capability file.
262262
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, super::Error> {
263263
let path = path.as_ref();
264-
let capability_file = std::fs::read_to_string(path).map_err(super::Error::ReadFile)?;
264+
let capability_file =
265+
std::fs::read_to_string(path).map_err(|e| super::Error::ReadFile(e, path.into()))?;
265266
let ext = path.extension().unwrap().to_string_lossy().to_string();
266267
let file: Self = match ext.as_str() {
267268
"toml" => toml::from_str(&capability_file)?,

crates/tauri-utils/src/acl/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! [Struct Update Syntax]: https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
2323
2424
use serde::{Deserialize, Serialize};
25-
use std::{num::NonZeroU64, str::FromStr, sync::Arc};
25+
use std::{num::NonZeroU64, path::PathBuf, str::FromStr, sync::Arc};
2626
use thiserror::Error;
2727
use url::Url;
2828

@@ -71,20 +71,20 @@ pub enum Error {
7171
LinksName,
7272

7373
/// IO error while reading a file
74-
#[error("failed to read file: {0}")]
75-
ReadFile(std::io::Error),
74+
#[error("failed to read file '{}': {}", _1.display(), _0)]
75+
ReadFile(std::io::Error, PathBuf),
7676

7777
/// IO error while writing a file
78-
#[error("failed to write file: {0}")]
79-
WriteFile(std::io::Error),
78+
#[error("failed to write file '{}': {}", _1.display(), _0)]
79+
WriteFile(std::io::Error, PathBuf),
8080

8181
/// IO error while creating a file
82-
#[error("failed to create file: {0}")]
83-
CreateFile(std::io::Error),
82+
#[error("failed to create file '{}': {}", _1.display(), _0)]
83+
CreateFile(std::io::Error, PathBuf),
8484

8585
/// IO error while creating a dir
86-
#[error("failed to create dir: {0}")]
87-
CreateDir(std::io::Error),
86+
#[error("failed to create dir '{}': {}", _1.display(), _0)]
87+
CreateDir(std::io::Error, PathBuf),
8888

8989
/// [`cargo_metadata`] was not able to complete successfully
9090
#[cfg(feature = "build")]

crates/tauri-utils/src/acl/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ pub fn generate_permissions_schema<P: AsRef<Path>>(
336336
let schema_str = serde_json::to_string_pretty(&schema)?;
337337

338338
let out_dir = out_dir.as_ref().join(PERMISSION_SCHEMAS_FOLDER_NAME);
339-
fs::create_dir_all(&out_dir).map_err(Error::CreateDir)?;
339+
fs::create_dir_all(&out_dir).map_err(|e| Error::CreateDir(e, out_dir.clone()))?;
340340

341341
let schema_path = out_dir.join(PERMISSION_SCHEMA_FILE_NAME);
342-
write_if_changed(&schema_path, schema_str).map_err(Error::WriteFile)?;
342+
write_if_changed(&schema_path, schema_str).map_err(|e| Error::WriteFile(e, schema_path))?;
343343

344344
Ok(())
345345
}

0 commit comments

Comments
 (0)