use axum::Json; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use synapse_sync::protocol::ErrorBody; #[derive(Debug)] pub struct ApiError { pub status: StatusCode, pub message: String, } impl ApiError { pub fn new(status: StatusCode, message: impl Into) -> Self { Self { status, message: message.into(), } } pub fn bad_request(m: impl Into) -> Self { Self::new(StatusCode::BAD_REQUEST, m) } pub fn not_found(m: impl Into) -> Self { Self::new(StatusCode::NOT_FOUND, m) } pub fn internal(e: impl std::fmt::Display) -> Self { tracing::error!(error = %e, "internal error"); Self::new(StatusCode::INTERNAL_SERVER_ERROR, "internal error") } } impl From for ApiError { fn from(e: synapse_core::Error) -> Self { use synapse_core::Error as E; match e { E::NotFound(m) => Self::not_found(m), E::Invalid(m) => Self::bad_request(m), E::Conflict(m) => Self::new(StatusCode::CONFLICT, m), other => Self::internal(other), } } } impl From for ApiError { fn from(e: std::io::Error) -> Self { Self::internal(e) } } impl IntoResponse for ApiError { fn into_response(self) -> Response { ( self.status, Json(ErrorBody { error: self.message, }), ) .into_response() } } pub type ApiResult = Result;