Skip to content

Rust Error Handling in Production — Observability, Logging, and Structured Errors

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Rust Error Handling in Production. We cover key concepts, practical examples, and best practices to help you master this topic.

Rust production error handling uses tracing for structured logging, sentry for error tracking, and structured error enums for API responses.

What You'll Learn

  • tracing for logging
  • Structured error enums
  • Error reporting
  • Log levels and spans

Why It Matters

Observability is critical in production. DodaZIP uses tracing for debug information.

Real-World Use

Production debugging, performance monitoring, audit logging, error tracking.

[dependencies]
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["json"] }
anyhow = "1.0"
thiserror = "1.0"
use tracing::{info, error, warn, debug, span, Level};
use tracing_subscriber::FmtSubscriber;

#[derive(Debug, thiserror::Error)]
pub enum ApiError {
    #[error("not found: {0}")]
    NotFound(String),

    #[error("validation error: {msg}")]
    Validation { msg: String },

    #[error("internal error")]
    Internal(#[from] anyhow::Error),
}

fn main() {
    // Initialize logger
    let subscriber = FmtSubscriber::builder()
        .with_max_level(Level::INFO)
        .json()
        .finish();
    tracing::subscriber::set_global_default(subscriber).unwrap();

    let span = span!(Level::INFO, "app_start", version = "1.0");
    let _guard = span.enter();

    info!("Application starting");

    match process_request("test") {
        Ok(result) => info!("Success: {}", result),
        Err(e) => error!("Error: {}", e),
    }

    warn!("This is a warning");
    debug!("Debug info (won't show at INFO level)");
}

fn process_request(input: &str) -> Result<String, ApiError> {
    if input.is_empty() {
        return Err(ApiError::Validation {
            msg: "input cannot be empty".into(),
        });
    }
    Ok(format!("Processed: {}", input))
}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro