惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

Lobsters

Data types à la carte (2008) A New Design for Pretty Printer Implementations in Rust The real origin of Lorem Ipsum Detect and profile fonts on any website, revealing their properties and metadata Navigating the MTE Landscape: iOS Memory Protection Deep Dive NixOS 26.05 released | Blog Voxel Space Nix on Sailfish X (Sailfish OS for Sony Xperia) Coalgebras and Automata The Critical State of Cyberspacs OpenRCT2 v0.5.1 “Swamp Castle" released! dax — cross-platform shell tools for Deno and Node.js Marknote 1.6.0 Parallel Reconstruction of Lawful TLS Wiretapping What Is a Dickover? On Rendering Diffs EV Stupidity Checklist "But it happened." - Casey Muratori's comment on Eric Schmidt's commencement speech The Go language server can do some impressive code navigation lpcvoid.com Rsync maintainer starts uses Claude, regressions mount NNN STACK — NixOS · Niri · Noctalia bijou64 Canonical takes over Flutter desktop maintenance Emacs bra size calculator I Am Retiring from Tech to Live Offline Racket v9.2 Flathub disallows LLM-based submissions Scriba: Structured logging in Lisp with multiple backends and auto-config (Scheme library) What are you doing this weekend? What are important data systems problems, ignored by research? Equivalence of Unicode strings is strange (2016) Leaving performance on the table You probably don't need Yocto, and that's fine Replacing Photoshop With NSString - The Guinea Pig in the Cocoa Mine Patching my guitar amp's firmware An AI audit of FreeBSD How do you version public web APIs? Lunacy | Red Vice tail CI logs over SSH Introducing Neptune: Direct3D virtualization for QEMU ACME CAA Extensions to Become Mandatory The Silent Critic One year of Roto, a compiled scripting language for Rust CIFSwitch: a non-universal Linux local root vulnerability Protestware for coding agents Deterministic Multithreaded Testing in Python with blanket Garnix is shutting down RIPE NCC session fixation: poaching logins with an Atlas probe GNOME 2.20 but its Web Components Agentic Search for Context Engineering – Leonie Monigatti akashina.tngl.sh/jjc Concerning Emacs (and Jazz) Content-addressed Rust builds (or, what kache actually caches) Package managers that package package managers Nitpicking the shell history scene in ‘Tron: Legacy’ What's cooking on SourceHut? Q2 2026 The tenth OpenPGP email summit Why Gentoo? Clojure on Fennel part three: parsing WordPress at 23 Finding Miscompiles for Fun, Not Profit GitHub - creusot-rs/creusot: Creusot helps you prove your Rust code is correct. Announcing Rust 1.96.0 | Rust Blog A Love Letter to Neovim sqlite AGENTS.md Am I a Bad Friend? Making wl_shm fast Rust (and Slint) on a jailbroken Kindle. CSS vs. JavaScript • Josh W. Comeau Erlang Ecosystem Foundation - Supporting the BEAM community CVE-2026-48710: A Maintainer's Perspective - Marcelo Trylesinski A brief note about slot access cost in Common Lisp Converting shallow Git bundles into normal repositories May I recommend thinking of Emacs as your Fortress of Solitude Keyboard latency probe Rethinking the GNOME clipboard issues What are some of your favourite developer tools? Devlog ⚡ Zig Programming Language Back to the Building Blocks’ Building Blocks Tech Notes: Theseus: translating win32 to wasm Fast is better than slow Canada’s Bill C-22 and the security cost of collecting more data Intent to Prototype: Embedding API 5 PostgreSQL locking behaviors that trip people up okmij.org Stop advertising in your commits! | AksDev GitHub - mplsllc/macsurf: A modern web browser for Classic Mac OS 9 PowerPC. Real CSS3, ES5 JavaScript, native HTTPS — built with CodeWarrior on the Carbon API. Introducing DoomBench - Can Your Data Stack Run DOOM? Software For My New Home Server Building a Scalable Ingestion Pipeline with Temporal (Part 1) Avoid using "<![CDATA[ ... ]]>" in RSS Are you a member of any professional associations? What is a harmonic? An interactive comic about additive synthesis How Virtual Tables Work in the Itanium C++ ABI Using SwiftUI to Build a Mac-assed App in 2026 A portentous reunion Accelerating copy_if using SIMD The pressure Just How Bad Was The Intel IAPX432?
Custom Errors Are Non-Negotiable in My Rust Applications | Triston Armstrong
Triston Armstrong · 2026-05-28 · via Lobsters

custom-rust-error

Custom Errors Are Non-Negotiable in My Rust Applications

Centralizing error management using a custom AppError enum, combined with map_err and From traits, solves the type chaos of Rust services, establishing a clean, single-source contract across the whole codebase, WITHOUT the need for janky 3rd party crates.

😤 From ? Nightmare to Cohesive Design

When you first start dipping toes into Rust - especially when a service interacts with diverse subsystems (database, external API, file system) - a common friction point appears.

Error handling in Rust is profoundly powerful, but coordinating heterogeneous error types generates immediate boilerplate pain.

Consider a function that must manage a data pipeline: connect to a DB, fetch external credentials, and then validate configuration. Each external dependency returns its own unique error type (sqlx::Error, reqwest::Error, config::ConfigError).

If you do not consolidate these types into a single, application-defined enum, your resulting function signature becomes a cascade of explicit error handling. The compiler forces you to manage this type disparity, leading to code that focuses more on error plumbing than business logic.

Here is the pain point:

// NOTE: This signature relies on error boxing, which loses type specificity.
async fn run_pipeline_ugly() -> Result<(), Box<dyn std::error::Error>> {
    // 1. DB Interaction (Needs '?' to convert to Box<dyn Error>)
    match db_call().await {
        Ok(_) => {},
        Err(e) => return Err(Box::new(e)), // Manual boxing and return
    }

    // 2. API Interaction (Repeat pattern)
    match api_call().await {
        Ok(_) => {},
        Err(e) => return Err(Box::new(e)), // Repetition, error type mismatch
    }
    
    Ok(())
}

To someone new to rust, handling Result errors is a HUGE painpoint, and you can see why. This is an overly simplistic view into that pattern, but you can see immediately how easily it can escalate to some nastiness.

🏗️ The Single Source of Truth: Establishing an AppError enum

In any medium-to-large applications, the single most critical step is defining system boundaries. When you write the core business logic, you must enforce one single, unified contract.

That contract, for the errors, is the AppError enum (or whatever you wanna call it).

Instead of letting failure types float around like a chaotic mess; std::io::Error, serde_json::Error, tokio::io::Error, etc. we map them all to one canonical type. This is sanity. Every consuming module only needs to worry about Result<T, AppError>. Period.

pub enum AppError {
    Io(std::io::Error),
    Serialization(serde_json::Error),
    Other(String),
}

🎣 Layer 1: Error Interception with map_err (The Interrogation Layer)

Before we even touch the From trait, we have to deal with the immediate pain of the foreign error. This is where Result::map_err steps in, and honestly, it’s magic.

If an external API call fails, and we just use the ? operator, that error propagates immediately. That doesn't work because we want to control the returned error, so, we must intercept it - which looks like this in its simplist form.

let result = SomeErrorResult().map_err(|e| AppError::Io(e))?;

But, maybe we need to log the exact stack trace, we need to check the error details, and we need to maybe wrap it in a higher-level, more business-centric error message; all before the error is allowed to flow into our system.

This is where map_err lives. It hands us a closure. This closure is the interception point.

// Assume we have this foreign error type
struct ExternalApiError {
    code: i32,
    message: String,
}

// Simulated API call
fn call_external_api() -> Result<u32, ExternalApiError> {
    Err(ExternalApiError { 
        code: 401, 
        message: "Auth token expired.".into()
    })
}

fn process_data() -> Result<u32, AppError> {
    let result = call_external_api();

    // 🛑 INTERCEPTION POINT: We use map_err to grab the error,
    // perform business logic (logging), and *manually* wrap it.
    let final_result = result.map_err(|e| {
        // 🧠 This closure is where our custom, critical logic runs.
        println!("[LOG]: Authentication failure detected. Time to warn the user.");
        
        // Return the canonical type, enforcing our custom message.
        AppError::Other(format!("Authentication failure: {}. Needs refresh.", e.message))
    })?; 
    
    Ok(final_result)
}

My take: This level of explicit control is Vastly superior to relying on a macro crate that just wraps everything without letting you inspect the underlying failure reason.

⚙️ Layer 2: Structural Propagation with impl From (The Glue)

The ultimate goal is to make the compiler handle the error promotion so we don't have to write map_err everywhere. That's where the impl From trait comes in.

If map_err is active manual intervention, impl From is passive, structural adherence. It is the declaration of trust: "If I see an io::Error, I guarantee I know how to convert it to AppError::Io."

This is how we make the ? operator magic.

// Assume AppError and AppError::Io(io::Error) are defined
impl From<io::Error> for AppError {
    fn from(err: io::Error) -> Self {
        // Direct mapping: External IO error becomes AppError::Io
        AppError::Io(err)
    }
}

// Function using the automatic conversion
fn read_data_file(path: &str) -> Result<(), AppError> {
    // The '?' sees io::Error, checks for 'From' trait, finds it, 
    // and executes the block above, cleaning up the error type instantly.
    std::fs::read_to_string(path)?; 
    Ok(())
}

The takeaway

We use impl From to let the compiler handle the boilerplate conversion; it’s powerful, clean, and keeps the function body almost entirely clean of error-checking logic.

Final Comments

This article is meant for newer people. This has been game changing for me in writing rust and dealing with the onslaught of error types and not having a clean way to handle them. Atleast, not known to me at the time.

I couldnt have made this if it werent for a peer of mine, Joban, showing me his implimentation. It was gamechanging to me with writing rust, and all of my credit goes out to him. Thank yuh buddy!