use serde::Serialize; use thiserror::Error; #[derive(Error, Debug)] pub enum AppError { #[error("Network error: {0}")] Network(#[from] reqwest::Error), #[error("IO error: {0}")] Io(#[from] std::io::Error), #[error("JSON error: {0}")] Json(#[from] serde_json::Error), #[error("OAuth error: {0}")] OAuth(String), #[error("Configuration error: {0}")] Config(String), #[error("Account error: {0}")] Account(String), #[error("Proxy error: {0}")] Proxy(String), #[error("Unknown error: {0}")] Unknown(String), } // Implement Serialize for API responses impl Serialize for AppError { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_str(self.to_string().as_str()) } } // Result type alias pub type AppResult = Result; // Implement IntoResponse for Axum impl axum::response::IntoResponse for AppError { fn into_response(self) -> axum::response::Response { let status = match &self { AppError::Network(_) => axum::http::StatusCode::BAD_GATEWAY, AppError::Io(_) => axum::http::StatusCode::INTERNAL_SERVER_ERROR, AppError::Json(_) => axum::http::StatusCode::BAD_REQUEST, AppError::OAuth(_) => axum::http::StatusCode::UNAUTHORIZED, AppError::Config(_) => axum::http::StatusCode::BAD_REQUEST, AppError::Account(_) => axum::http::StatusCode::NOT_FOUND, AppError::Proxy(_) => axum::http::StatusCode::SERVICE_UNAVAILABLE, AppError::Unknown(_) => axum::http::StatusCode::INTERNAL_SERVER_ERROR, }; let body = serde_json::json!({ "success": false, "error": self.to_string() }); (status, axum::Json(body)).into_response() } }