Error-Handling Skill
thiserror / anyhow design and refactors - an Agent Skill for Rust 1.97.0 services and libraries.
Search across all documentation pages
thiserror / anyhow design and refactors - an Agent Skill for Rust 1.97.0 services and libraries.
Refactors string errors and unwrap chains into thiserror enums in libraries, anyhow::Result in binaries, context via .context(), and Axum IntoResponse mappings. Adds From impls and #[non_exhaustive] where APIs evolve.
unwrap in production pathBox<dyn Error>failure or error-chain legacy code| Input | Why |
|---|---|
| Crate type | lib vs bin policy |
| Error consumers | match vs propagate |
| HTTP exposure | status code mapping table |
| Backtrace policy | RUST_BACKTRACE in prod |
error.rs module with typed enumsmain returning anyhow::ResultResult<T, AppError> with IntoResponseexpect in lib - return Result.rg 'unwrap\(|expect\(' src/ --type rust
cargo clippy -- -D clippy::unwrap_used
cargo test error_use axum::{http::StatusCode, response::IntoResponse};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AppError {
#[error("not found")]
NotFound,
#[error(transparent)]
Internal(#[from] anyhow::Error),
}
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
let status = match self {
AppError::NotFound => StatusCode::NOT_FOUND,
AppError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
(status, self.to_string()).into_response()
}
}Fine for integration tests; unit tests often assert specific error variants.
Incremental: new modules use thiserror; wrap legacy at boundaries.
Only true bugs after validation; user errors stay Result.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026