#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ExitCode {
Ok = 0,
Error = 1,
Cancel = 2,
Auth = 4,
}
impl ExitCode {
#[must_use]
pub fn code(self) -> i32 {
match self {
ExitCode::Ok => 0,
ExitCode::Error => 1,
ExitCode::Cancel => 2,
ExitCode::Auth => 4,
}
}
#[must_use]
pub fn as_u8(self) -> u8 {
match self {
ExitCode::Ok => 0,
ExitCode::Error => 1,
ExitCode::Cancel => 2,
ExitCode::Auth => 4,
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct FlagError(pub String);
impl FlagError {
pub fn new(msg: impl Into<String>) -> Self {
Self(msg.into())
}
}
#[derive(Debug, thiserror::Error)]
#[error("")]
pub struct SilentError;
#[derive(Debug, thiserror::Error)]
#[error("authentication required for {hostname}")]
pub struct AuthError {
pub hostname: String,
}
impl AuthError {
pub fn new(hostname: impl Into<String>) -> Self {
Self {
hostname: hostname.into(),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("cancelled")]
pub struct CancelError;
#[derive(Debug, Clone)]
pub struct ApiErrorItem {
pub field: Option<String>,
pub message: String,
}
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
#[error("HTTP {status} on {url}: {message}")]
Http {
status: u16,
url: String,
message: String,
errors: Vec<ApiErrorItem>,
},
#[error("network error: {0}")]
Network(String),
#[error("failed to decode response: {0}")]
Decode(String),
}
impl ApiError {
#[must_use]
pub fn status(&self) -> Option<u16> {
match self {
ApiError::Http { status, .. } => Some(*status),
_ => None,
}
}
#[must_use]
pub fn is_unauthorized(&self) -> bool {
self.status() == Some(401)
}
#[must_use]
pub fn is_not_found(&self) -> bool {
self.status() == Some(404)
}
#[must_use]
pub fn is_gone(&self) -> bool {
self.status() == Some(410)
}
#[must_use]
pub fn http_message(&self) -> Option<&str> {
match self {
ApiError::Http { message, .. } => Some(message),
_ => None,
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum GitError {
#[error("git command failed (exit {code}): {stderr}")]
Command { code: i32, stderr: String },
#[error("git executable not found: {0}")]
NotFound(String),
#[error("could not parse remote URL: {0}")]
RemoteParse(String),
#[error("{0}")]
Other(String),
}
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("config io error: {0}")]
Io(String),
#[error("config parse error: {0}")]
Parse(String),
}
#[derive(Debug, thiserror::Error)]
pub enum PromptError {
#[error("cancelled")]
Cancelled,
#[error("{0}")]
Other(String),
}