error-concept

Safe API error response mapping

At a hypothetical service boundary, map expected failures to stable public codes and suitable statuses. This is pure Rust mapping, not a running HTTP server. Internal diagnostics belong in access-controlled logs without secrets. Expected stdout (not executed): 400 invalid_input 404 not_found 500 internal_error Expected stderr: (empty) Expected exit: success Common mistake: Returning debug dumps, database paths or credentials to clients.

Formal Syntax & Structure

rust syntaxrust
match domain_error { ... } -> (status, public_code)

Working Example

Example Snippetrust
enum Failure { Invalid, Missing, Internal }fn public(e: Failure) -> (u16, &'static str) {    match e {        Failure::Invalid => (400, "invalid_input"),        Failure::Missing => (404, "not_found"),        Failure::Internal => (500, "internal_error"),    }}fn main() {    for e in [Failure::Invalid, Failure::Missing, Failure::Internal] {        let (status, code) = public(e);        println!("{status} {code}");    }}
Rust Reference