Spaces:
Sleeping
Sleeping
File size: 1,866 Bytes
bbb1195 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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<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()
}
}
|