/
VVRONGG
/
backside
Обзор
Документация
Войти
/
VVRONGG
/
backside
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
src/error.rs
53 строки
2 KB
VVrongg
feat: delta-sync + routes API, hardened and tested
22 май 2026, 16:13
22 май 2026, 16:13
9d7239f
Код
Авторство
О чём код?
use axum::{ Json, http::StatusCode, response::{IntoResponse, Response}, }; use serde_json::json; /// Unified error type for handlers and services. /// /// Implements `IntoResponse`, so handlers can return `Result<T, AppError>` and /// use `?` instead of mapping every fallible call to a `StatusCode` by hand. #[derive(Debug)] pub enum AppError { /// 400 — malformed/invalid input from the client. BadRequest(String), /// 401 — missing or invalid credentials. Unauthorized, /// 404 — the requested resource does not exist. NotFound, /// 413 — the request payload is too large. PayloadTooLarge, /// 500 — unexpected internal failure (DB, etc.). Logged, not exposed. Internal(anyhow::Error), } impl IntoResponse for AppError { fn into_response(self) -> Response { let (status, message) = match self { AppError::BadRequest(m) => (StatusCode::BAD_REQUEST, m), AppError::Unauthorized => (StatusCode::UNAUTHORIZED, "unauthorized".to_owned()), AppError::NotFound => (StatusCode::NOT_FOUND, "not found".to_owned()), AppError::PayloadTooLarge => { (StatusCode::PAYLOAD_TOO_LARGE, "payload too large".to_owned()) } AppError::Internal(e) => { // log the real cause, return a generic message to the client tracing::error!("internal error: {e:?}"); ( StatusCode::INTERNAL_SERVER_ERROR, "internal server error".to_owned(), ) } }; (status, Json(json!({ "error": message }))).into_response() } } /// Lets `?` turn any sqlx error into a logged 500. impl From<sqlx::Error> for AppError { fn from(e: sqlx::Error) -> Self { AppError::Internal(e.into()) } }