use crate::service::serror::FeatureDisabledError;
#[cfg(feature = "network")]
use crate::service::HttpError;
use std::fmt::{Display, Formatter};
use std::sync::{MutexGuard, PoisonError};
#[derive(Debug)]
pub enum ServiceError {
Io(std::io::Error),
Fmt(std::fmt::Error),
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
Json(serde_json::Error),
#[cfg(feature = "network")]
#[cfg_attr(docsrs, doc(cfg(feature = "network")))]
Network(reqwest::Error),
#[cfg(feature = "network")]
#[cfg_attr(docsrs, doc(cfg(feature = "network")))]
Http(HttpError),
LockPoisoned,
ConfigDeserialization,
FeatureDisabled(FeatureDisabledError),
Unknown(Box<dyn std::error::Error + Send + Sync>),
}
impl Display for ServiceError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(err) => write!(f, "IO error: {}", err),
Self::Fmt(err) => write!(f, "Fmt error: {}", err),
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
Self::Json(err) => write!(f, "JSON error: {}", err),
#[cfg(feature = "network")]
#[cfg_attr(docsrs, doc(cfg(feature = "network")))]
Self::Network(err) => write!(f, "Network error: {}", err),
#[cfg(feature = "network")]
#[cfg_attr(docsrs, doc(cfg(feature = "network")))]
Self::Http(err) => write!(f, "Http error: {}", err.status_code()),
Self::LockPoisoned => write!(f, "Lock poisoned"),
Self::ConfigDeserialization => write!(f, "Config deserialization error"),
Self::FeatureDisabled(err) => write!(f, "{}", err),
Self::Unknown(err) => write!(f, "Unknown service error: {}", err),
}
}
}
impl std::error::Error for ServiceError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
Self::Fmt(err) => Some(err),
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
Self::Json(err) => Some(err),
#[cfg(feature = "network")]
#[cfg_attr(docsrs, doc(cfg(feature = "network")))]
Self::Network(err) => Some(err),
#[cfg(feature = "network")]
#[cfg_attr(docsrs, doc(cfg(feature = "network")))]
Self::Http(err) => Some(err),
Self::LockPoisoned => None,
Self::ConfigDeserialization => None,
Self::FeatureDisabled(err) => Some(err),
Self::Unknown(err) => Some(err.as_ref()),
}
}
}
impl From<std::fmt::Error> for ServiceError {
fn from(err: std::fmt::Error) -> Self {
ServiceError::Fmt(err)
}
}
impl From<std::io::Error> for ServiceError {
fn from(err: std::io::Error) -> Self {
ServiceError::Io(err)
}
}
impl<T> From<PoisonError<MutexGuard<'_, T>>> for ServiceError {
fn from(_err: PoisonError<MutexGuard<'_, T>>) -> Self {
ServiceError::LockPoisoned
}
}