Skip to content

Commit e0012dc

Browse files
committed
fix: improve error handling and avoid the panic
1 parent 1c8a53d commit e0012dc

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub enum ServiceError {
1414
)]
1515
NoWriteAccess,
1616
#[error("{0}")]
17+
InvalidConfig(String),
18+
#[error("{0}")]
1719
FromString(String),
1820
#[error("{0}")]
1921
TransportError(#[from] TransportError),

src/fs_service/core.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ pub struct FileSystemService {
1818

1919
impl FileSystemService {
2020
pub fn try_new(allowed_directories: &[String]) -> ServiceResult<Self> {
21-
let normalized_dirs: Vec<PathBuf> = allowed_directories
21+
let normalized_dirs: ServiceResult<Vec<PathBuf>> = allowed_directories
2222
.iter()
2323
.map(fix_dockerhub_mcp_registry_gateway)
24-
.map_while(|dir| {
24+
.map(|dir| {
2525
let expand_result = expand_home(dir.into());
2626
if !expand_result.is_dir() {
27-
panic!("{}", format!("Error: {dir} is not a directory"));
27+
return Err(ServiceError::InvalidConfig(format!(
28+
"Error: The path `{dir}` is not a valid directory. Please double-check your server configuration to ensure the directory exists and is accessible."
29+
)));
2830
}
29-
Some(expand_result)
31+
Ok(expand_result)
3032
})
3133
.collect();
3234

3335
Ok(Self {
34-
allowed_path: RwLock::new(Arc::new(normalized_dirs)),
36+
allowed_path: RwLock::new(Arc::new(normalized_dirs?)),
3537
})
3638
}
3739

0 commit comments

Comments
 (0)