diff options
author | jhspetersson <jhspetersson@gmail.com> | 2023-05-10 08:40:10 +0200 |
---|---|---|
committer | jhspetersson <jhspetersson@gmail.com> | 2023-05-10 08:40:10 +0200 |
commit | 1f0ce2c10214b60df8c048f80114100d843c66e9 (patch) | |
tree | d1b7add6bea685f59cc16b12d52738b64fe9af14 | |
parent | 98962f81de2fc0fe3ec38b83199426e32feb6a4d (diff) |
replace panics with graceful exit on errors
-rw-r--r-- | src/function.rs | 11 | ||||
-rw-r--r-- | src/ignore/docker.rs | 4 | ||||
-rw-r--r-- | src/ignore/git.rs | 4 | ||||
-rw-r--r-- | src/ignore/hg.rs | 5 | ||||
-rw-r--r-- | src/searcher.rs | 16 | ||||
-rw-r--r-- | src/util/glob.rs | 6 | ||||
-rw-r--r-- | src/util/mod.rs | 9 |
7 files changed, 27 insertions, 28 deletions
diff --git a/src/function.rs b/src/function.rs index a8ef0fa..1ca8c9c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -17,7 +17,7 @@ use serde::ser::{Serialize, Serializer}; use xattr::FileExt; use crate::fileinfo::FileInfo; -use crate::util::{capitalize, error_message, format_date}; +use crate::util::{capitalize, error_exit, format_date}; use crate::util::format_datetime; use crate::util::parse_datetime; use crate::util::parse_filesize; @@ -206,10 +206,7 @@ impl Variant { Ok((dt_from, dt_to)) => { return (dt_from, dt_to); }, - _ => { - error_message("Can't parse datetime", &self.string_value); - std::process::exit(2); - } + _ => error_exit("Can't parse datetime", &self.string_value) } } @@ -758,11 +755,11 @@ pub fn get_value(function: &Option<Function>, let limit = function_args.get(0).unwrap(); match limit.parse::<i64>() { Ok(limit) => return Variant::from_int(rng.gen_range(val..limit)), - _ => panic!("Could not parse function argument") + _ => error_exit("Could not parse limit argument of RANDOM function", limit.as_str()) } } }, - _ => panic!("Could not parse function argument") + _ => error_exit("Could not parse an argument of RANDOM function", function_arg.as_str()) } }, _ => { diff --git a/src/ignore/docker.rs b/src/ignore/docker.rs index 9927bc7..4396538 100644 --- a/src/ignore/docker.rs +++ b/src/ignore/docker.rs @@ -9,6 +9,8 @@ use regex::Captures; use regex::Error; use regex::Regex; +use crate::util::error_exit; + #[derive(Clone, Debug)] pub struct DockerignoreFilter { pub regex: Regex, @@ -140,7 +142,7 @@ fn convert_dockerignore_glob(glob: &str, file_path: &Path) -> Result<Regex, Erro "." => "\\.", "*" => "[^/]*", "?" => "[^/]", - _ => panic!("Error parsing pattern") + _ => error_exit(".dockerignore", "Error parsing pattern") }.to_string() }).to_string(); diff --git a/src/ignore/git.rs b/src/ignore/git.rs index 82a632d..3e93f3f 100644 --- a/src/ignore/git.rs +++ b/src/ignore/git.rs @@ -8,6 +8,8 @@ use regex::Captures; use regex::Error; use regex::Regex; +use crate::util::error_exit; + #[derive(Clone, Debug)] pub struct GitignoreFilter { pub regex: Regex, @@ -210,7 +212,7 @@ fn convert_gitignore_glob(glob: &str, file_path: &Path) -> Result<Regex, Error> "." => "\\.", "*" => "[^/]*", "?" => "[^/]+", - _ => panic!("Error parsing pattern") + _ => error_exit(".gitignore", "Error parsing pattern") }.to_string() }).to_string(); diff --git a/src/ignore/hg.rs b/src/ignore/hg.rs index 72b2e52..8930820 100644 --- a/src/ignore/hg.rs +++ b/src/ignore/hg.rs @@ -8,6 +8,7 @@ use std::path::Path; use regex::Captures; use regex::Error; use regex::Regex; +use crate::util::error_exit; #[derive(Clone, Debug)] pub struct HgignoreFilter { @@ -182,7 +183,7 @@ fn convert_hgignore_glob(glob: &str, file_path: &Path) -> Result<Regex, Error> { ")" => "\\)", "^" => "\\^", "$" => "\\$", - _ => panic!("Error parsing pattern") + _ => error_exit(".hgignore", "Error parsing pattern") }.to_string() }).to_string(); @@ -207,7 +208,7 @@ fn convert_hgignore_glob(glob: &str, file_path: &Path) -> Result<Regex, Error> { ")" => "\\)", "^" => "\\^", "$" => "\\$", - _ => panic!("Error parsing pattern") + _ => error_exit(".hgignore", "Error parsing pattern") }.to_string() }).to_string(); diff --git a/src/searcher.rs b/src/searcher.rs index 5a96e41..767c973 100644 --- a/src/searcher.rs +++ b/src/searcher.rs @@ -1627,9 +1627,7 @@ impl <'a> Searcher<'a> { self.regex_cache.insert(val, regex.clone()); return regex.is_match(&field_value.to_string()); }, - _ => { - panic!("Incorrect regex expression") - } + _ => error_exit("Incorrect regex expression", val.as_str()) } } } @@ -1647,9 +1645,7 @@ impl <'a> Searcher<'a> { self.regex_cache.insert(val, regex.clone()); return !regex.is_match(&field_value.to_string()); }, - _ => { - panic!("Incorrect regex expression") - } + _ => error_exit("Incorrect regex expression", val.as_str()) } } } @@ -1668,9 +1664,7 @@ impl <'a> Searcher<'a> { self.regex_cache.insert(val, regex.clone()); return regex.is_match(&field_value.to_string()); }, - _ => { - panic!("Incorrect LIKE expression") - } + _ => error_exit("Incorrect LIKE expression", val.as_str()) } } } @@ -1689,9 +1683,7 @@ impl <'a> Searcher<'a> { self.regex_cache.insert(val, regex.clone()); return !regex.is_match(&field_value.to_string()); }, - _ => { - panic!("Incorrect LIKE expression") - } + _ => error_exit("Incorrect LIKE expression", val.as_str()) } } } diff --git a/src/util/glob.rs b/src/util/glob.rs index 747d3de..1005bae 100644 --- a/src/util/glob.rs +++ b/src/util/glob.rs @@ -3,6 +3,8 @@ use std::ops::Index; use regex::Captures; use regex::Regex; +use crate::util::error_exit; + pub fn is_glob(s: &str) -> bool { s.contains("*") || s.contains('?') } @@ -21,7 +23,7 @@ pub fn convert_glob_to_pattern(s: &str) -> String { ")" => "\\)", "^" => "\\^", "$" => "\\$", - _ => panic!("Error parsing glob") + _ => error_exit("Error parsing glob expression", s) }.to_string() }); @@ -44,7 +46,7 @@ pub fn convert_like_to_pattern(s: &str) -> String { ")" => "\\)", "^" => "\\^", "$" => "\\$", - _ => panic!("Error parsing like expression") + _ => error_exit("Error parsing LIKE expression", s) }.to_string() }); diff --git a/src/util/mod.rs b/src/util/mod.rs index 6be68ed..617e469 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -150,6 +150,11 @@ pub fn error_message(source: &str, description: &str) { eprint!("{}: {}", source, description); } +pub fn error_exit(source: &str, description: &str) -> ! { + error_message(source, description); + std::process::exit(2); +} + pub fn get_extension(s: &str) -> String { match Path::new(s).extension() { Some(ext) => ext.to_string_lossy().to_string(), @@ -352,9 +357,7 @@ pub fn format_filesize(size: u64, modifier: &str) -> String { fixed_at = None; format = humansize::BINARY; }, - _ => { - panic!("Unknown file size modifier"); - } + _ => error_exit("Unknown file size modifier", modifier.as_str()) }; if zeroes == -1 { |