/
alexefan136
/
flowstack
Обзор
Документация
Войти
/
alexefan136
/
flowstack
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
core/gateway/src/error.rs
78 строк
2 KB
Alexander Efanov
Обновление репозитория
15 июл 2026, 12:19
15 июл 2026, 12:19
76704c6
Код
Авторство
О чём код?
//! Унифицированные ошибки gateway use axum::{ http::StatusCode, response::{IntoResponse, Response}, Json, }; use serde_json::json; /// Главная ошибка приложения #[derive(Debug, thiserror::Error)] #[allow(dead_code)] pub enum AppError { #[error("Not found: {0}")] NotFound(String), #[error("Unauthorized: {0}")] Unauthorized(String), #[error("Forbidden: {0}")] Forbidden(String), #[error("Bad request: {0}")] BadRequest(String), #[error("Rate limit exceeded")] RateLimited, #[error("Internal error: {0}")] Internal(String), #[error(transparent)] Anyhow(#[from] anyhow::Error), } impl IntoResponse for AppError { fn into_response(self) -> Response { // ✅ Все три поля теперь имеют единообразные типы: // - StatusCode (копия, т.к. Copy) // - &'static str (строковый литерал) // - String (владеемая строка для message) let (status, error_type, message): (StatusCode, &'static str, String) = match &self { AppError::NotFound(msg) => (StatusCode::NOT_FOUND, "not_found", msg.clone()), AppError::Unauthorized(msg) => (StatusCode::UNAUTHORIZED, "unauthorized", msg.clone()), AppError::Forbidden(msg) => (StatusCode::FORBIDDEN, "forbidden", msg.clone()), AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, "bad_request", msg.clone()), AppError::RateLimited => ( StatusCode::TOO_MANY_REQUESTS, "rate_limited", // ✅ Было: "rate_limited".to_string() "Too many requests".to_string(), ), AppError::Internal(msg) => ( StatusCode::INTERNAL_SERVER_ERROR, "internal_error", msg.clone(), ), AppError::Anyhow(err) => ( StatusCode::INTERNAL_SERVER_ERROR, "internal_error", err.to_string(), ), }; let body = Json(json!({ "error": { "type": error_type, "message": message, "status": status.as_u16() } })); (status, body).into_response() } } /// Тип-алиас для Result с AppError #[allow(dead_code)] pub type AppResult<T> = Result<T, AppError>;