Better error display

This commit is contained in:
Wildan M 2026-04-16 04:04:53 +07:00
parent 8efa32c53a
commit 61a66d3755
No known key found for this signature in database
GPG Key ID: 01AC53185C679C79
2 changed files with 29 additions and 34 deletions

View File

@ -134,10 +134,7 @@ impl FromStr for CliCommand {
"push-tree" => Ok(CliCommand::PushTree),
"cook-tree" => Ok(CliCommand::CookTree),
"find" => Ok(CliCommand::Find),
_ => Err(Error::Other(format!(
"Unknown command '{}'\n{}\n",
s, REPO_HELP_STR
))),
_ => bail_options_err!("Unknown command {:?}", s),
}
}
}
@ -188,7 +185,10 @@ impl CliConfig {
fn main() {
init_config();
if let Err(e) = main_inner() {
eprintln!("{:?}", e);
match e {
Error::Options(e) => eprintln!("{}\n{}", e, REPO_HELP_STR),
e => eprintln!("{}", e),
}
process::exit(1);
};
}
@ -197,8 +197,7 @@ fn main_inner() -> Result<()> {
let args: Vec<String> = env::args().skip(1).collect();
if args.is_empty() || args.contains(&"--help".to_string()) || args.contains(&"-h".to_string()) {
println!("{}", REPO_HELP_STR);
process::exit(1);
bail_options_err!("");
}
let (config, command, recipes) = parse_args(args)?;
@ -259,10 +258,10 @@ fn main_inner() -> Result<()> {
Err(e) => {
if config.cook.nonstop {
if verbose {
eprintln!("{:?}", e);
eprintln!("{}", e);
}
if let Err(e) = handle_nonstop_fail(recipe) {
eprintln!("{:?}", e)
eprintln!("{}", e)
};
}
print_failed(&command, &recipe.name);
@ -417,6 +416,12 @@ fn publish_packages(recipe_names: &Vec<CookRecipe>, repo_path: &PathBuf) -> Resu
run_command(command, &None)
}
macro_rules! bail_options_err {
($($arg:tt)*) => {
return Err(cookbook::Error::Options(format!($($arg)*)))
};
}
fn parse_args(args: Vec<String>) -> Result<(CliConfig, CliCommand, Vec<CookRecipe>)> {
let mut config = CliConfig::new()?;
let mut command: Option<String> = None;
@ -433,13 +438,10 @@ fn parse_args(args: Vec<String>) -> Result<(CliConfig, CliCommand, Vec<CookRecip
"--filesystem" => {
config.filesystem = Some({
let r = redox_installer::Config::from_file(&PathBuf::from(value));
r.map_err(|e| Error::Other(e.to_string()))?
r.map_err(|e| Error::Other(format!("{:?}", e)))?
})
}
_ => {
eprintln!("Error: Unknown flag with value: {}", arg);
process::exit(1);
}
_ => bail_options_err!("Error: Unknown flag with value: {}", arg),
}
} else if arg.starts_with("--category-") {
// to workaround make command limit we provide this option
@ -449,18 +451,12 @@ fn parse_args(args: Vec<String>) -> Result<(CliConfig, CliCommand, Vec<CookRecip
"--repo-binary" => override_filesystem_repo_binary = true,
"--with-package-deps" => config.with_package_deps = true,
"--all" => config.all = true,
_ => {
eprintln!("Error: Unknown flag: {}", arg);
process::exit(1);
}
_ => bail_options_err!("Error: Unknown flag: {}", arg),
}
}
} else if arg.starts_with('-') {
match arg.as_str() {
_ => {
eprintln!("Error: Unknown flag: {}", arg);
process::exit(1);
}
_ => bail_options_err!("Error: Unknown flag: {}", arg),
}
} else if command.is_none() {
// The first non-flag argument is the command
@ -481,7 +477,7 @@ fn parse_args(args: Vec<String>) -> Result<(CliConfig, CliCommand, Vec<CookRecip
}
let Some(command) = command else {
return Err(Error::Other(format!("Error: No command specified.")));
bail_options_err!("Error: No command specified");
};
let command: CliCommand = str::parse(&command)?;
if command.is_informational() {
@ -494,22 +490,20 @@ fn parse_args(args: Vec<String>) -> Result<(CliConfig, CliCommand, Vec<CookRecip
if recipe_names.is_empty() {
if config.all || config.category.is_some() {
if !recipe_names.is_empty() {
return Err(Error::Other(format!(
"Do not specify recipe names when using the --all or --category flag."
)));
bail_options_err!(
"Do not specify recipe names when using the --all or --category flag"
);
}
if config.all && config.category.is_some() {
return Err(Error::Other(format!(
"Do not specify both --all and --category flag."
)));
bail_options_err!("Do not specify both --all and --category flag.");
}
if config.all && !command.is_cleaning() {
// because read_recipe is false by logic below
// some recipes on wip folders are invalid anyway
return Err(Error::Other(format!(
bail_options_err!(
"Refusing to run an unrealistic command to {} all recipes",
command.to_string()
)));
);
}
let all_recipes_path = match &config.category {
None => staged_pkg::list(""),
@ -534,9 +528,9 @@ fn parse_args(args: Vec<String>) -> Result<(CliConfig, CliCommand, Vec<CookRecip
.filter_map(|k| PackageName::new(k.to_string()).ok())
.collect();
} else {
return Err(Error::Other(format!(
bail_options_err!(
"Error: No recipe names or filesystem config provided and --all flag was not used."
)));
);
}
}
}

View File

@ -46,6 +46,7 @@ pub enum Error {
Command(Command, ExitStatus),
Package(pkg::PackageError),
Pkgar(pkgar::Error),
Options(String),
Other(String),
}
@ -95,7 +96,7 @@ impl Display for Error {
}
Error::Package(package_error) => write!(f, "{}", package_error),
Error::Pkgar(error) => write!(f, "{}", error),
Error::Other(context) => {
Error::Other(context) | Error::Options(context) => {
write!(f, "{context}")
}
}