Skip to content

Commit eba538e

Browse files
tbillingtonchoc
andauthored
add basic error handling to scan (#54)
Co-authored-by: choc <choc@chocs-MacBook-Pro.local>
1 parent 1088821 commit eba538e

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

kondo-lib/src/lib.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,19 @@ struct ProjectIter {
197197
it: walkdir::IntoIter,
198198
}
199199

200+
pub enum Red {
201+
IOError(::std::io::Error),
202+
WalkdirError(walkdir::Error),
203+
}
204+
200205
impl Iterator for ProjectIter {
201-
type Item = Project;
206+
type Item = Result<Project, Red>;
202207

203208
fn next(&mut self) -> Option<Self::Item> {
204209
loop {
205210
let entry: walkdir::DirEntry = match self.it.next() {
206211
None => return None,
207-
Some(Err(_)) => continue,
212+
Some(Err(e)) => return Some(Err(Red::WalkdirError(e))),
208213
Some(Ok(entry)) => entry,
209214
};
210215
if !entry.file_type().is_dir() {
@@ -215,15 +220,17 @@ impl Iterator for ProjectIter {
215220
continue;
216221
}
217222
let rd = match entry.path().read_dir() {
218-
Err(_) => continue,
223+
Err(e) => return Some(Err(Red::IOError(e))),
219224
Ok(rd) => rd,
220225
};
221-
for dir_entry in rd.filter_map(|rd| rd.ok()) {
222-
let file_name = match dir_entry.file_name().into_string() {
223-
Err(_) => continue,
224-
Ok(file_name) => file_name,
226+
// intentionally ignoring errors while iterating the ReadDir
227+
// can't return them because we'll lose the context of where we are
228+
for dir_entry in rd.filter_map(|rd| rd.ok()).map(|de| de.file_name()) {
229+
let file_name = match dir_entry.to_str() {
230+
None => continue,
231+
Some(file_name) => file_name,
225232
};
226-
let p_type = match file_name.as_str() {
233+
let p_type = match file_name {
227234
FILE_CARGO_TOML => Some(ProjectType::Cargo),
228235
FILE_PACKAGE_JSON => Some(ProjectType::Node),
229236
FILE_ASSEMBLY_CSHARP => Some(ProjectType::Unity),
@@ -243,17 +250,17 @@ impl Iterator for ProjectIter {
243250
};
244251
if let Some(project_type) = p_type {
245252
self.it.skip_current_dir();
246-
return Some(Project {
253+
return Some(Ok(Project {
247254
project_type,
248255
path: entry.path().to_path_buf(),
249-
});
256+
}));
250257
}
251258
}
252259
}
253260
}
254261
}
255262

256-
pub fn scan<P: AsRef<path::Path>>(p: &P) -> impl Iterator<Item = Project> {
263+
pub fn scan<P: AsRef<path::Path>>(p: &P) -> impl Iterator<Item = Result<Project, Red>> {
257264
ProjectIter {
258265
it: walkdir::WalkDir::new(p)
259266
.follow_links(SYMLINK_FOLLOW)

kondo-ui/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn spawn_scanner_thread(
184184
.spawn(move || loop {
185185
match scan_starter_recv.recv().expect("scan starter thread") {
186186
ScanStarterThreadMsg::StartScan(p) => {
187-
scan(&p).for_each(|project| {
187+
scan(&p).filter_map(|p| p.ok()).for_each(|project| {
188188
let name = project.name();
189189
let project_size = project.size_dirs();
190190
let display = path::Path::new(&name)

kondo/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn main() -> Result<(), Box<dyn Error>> {
6060

6161
let mut clean_all = opt.all;
6262

63-
'project_loop: for project in dirs.iter().flat_map(scan) {
63+
'project_loop: for project in dirs.iter().flat_map(scan).filter_map(|p| p.ok()) {
6464
write_buffer.clear();
6565

6666
let project_artifact_bytes = project

0 commit comments

Comments
 (0)