errors fix

This commit is contained in:
2026-04-24 17:01:57 +07:00
parent 0dc6562dd2
commit 17a4980329
6 changed files with 203 additions and 82 deletions
+33 -6
View File
@@ -4,12 +4,18 @@ use axum::{
Json,
};
use serde_json::json;
use tracing::error;
use tracing::{error, warn};
#[derive(thiserror::Error, Debug)]
pub enum AppError {
#[error("User not found")]
UserNotFound,
#[error("Trial already used")]
TrialAlreadyUsed,
#[error("Database error: {0}")]
Database(#[from] sqlx::Error),
#[error("Transaction failed: {0}")]
Transaction(String),
#[error("Redis error: {0}")]
Redis(#[from] redis::RedisError),
#[error("Unauthorized: {0}")]
@@ -24,9 +30,11 @@ pub enum AppError {
impl IntoResponse for AppError {
fn into_response(self) -> Response {
// Определяем тип ошибки для метрики
let error_type = match &self {
AppError::Database(_) => "database",
AppError::Transaction(_) => "transaction",
AppError::UserNotFound => "user_not_found",
AppError::TrialAlreadyUsed => "trial_used",
AppError::Redis(_) => "redis",
AppError::Unauthorized(_) => "unauthorized",
AppError::NotFound(_) => "not_found",
@@ -34,15 +42,29 @@ impl IntoResponse for AppError {
AppError::Internal => "internal",
};
// Инкрементируем метрику ошибок
metrics::counter!("app_errors_total", "type" => error_type).increment(1);
let (status, error_message) = match &self {
AppError::UserNotFound => {
warn!("Attempted action for non-existent user");
(StatusCode::NOT_FOUND, "User profile not found".to_string())
}
AppError::TrialAlreadyUsed => {
warn!("Attempted to activate exhausted trial");
(StatusCode::CONFLICT, "Trial already used".to_string())
}
AppError::Database(e) => {
error!("DB Error: {:?}", e);
error!("DB Execution Error: {:?}", e);
(
StatusCode::INTERNAL_SERVER_ERROR,
"Internal server error".to_string(),
"Internal server database error".to_string(),
)
}
AppError::Transaction(msg) => {
error!("Transaction commit failed: {}", msg);
(
StatusCode::INTERNAL_SERVER_ERROR,
"Transaction processing failed".to_string(),
)
}
AppError::Redis(e) => {
@@ -61,7 +83,12 @@ impl IntoResponse for AppError {
),
};
let body = Json(json!({ "error": error_message }));
let body = Json(json!({
"success": false,
"error": error_message,
"code": status.as_u16()
}));
(status, body).into_response()
}
}