From 49e97f7ef8e26e9c1691dc92ec8cc5e2ca7ce8f3 Mon Sep 17 00:00:00 2001 From: Mike Dallas Date: Thu, 3 Nov 2022 21:25:35 +0000 Subject: [PATCH] add some error handling --- src/main.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 23fd071..e0bdaec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,11 +125,25 @@ async fn main() -> Result<(), GertError> { let use_human_readable = matches.is_present("human_readable"); // restrict downloads to these subreddits let subreddits: Vec<&str> = matches.values_of("subreddits").unwrap().collect(); - let limit = matches.value_of("limit").unwrap().parse::().expect("Limit must be a number"); + let limit = match matches.value_of("limit").unwrap().parse::() { + Ok(limit) => limit, + Err(_) => { + let err = clap::Error::with_description("Limit must be a number", clap::ErrorKind::InvalidValue); + err.exit(); + } + }; let period = matches.value_of("period"); let feed = matches.value_of("feed").unwrap(); let pattern = match matches.value_of("match") { - Some(pattern) => regex::Regex::new(pattern).expect("Invalid regex pattern"), + Some(pattern) => { + match regex::Regex::new(pattern) { + Ok(reg) => reg, + Err(_) => { + let err = clap::Error::with_description("Invalid regex pattern", clap::ErrorKind::InvalidValue); + err.exit(); + } + } + }, None => regex::Regex::new(".*").unwrap(), };