use autumn_web::AutumnError;
use thiserror::Error;
use crate::money::MoneyError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BillingError {
#[error("billing config: {0}")]
Config(String),
#[error("billing provider {provider}: {message}")]
Provider {
provider: &'static str,
message: String,
},
#[error("malformed billing event: {0}")]
Malformed(String),
#[error("unsupported billing operation: {0}")]
Unsupported(&'static str),
#[error("billing store: {0}")]
Store(String),
#[error("billing {0} not found")]
NotFound(&'static str),
#[error("billing conflict: {0}")]
Conflict(String),
#[error(transparent)]
Money(#[from] MoneyError),
#[error("billing requires an authenticated user")]
Unauthenticated,
#[error("billing forbidden: {0}")]
Forbidden(String),
}
impl BillingError {
#[must_use]
pub fn provider(provider: &'static str, message: impl Into<String>) -> Self {
Self::Provider {
provider,
message: message.into(),
}
}
#[must_use]
pub fn store(message: impl Into<String>) -> Self {
Self::Store(message.into())
}
}
impl BillingError {
#[must_use]
pub fn into_autumn(self) -> AutumnError {
let message = self.to_string();
match self {
Self::Unauthenticated => AutumnError::unauthorized_msg(message),
Self::Forbidden(_) => AutumnError::forbidden_msg(message),
Self::NotFound(_) => AutumnError::not_found_msg(message),
Self::Conflict(_) => AutumnError::conflict_msg(message),
Self::Money(_) | Self::Unsupported(_) => AutumnError::bad_request_msg(message),
Self::Provider { .. } => AutumnError::service_unavailable_msg(message),
Self::Config(_) | Self::Malformed(_) | Self::Store(_) => {
AutumnError::internal_server_error_msg(message)
}
}
}
}