Spaces:
Sleeping
Sleeping
| use serde::Serialize; | |
| use thiserror::Error; | |
| pub enum AppError { | |
| Network( reqwest::Error), | |
| Io( std::io::Error), | |
| Json( serde_json::Error), | |
| OAuth(String), | |
| Config(String), | |
| Account(String), | |
| Proxy(String), | |
| Unknown(String), | |
| } | |
| // Implement Serialize for API responses | |
| impl Serialize for AppError { | |
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
| where | |
| S: serde::Serializer, | |
| { | |
| serializer.serialize_str(self.to_string().as_str()) | |
| } | |
| } | |
| // Result type alias | |
| pub type AppResult<T> = Result<T, AppError>; | |
| // 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() | |
| } | |
| } | |