add error handling

This commit is contained in:
steven-omaha
2023-02-24 16:02:45 +01:00
parent ef937ca146
commit f94f08a18a
2 changed files with 21 additions and 3 deletions
+9 -3
View File
@@ -207,7 +207,7 @@ impl Pacdef {
fn show_group_content(&self, groups: &ArgMatches) -> Result<()> {
let mut iter = groups
.get_many::<String>("group")
.get_many::<String>("groups")
.context("getting groups from args")?
.peekable();
@@ -218,7 +218,7 @@ impl Pacdef {
.groups
.iter()
.find(|g| g.name == *arg_group)
.ok_or_else(|| anyhow!("group {} not found", *arg_group))?;
.ok_or_else(|| anyhow!(crate::Error::GroupFileNotFound(g.name)))?;
if show_more_than_one_group {
let name = &group.name;
@@ -297,7 +297,13 @@ impl Pacdef {
.collect();
for file in &paths {
ensure!(!file.exists(), "group already exists under {file:?}");
file.file_name()
.ok_or_else(|| crate::Error::InvalidGroupName("..".to_string()))?;
ensure!(
!file.exists(),
crate::Error::GroupAlreadyExists(file.clone())
);
}
for file in &paths {
+12
View File
@@ -1,5 +1,6 @@
use std::error::Error as ErrorTrait;
use std::fmt::Display;
use std::path::PathBuf;
/// Error types for pacdef.
#[derive(Debug)]
@@ -11,6 +12,10 @@ pub enum Error {
ConfigFileNotFound,
/// Group file not found.
GroupFileNotFound(String),
/// Group already exists.
GroupAlreadyExists(PathBuf),
/// Invalid group name ('.' or '..')
InvalidGroupName(String),
}
impl Display for Error {
@@ -19,6 +24,13 @@ impl Display for Error {
Self::NoPackagesFound => f.write_str("no packages matching query"),
Self::ConfigFileNotFound => f.write_str("config file not found"),
Self::GroupFileNotFound(name) => f.write_str(&format!("group file '{name}' not found")),
Self::GroupAlreadyExists(path) => f.write_str(&format!(
"group file '{}' already exists",
path.to_string_lossy()
)),
Self::InvalidGroupName(name) => {
f.write_str(&format!("group name '{name}' is not valid"))
}
}
}
}