use std::fmt;
pub mod exit_codes {
pub const SUCCESS: i32 = 0;
pub const GENERAL: i32 = 1;
pub const CONFLICT: i32 = 2;
pub const API_ERROR: i32 = 3;
pub const VALIDATION: i32 = 4;
pub const AUTH: i32 = 5;
}
#[derive(Debug)]
pub enum StaxError {
General(anyhow::Error),
Conflict(String),
Api(anyhow::Error),
Validation(String),
Auth(String),
}
impl StaxError {
pub fn exit_code(&self) -> i32 {
match self {
StaxError::General(_) => exit_codes::GENERAL,
StaxError::Conflict(_) => exit_codes::CONFLICT,
StaxError::Api(_) => exit_codes::API_ERROR,
StaxError::Validation(_) => exit_codes::VALIDATION,
StaxError::Auth(_) => exit_codes::AUTH,
}
}
pub fn conflict(msg: impl Into<String>) -> Self {
StaxError::Conflict(msg.into())
}
pub fn api(err: anyhow::Error) -> Self {
StaxError::Api(err)
}
pub fn validation(msg: impl Into<String>) -> Self {
StaxError::Validation(msg.into())
}
pub fn auth(msg: impl Into<String>) -> Self {
StaxError::Auth(msg.into())
}
}
impl fmt::Display for StaxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StaxError::General(err) => write!(f, "{}", err),
StaxError::Conflict(msg) => write!(f, "Conflict: {}", msg),
StaxError::Api(err) => write!(f, "API error: {}", err),
StaxError::Validation(msg) => write!(f, "Validation error: {}", msg),
StaxError::Auth(msg) => write!(f, "Authentication error: {}", msg),
}
}
}
impl std::error::Error for StaxError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
StaxError::General(err) | StaxError::Api(err) => err.source(),
_ => None,
}
}
}
impl From<anyhow::Error> for StaxError {
fn from(err: anyhow::Error) -> Self {
StaxError::General(err)
}
}
pub type StaxResult<T> = Result<T, StaxError>;
#[derive(Debug)]
pub struct ConflictStopped;
impl std::fmt::Display for ConflictStopped {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Stopped on rebase conflict")
}
}
impl std::error::Error for ConflictStopped {}