use crate::{AppError, ValidationErrors};
use axum::http::StatusCode;
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum EventRejection {
#[error("{message}")]
Forbidden {
code: &'static str,
message: &'static str,
},
#[error("{message}")]
Conflict {
code: &'static str,
message: &'static str,
},
#[error("Validation failed")]
Validation {
code: &'static str,
errors: ValidationErrors,
},
#[error("{message}")]
Unavailable {
code: &'static str,
message: &'static str,
},
}
impl EventRejection {
pub fn code(&self) -> &'static str {
match self {
Self::Forbidden { code, .. }
| Self::Conflict { code, .. }
| Self::Validation { code, .. }
| Self::Unavailable { code, .. } => code,
}
}
pub fn status(&self) -> StatusCode {
match self {
Self::Forbidden { .. } => StatusCode::FORBIDDEN,
Self::Conflict { .. } => StatusCode::CONFLICT,
Self::Validation { .. } => StatusCode::UNPROCESSABLE_ENTITY,
Self::Unavailable { .. } => StatusCode::SERVICE_UNAVAILABLE,
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum EventError {
#[error("Application rejected lifecycle operation")]
Reject(#[from] EventRejection),
#[error("Lifecycle listener failed")]
Failure(#[source] AppError),
}
impl From<AppError> for EventError {
fn from(error: AppError) -> Self {
match error {
AppError::Rejected(problem) => Self::Reject(problem),
error => Self::Failure(error),
}
}
}
impl From<sea_orm::DbErr> for EventError {
fn from(error: sea_orm::DbErr) -> Self {
Self::Failure(error.into())
}
}
impl From<anyhow::Error> for EventError {
fn from(error: anyhow::Error) -> Self {
Self::Failure(error.into())
}
}
pub type EventResult = std::result::Result<(), EventError>;