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