expression
Axum: redact internal failures
Internal failures need a public message that does not reveal sensitive implementation details. Keep diagnostics in an appropriately protected logging system with redaction. Here an artificial storage failure maps to a generic public error; no storage or logging is performed. In-memory standalone Cargo example. Expected stdout (not executed): 500 internal_error Common mistake: Sending detailed internal diagnostics to every API consumer.
Formal Syntax & Structure
rust syntaxrust
public response = stable status + codeWorking Example
Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use axum::{Json, routing::get, response::{IntoResponse, Response}}; struct StorageFailure; impl IntoResponse for StorageFailure { fn into_response(self) -> Response { (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"code":"internal_error"}))).into_response() } } async fn read() -> Result<String, StorageFailure> { Err(StorageFailure) } let r = Router::new().route("/", get(read)).oneshot(Request::new(Body::empty())).await.unwrap(); println!("{}", r.status().as_u16()); let v: serde_json::Value = serde_json::from_slice(&to_bytes(r.into_body(), 128).await.unwrap()).unwrap(); println!("{}", v["code"].as_str().unwrap());}