pub const FAILURE: i32 = 1;
pub const USAGE: i32 = 2;
pub const NOT_FOUND: i32 = 3;
pub const CONFIG: i32 = 4;
pub const AUTH: i32 = 5;
pub const INTERRUPTED: i32 = 130;
#[must_use]
pub fn exit_code_for(err: &(dyn std::error::Error + 'static)) -> i32 {
use vta_sdk::error::VtaError;
if err
.downcast_ref::<crate::config_file::ConfigFileError>()
.is_some()
{
return CONFIG;
}
if err.downcast_ref::<crate::confirm::NotConfirmed>().is_some() {
return USAGE;
}
match err.downcast_ref::<VtaError>() {
Some(VtaError::NotFound(_)) => NOT_FOUND,
Some(VtaError::Validation(_)) => CONFIG,
Some(VtaError::Auth(_)) => AUTH,
_ => FAILURE,
}
}
pub fn fail(err: &(dyn std::error::Error + 'static)) -> ! {
crate::render::print_cli_error(err);
std::process::exit(exit_code_for(err));
}
#[cfg(test)]
mod tests {
use super::*;
use vta_sdk::error::VtaError;
fn code(e: Box<dyn std::error::Error>) -> i32 {
exit_code_for(e.as_ref())
}
#[test]
fn typed_errors_pick_their_code() {
assert_eq!(code(Box::new(VtaError::NotFound("x".into()))), NOT_FOUND);
assert_eq!(code(Box::new(VtaError::Auth("x".into()))), AUTH);
assert_eq!(code(Box::new(VtaError::Validation("x".into()))), CONFIG);
assert_eq!(code(Box::new(VtaError::Forbidden("x".into()))), FAILURE);
assert_eq!(code("boom".into()), FAILURE);
assert_eq!(
code(Box::new(crate::config_file::ConfigFileError::new(
std::path::Path::new("/x/config.toml"),
"line 1".into(),
))),
CONFIG
);
assert_eq!(
code(Box::new(crate::confirm::NotConfirmed::new("x"))),
USAGE
);
}
}