use axum::{ Json, http::StatusCode, response::{IntoResponse, Response}, }; use serde_json::json; /// Do not reflect parser, database, TLS or request contents to callers/logs. pub async fn sanitize(mut response: Response) -> Response { response.headers_mut().insert( "cache-control", axum::http::HeaderValue::from_static("no-store"), ); let status = response.status(); if response .headers() .get("content-type") .is_some_and(|h| h == "application/json") { return response; } if !status.is_client_error() && !status.is_server_error() { return response; } let (code, message) = match status { StatusCode::UNAUTHORIZED => ("UNAUTHENTICATED", "authentication required"), StatusCode::FORBIDDEN => ("FORBIDDEN", "access denied"), StatusCode::NOT_FOUND => ("NOT_FOUND", "resource not found"), StatusCode::SERVICE_UNAVAILABLE => ("DEPENDENCY_UNAVAILABLE", "dependency unavailable"), StatusCode::PAYLOAD_TOO_LARGE => ("QUOTA_EXCEEDED", "request size limit"), _ => ("VALIDATION_ERROR", "request could not be processed"), }; (status,Json(json!({"code":code,"message":message,"retryable":status.is_server_error(),"correlation_id":uuid::Uuid::new_v4(),"details":{}}))).into_response() }