pipeline and codebase updates
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tracing::error;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum AppError {
|
||||
#[error("Database error: {0}")]
|
||||
Database(#[from] sqlx::Error),
|
||||
#[error("Redis error: {0}")]
|
||||
Redis(#[from] redis::RedisError),
|
||||
#[error("Unauthorized: {0}")]
|
||||
Unauthorized(String),
|
||||
#[error("Not found: {0}")]
|
||||
NotFound(String),
|
||||
#[error("Business Logic: {0}")]
|
||||
BusinessLogic(String),
|
||||
#[error("Internal Server Error")]
|
||||
Internal,
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
// Определяем тип ошибки для метрики
|
||||
let error_type = match &self {
|
||||
AppError::Database(_) => "database",
|
||||
AppError::Redis(_) => "redis",
|
||||
AppError::Unauthorized(_) => "unauthorized",
|
||||
AppError::NotFound(_) => "not_found",
|
||||
AppError::BusinessLogic(_) => "business_logic",
|
||||
AppError::Internal => "internal",
|
||||
};
|
||||
|
||||
// Инкрементируем метрику ошибок
|
||||
metrics::counter!("app_errors_total", "type" => error_type).increment(1);
|
||||
|
||||
let (status, error_message) = match &self {
|
||||
AppError::Database(e) => {
|
||||
error!("DB Error: {:?}", e);
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Internal server error".to_string(),
|
||||
)
|
||||
}
|
||||
AppError::Redis(e) => {
|
||||
error!("Redis Error: {:?}", e);
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Internal server error".to_string(),
|
||||
)
|
||||
}
|
||||
AppError::Unauthorized(msg) => (StatusCode::UNAUTHORIZED, msg.clone()),
|
||||
AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
|
||||
AppError::BusinessLogic(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
|
||||
AppError::Internal => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Critical failure".to_string(),
|
||||
),
|
||||
};
|
||||
|
||||
let body = Json(json!({ "error": error_message }));
|
||||
(status, body).into_response()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user