fix: gracefully handle missing directories in list operations
This commit is contained in:
@@ -185,9 +185,17 @@ pub async fn list_directory(
|
||||
.await
|
||||
.map_err(|err| err.to_list_message())?;
|
||||
|
||||
let metadata = fs::metadata(&resolved_path)
|
||||
.await
|
||||
.map_err(|err| FsCommandError::from(err).to_list_message())?;
|
||||
let metadata = match fs::metadata(&resolved_path).await {
|
||||
Ok(metadata) => metadata,
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
|
||||
return Ok(DirectoryListResult {
|
||||
directory: normalize_path(&resolved_path),
|
||||
path: normalize_path(&resolved_path),
|
||||
entries: Vec::new(),
|
||||
});
|
||||
}
|
||||
Err(err) => return Err(FsCommandError::from(err).to_list_message()),
|
||||
};
|
||||
|
||||
if !metadata.is_dir() {
|
||||
return Err(FsCommandError::NotDirectory.to_list_message());
|
||||
|
||||
@@ -6378,17 +6378,20 @@ async function main(options = {}) {
|
||||
entries: entries.filter(Boolean)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to list directory:', error);
|
||||
const err = error;
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
const code = err.code;
|
||||
if (code === 'ENOENT') {
|
||||
return res.status(404).json({ error: 'Directory not found' });
|
||||
return res.json({
|
||||
path: path.resolve(normalizeDirectoryPath(rawPath)),
|
||||
entries: []
|
||||
});
|
||||
}
|
||||
if (code === 'EACCES') {
|
||||
return res.status(403).json({ error: 'Access to directory denied' });
|
||||
}
|
||||
}
|
||||
console.error('Failed to list directory:', error);
|
||||
res.status(500).json({ error: (error && error.message) || 'Failed to list directory' });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user