feat: v2 rewrite — hexagonal arch, ActivityPub federation, NATS, deployment-ready (#1)
This commit was merged in pull request #1.
This commit is contained in:
46
crates/presentation/src/errors.rs
Normal file
46
crates/presentation/src/errors.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use api_types::responses::ErrorResponse;
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use domain::errors::DomainError;
|
||||
|
||||
pub enum ApiError {
|
||||
Domain(DomainError),
|
||||
Unauthorized,
|
||||
BadRequest(String),
|
||||
}
|
||||
|
||||
impl From<DomainError> for ApiError {
|
||||
fn from(e: DomainError) -> Self {
|
||||
Self::Domain(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, msg) = match self {
|
||||
Self::Domain(DomainError::NotFound) => (StatusCode::NOT_FOUND, "not found".into()),
|
||||
Self::Domain(DomainError::Unauthorized) => {
|
||||
(StatusCode::UNAUTHORIZED, "unauthorized".into())
|
||||
}
|
||||
Self::Domain(DomainError::Forbidden) => (StatusCode::FORBIDDEN, "forbidden".into()),
|
||||
Self::Domain(DomainError::Conflict(m)) => (StatusCode::CONFLICT, m),
|
||||
Self::Domain(DomainError::UniqueViolation { field }) => {
|
||||
(StatusCode::CONFLICT, format!("{field} already taken"))
|
||||
}
|
||||
Self::Domain(DomainError::InvalidInput(m)) => (StatusCode::UNPROCESSABLE_ENTITY, m),
|
||||
Self::Domain(DomainError::ExternalService(_)) => {
|
||||
(StatusCode::BAD_GATEWAY, "external service error".into())
|
||||
}
|
||||
Self::Domain(DomainError::Internal(_)) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"internal server error".into(),
|
||||
),
|
||||
Self::Unauthorized => (StatusCode::UNAUTHORIZED, "unauthorized".into()),
|
||||
Self::BadRequest(m) => (StatusCode::BAD_REQUEST, m),
|
||||
};
|
||||
(status, Json(ErrorResponse { error: msg })).into_response()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user