Skip to content

Commit 194280d

Browse files
committed
Added better error message handling
1 parent 97726c2 commit 194280d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,13 @@ fn get_sort_type(sort_t: [bool; 4]) -> DirSortType {
136136
impl Directory {
137137
fn new(dir: std::path::PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
138138
if !std::path::Path::new(&dir).exists() {
139-
println!("{}OS Error (2): Path does not exist.", termion::color::Fg(termion::color::Red));
140-
std::process::exit(1);
139+
return Err(
140+
Box::new(
141+
std::io::Error::from_raw_os_error(2)
142+
)
143+
)
141144
}
145+
142146
if !std::path::Path::new(&dir).is_dir() {
143147
let f = File::new(dir.clone());
144148
match input::Cli::from_args().long {
@@ -413,10 +417,10 @@ impl std::fmt::Debug for File {
413417
res = format!("{}{}", v.get_color_for_type(), res);
414418
}
415419
}
416-
let mut time = &self.modified;
417-
if input::Cli::from_args().created_time {
418-
time = &self.created;
419-
}
420+
421+
let time = if input::Cli::from_args().created_time { &self.created }
422+
else { &self.modified };
423+
420424
writeln!(f, "{} {green}{} {yellow}{} {blue} {}{} {}",
421425
self.perms, self.size, self.user, self.group, time, res,
422426
green = termion::color::Fg(termion::color::LightGreen),
@@ -439,9 +443,10 @@ impl std::fmt::Display for Directory {
439443

440444
fn main() {
441445
println!("{}",
442-
Directory::new(input::Cli::from_args().dir)
443-
.expect("Failed to run natls")
444-
.setup()
446+
match Directory::new(input::Cli::from_args().dir) {
447+
Ok(mut res) => format!("{}", res.setup()),
448+
Err(err) => format!("{}{}", termion::color::Fg(termion::color::Red), err)
449+
}
445450
);
446451
}
447452

0 commit comments

Comments
 (0)