Skip to content

Commit ffce554

Browse files
authored
style: return Err instead of panic on storer invalid paths (#987)
* style: return Err instead of panic on storer invalid paths This is a quick fix. The errors in the `fs` crate should probably be improved when we have more bandwidth. * fix(test): clone impl
1 parent ec82f2c commit ffce554

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

agent-control/src/opamp/instance_id/on_host/storer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::opamp::instance_id::getter::DataStored;
44
use crate::opamp::instance_id::storer::InstanceIDStorer;
55
use fs::directory_manager::{DirectoryManagementError, DirectoryManager, DirectoryManagerFs};
66
use fs::file_reader::{FileReader, FileReaderError};
7+
use fs::utils::FsError;
78
use fs::writer_file::{FileWriter, WriteError};
89
use fs::LocalFile;
910
use std::fs::Permissions;
@@ -88,7 +89,10 @@ where
8889
// Get a ref to the target file's parent directory
8990
let dest_dir = dest_file
9091
.parent()
91-
.expect("parent directory dest_file should be valid (not empty nor root dir)");
92+
.ok_or(WriteError::from(FsError::InvalidPath(format!(
93+
"no parent directory found for {} (empty or root dir)",
94+
dest_file.display()
95+
))))?;
9296

9397
self.dir_manager
9498
.create(dest_dir, Permissions::from_mode(DIRECTORY_PERMISSIONS))?;

agent-control/src/sub_agent/persister/config_persister.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ pub mod tests {
5959
ErrorDeletingDirectory(delete_error.to_string()),
6060
),
6161
InvalidDirectory(invalid_dir_error) => match invalid_dir_error {
62-
FsError::InvalidPath() => {
63-
PersistError::DirectoryError(InvalidDirectory(FsError::InvalidPath()))
64-
}
62+
FsError::InvalidPath(s) => PersistError::DirectoryError(InvalidDirectory(
63+
FsError::InvalidPath(s.to_string()),
64+
)),
6565
FsError::DotsDisallowed(msg) => PersistError::DirectoryError(
6666
InvalidDirectory(FsError::DotsDisallowed(msg.to_string())),
6767
),
@@ -76,9 +76,9 @@ pub mod tests {
7676
)))
7777
}
7878
InvalidPath(fs_error) => match fs_error {
79-
FsError::InvalidPath() => {
80-
PersistError::FileError(InvalidPath(FsError::InvalidPath()))
81-
}
79+
FsError::InvalidPath(s) => PersistError::FileError(InvalidPath(
80+
FsError::InvalidPath(s.to_string()),
81+
)),
8282
FsError::DotsDisallowed(path) => PersistError::FileError(InvalidPath(
8383
FsError::DotsDisallowed(path.to_string()),
8484
)),

fs/src/utils.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use thiserror::Error;
44

55
#[derive(Error, Debug, Clone)]
66
pub enum FsError {
7-
#[error("invalid path")]
8-
InvalidPath(),
7+
#[error("invalid path: `{0}`")]
8+
InvalidPath(String),
99

1010
#[error("dots disallowed in path `{0}`")]
1111
DotsDisallowed(String),
@@ -14,7 +14,10 @@ pub enum FsError {
1414
#[cfg(target_family = "unix")]
1515
pub fn validate_path(path: &Path) -> Result<(), FsError> {
1616
match path.to_str() {
17-
None => Err(FsError::InvalidPath()),
17+
None => Err(FsError::InvalidPath(format!(
18+
"{} is not valid unicode",
19+
path.to_string_lossy()
20+
))),
1821
Some(valid_path) => {
1922
// disallow dots
2023
let dots_regex = Regex::new(r"\.\.").unwrap();

0 commit comments

Comments
 (0)