use axum::{ http::StatusCode, response::{IntoResponse, Response}, }; use serde::Serialize; use serde_json::Value; use crate::{api_response::json_error_body, request_context::RequestContext}; #[derive(Debug)] pub struct AppError { status_code: StatusCode, code: &'static str, message: &'static str, details: Option, } #[derive(Clone, Debug, Serialize)] pub struct ApiErrorPayload { pub code: &'static str, pub message: &'static str, #[serde(skip_serializing_if = "Option::is_none")] pub details: Option, } impl AppError { pub fn from_status(status_code: StatusCode) -> Self { let (code, message) = resolve_http_error(status_code); Self { status_code, code, message, details: None, } } pub fn code(&self) -> &'static str { self.code } pub fn into_response_with_context(self, request_context: Option<&RequestContext>) -> Response { let status_code = self.status_code; let payload = self.to_payload(); (status_code, json_error_body(request_context, &payload)).into_response() } fn to_payload(&self) -> ApiErrorPayload { ApiErrorPayload { code: self.code, message: self.message, details: self.details.clone(), } } } impl IntoResponse for AppError { fn into_response(self) -> Response { self.into_response_with_context(None) } } fn resolve_http_error(status_code: StatusCode) -> (&'static str, &'static str) { match status_code { StatusCode::BAD_REQUEST => ("BAD_REQUEST", "请求参数不合法"), StatusCode::UNAUTHORIZED => ("UNAUTHORIZED", "未授权访问"), StatusCode::FORBIDDEN => ("FORBIDDEN", "禁止访问"), StatusCode::NOT_FOUND => ("NOT_FOUND", "资源不存在"), StatusCode::CONFLICT => ("CONFLICT", "请求冲突"), StatusCode::TOO_MANY_REQUESTS => ("TOO_MANY_REQUESTS", "请求过于频繁"), StatusCode::BAD_GATEWAY => ("UPSTREAM_ERROR", "上游服务请求失败"), _ if status_code.is_client_error() => ("BAD_REQUEST", "请求参数不合法"), _ => ("INTERNAL_SERVER_ERROR", "服务器内部错误"), } }