use std::process::ExitCode;
use std::sync::Arc;
use thiserror::Error;
pub const EXIT_USAGE: u8 = 64;
pub const EXIT_INVALID_INPUT: u8 = 65;
pub const EXIT_NOT_FOUND: u8 = 66;
pub const EXIT_UNAVAILABLE: u8 = 69;
pub const EXIT_INTERNAL: u8 = 70;
pub const EXIT_NETWORK: u8 = 74;
pub const EXIT_IO: u8 = EXIT_NETWORK;
pub const EXIT_CONFIG: u8 = 78;
pub const EXIT_TIMEOUT: u8 = 124;
pub const EXIT_INTERRUPTED: u8 = 130;
pub const EXIT_BROKEN_PIPE: u8 = 141;
pub const EXIT_TERMINATED: u8 = 143;
pub fn io_at(op: IoOp, path: &std::path::Path) -> impl FnOnce(std::io::Error) -> AppError {
let path = path.display().to_string();
move |e| {
let detail = ErrorDetail::io(op, Some(path), &e);
AppError::of_with_source(detail, e)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ErrorKind {
Usage,
InvalidInput,
NotFound,
RateLimited,
Unavailable,
Timeout,
Network,
Budget,
Parse,
Config,
Io,
Internal,
Interrupted,
Terminated,
BrokenPipe,
}
impl ErrorKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Usage => "usage",
Self::InvalidInput => "invalid_input",
Self::NotFound => "not_found",
Self::RateLimited => "rate_limited",
Self::Unavailable => "unavailable",
Self::Timeout => "timeout",
Self::Network => "network",
Self::Budget => "budget",
Self::Parse => "parse",
Self::Config => "config",
Self::Io => "io",
Self::Internal => "internal",
Self::Interrupted => "interrupted",
Self::Terminated => "terminated",
Self::BrokenPipe => "broken_pipe",
}
}
pub fn exit_code(self) -> u8 {
match self {
Self::Usage => EXIT_USAGE,
Self::InvalidInput => EXIT_INVALID_INPUT,
Self::NotFound => EXIT_NOT_FOUND,
Self::RateLimited | Self::Unavailable => EXIT_UNAVAILABLE,
Self::Network | Self::Budget => EXIT_NETWORK,
Self::Timeout => EXIT_TIMEOUT,
Self::Parse => EXIT_INVALID_INPUT,
Self::Config => EXIT_CONFIG,
Self::Io => EXIT_IO,
Self::Internal => EXIT_INTERNAL,
Self::Interrupted => EXIT_INTERRUPTED,
Self::Terminated => EXIT_TERMINATED,
Self::BrokenPipe => EXIT_BROKEN_PIPE,
}
}
pub fn retryable(self) -> bool {
matches!(
self,
Self::RateLimited | Self::Unavailable | Self::Timeout | Self::Network
)
}
pub fn is_retryable(self) -> bool {
self.retryable()
}
pub fn is_permanent(self) -> bool {
!self.retryable()
}
pub fn retry_kind(self) -> crate::retry::RetryKind {
use crate::retry::RetryKind;
match self {
Self::RateLimited => RetryKind::RateLimited,
Self::Unavailable => RetryKind::TransientServer,
Self::Timeout => RetryKind::Timeout,
Self::Network => RetryKind::TransientNetwork,
_ => RetryKind::Permanent,
}
}
}
#[derive(Debug, Error, Clone)]
#[non_exhaustive]
pub enum AppError {
#[error("{message}")]
Structured {
kind: ErrorKind,
message: String,
detail: Box<ErrorDetail>,
retry_after_secs: Option<u64>,
#[source]
source: Option<Arc<dyn std::error::Error + Send + Sync>>,
},
}
impl AppError {
pub fn of(detail: ErrorDetail) -> Self {
Self::Structured {
kind: detail.kind(),
message: render::render_en(&detail),
detail: Box::new(detail),
retry_after_secs: None,
source: None,
}
}
pub fn of_with_source(
detail: ErrorDetail,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::Structured {
kind: detail.kind(),
message: render::render_en(&detail),
detail: Box::new(detail),
retry_after_secs: None,
source: Some(Arc::new(source)),
}
}
pub fn localized(&self, locale: crate::i18n::Locale) -> String {
match self {
Self::Structured {
detail, message, ..
} => match locale {
crate::i18n::Locale::PtBr => render::render_pt_br(detail),
crate::i18n::Locale::En => message.clone(),
},
}
}
pub fn with_retry_after(mut self, secs: u64) -> Self {
let Self::Structured {
retry_after_secs, ..
} = &mut self;
*retry_after_secs = Some(secs);
self
}
pub fn interrupted() -> Self {
Self::of(ErrorDetail::Interrupted)
}
pub fn terminated() -> Self {
Self::of(ErrorDetail::Terminated)
}
pub fn broken_pipe() -> Self {
Self::of(ErrorDetail::BrokenPipe)
}
pub fn kind(&self) -> ErrorKind {
match self {
Self::Structured { kind, .. } => *kind,
}
}
pub fn detail(&self) -> &ErrorDetail {
match self {
Self::Structured { detail, .. } => detail,
}
}
pub fn message(&self) -> &str {
match self {
Self::Structured { message, .. } => message,
}
}
pub fn retry_after_secs(&self) -> Option<u64> {
match self {
Self::Structured {
retry_after_secs, ..
} => *retry_after_secs,
}
}
pub fn retryable(&self) -> bool {
self.detail().retryable()
}
pub fn suggestions(&self) -> Option<&[Suggestion]> {
match self.detail() {
ErrorDetail::WithSuggestions { suggestions, .. } => Some(suggestions),
_ => None,
}
}
pub fn is_retryable(&self) -> bool {
self.retryable()
}
pub fn is_permanent(&self) -> bool {
!self.retryable()
}
pub fn retry_kind(&self) -> crate::retry::RetryKind {
self.kind().retry_kind()
}
pub fn exit_code(&self) -> ExitCode {
ExitCode::from(self.kind().exit_code())
}
pub fn from_http_status(status: u16, body_hint: &str) -> Self {
Self::of(ErrorDetail::HttpStatus {
status,
hint: body_hint.to_string(),
})
}
}
pub type AppResult<T> = Result<T, AppError>;
mod classify;
mod detail;
mod render;
mod suggestion;
mod vocab;
#[cfg(test)]
mod tests;
pub use detail::ErrorDetail;
pub use suggestion::Suggestion;
pub use vocab::{
AllowlistStage, ContentKind, DestructiveEffect, InternalOp, IoCause, IoOp, Subject, ValueSource,
};