Rust gives you memory safety, but calling .unwrap() inside an Axum web handler will still panic and kill the handler task on unexpected input.
Clean Error Handling With IntoResponse
Define a domain error enum using thiserror and implement IntoResponse once so handlers can cleanly use the ? operator:
use axum::{http::StatusCode, response::{IntoResponse, Response}, Json}; use serde_json::json; enum ApiError { NotFound, Internal, } impl IntoResponse for ApiError { fn into_response(self) -> Response { let (status, msg) = match self { ApiError::NotFound => (StatusCode::NOT_FOUND, "Resource not found"), ApiError::Internal => (StatusCode::INTERNAL_SERVER_ERROR, "Internal error"), }; (status, Json(json!({ "error": msg }))).into_response() } } Want Production-Ready .cursorrules & Boilerplates?
I'm a student developer in Japan funding my upcoming new life next spring by packaging strict, battle-tested AI coding setups:
- Free Starter
.cursorrules& Reviewer Prompts ($0+): Download Free Starter Pack (or GitHub Repo) - Rust High-Performance Web Services Toolkit ($19): Get Stack Kit
- All 10 Production Stacks Bundled ($99): 10-in-1 Ultimate Developer Suite