use std::fmt;
#[derive(Debug)]
pub enum EngineError {
BindListener {
address: String,
source: std::io::Error,
},
Serve {
source: std::io::Error,
},
InvalidPort(String),
Startup {
subsystem: &'static str,
stage: &'static str,
source: StartupError,
},
Shutdown {
subsystem: &'static str,
source: ShutdownError,
},
}
#[derive(Debug)]
pub enum StartupError {
#[cfg(feature = "db")]
Db(arcature_db::DbConnectError),
#[cfg(feature = "cache")]
Cache(arcature_cache::CacheConnectError),
#[cfg(feature = "storage")]
Storage(arcature_storage::StorageConnectError),
#[cfg(feature = "mail")]
Mail(arcature_mail::MailConfigError),
#[cfg(feature = "jobs")]
JobsMigrate(arcature_jobs::MigrateError),
}
#[derive(Debug)]
pub enum ShutdownError {
#[cfg(feature = "jobs")]
Worker(arcature_jobs::WorkerError),
#[cfg(feature = "mail")]
Mail(String),
}
impl EngineError {
#[must_use]
pub fn bind_address(&self) -> Option<&str> {
match self {
Self::BindListener { address, .. } => Some(address),
_ => None,
}
}
}
impl fmt::Display for EngineError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BindListener { address, source } => {
write!(f, "failed to bind HTTP listener on {address}: {source}")
}
Self::Serve { source } => write!(f, "HTTP server error: {source}"),
Self::InvalidPort(value) => write!(f, "invalid HTTP port: {value:?}"),
Self::Startup {
subsystem,
stage,
source,
} => {
write!(
f,
"subsystem {subsystem} failed to start ({stage}): {source}"
)
}
Self::Shutdown { subsystem, source } => {
write!(f, "subsystem {subsystem} failed during shutdown: {source}")
}
}
}
}
impl std::error::Error for EngineError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::BindListener { source, .. } | Self::Serve { source } => Some(source),
Self::Startup { source, .. } => Some(source),
Self::Shutdown { source, .. } => Some(source),
Self::InvalidPort(_) => None,
}
}
}
impl fmt::Display for StartupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "db")]
Self::Db(e) => write!(f, "database connect: {e}"),
#[cfg(feature = "cache")]
Self::Cache(e) => write!(f, "cache connect: {e}"),
#[cfg(feature = "storage")]
Self::Storage(e) => write!(f, "storage connect: {e}"),
#[cfg(feature = "mail")]
Self::Mail(e) => write!(f, "mailer config: {e}"),
#[cfg(feature = "jobs")]
Self::JobsMigrate(e) => write!(f, "jobs migrate: {e}"),
#[allow(unreachable_patterns)]
_ => write!(f, "unknown startup error"),
}
}
}
impl std::error::Error for StartupError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(feature = "db")]
Self::Db(e) => Some(e),
#[cfg(feature = "cache")]
Self::Cache(e) => Some(e),
#[cfg(feature = "storage")]
Self::Storage(e) => Some(e),
#[cfg(feature = "mail")]
Self::Mail(e) => Some(e),
#[cfg(feature = "jobs")]
Self::JobsMigrate(e) => Some(e),
#[allow(unreachable_patterns)]
_ => None,
}
}
}
impl fmt::Display for ShutdownError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "jobs")]
Self::Worker(e) => write!(f, "worker: {e}"),
#[cfg(feature = "mail")]
Self::Mail(msg) => write!(f, "mailer shutdown: {msg}"),
#[allow(unreachable_patterns)]
_ => write!(f, "unknown shutdown error"),
}
}
}
impl std::error::Error for ShutdownError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(feature = "jobs")]
Self::Worker(e) => Some(e),
#[cfg(feature = "mail")]
Self::Mail(_) => None,
#[allow(unreachable_patterns)]
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, EngineError>;