handle strip_prefix error

This commit is contained in:
Nikita Galaiko 2023-04-27 10:43:50 +02:00
parent 4bfd84ee86
commit 13da9d8f87
2 changed files with 13 additions and 5 deletions

View File

@ -59,9 +59,16 @@ impl Dispatcher {
continue;
}
for file_path in event.paths {
let relative_file_path =
file_path.strip_prefix(&self.project_path).unwrap();
if let Err(e) = rtx.send(relative_file_path.to_path_buf()) {
if let Err(e) = file_path
.strip_prefix(&self.project_path)
.with_context(|| {
format!(
"failed to striprefix from file path: {}",
file_path.display()
)
})
.map(|relative_file_path| rtx.send(relative_file_path.to_path_buf()))
{
log::error!(
"{}: failed to send file change event: {:#}",
self.project_id,

View File

@ -1,9 +1,10 @@
use std::path::{Path, PathBuf};
use anyhow::Result;
use walkdir::WalkDir;
// Returns an ordered list of relative paths for files inside a directory recursively.
pub fn list_files<P: AsRef<Path>>(dir_path: P) -> Result<Vec<PathBuf>, std::io::Error> {
pub fn list_files<P: AsRef<Path>>(dir_path: P) -> Result<Vec<PathBuf>> {
let mut files = vec![];
let dir_path = dir_path.as_ref();
if !dir_path.exists() {
@ -13,7 +14,7 @@ pub fn list_files<P: AsRef<Path>>(dir_path: P) -> Result<Vec<PathBuf>, std::io::
let entry = entry?;
if entry.file_type().is_file() {
let path = entry.path();
let path = path.strip_prefix(dir_path).unwrap();
let path = path.strip_prefix(dir_path)?;
let path = path.to_path_buf();
files.push(path);
}