coder-rust-error-handling
<skill_overview> Design predictable and ergonomic error handling in Rust Designing error types Propagating errors with Result Mapping errors at API boundaries Choosing between panic and Result The Rust Book - Error Handling </skill_overview> <panic_vs_result> <use_panic_when> Unrecoverable, invariant-breaking failures Bug in the program (logic error) </use_panic_when> <use_result_when> Expected errors (not found, validation) I/O or external dependency failures </use_result_when> </panic_vs_result> <error_types> Define a dedicated error enum for a module or service Implement Display and Error for custom errors Use From to convert lower-level errors enum ServiceError { NotFound, InvalidInput, Io(std::io::Error) } </error_types> Use ? to propagate errors Add context with map_err or custom variants Preserve original error for debugging <boundary_mapping> Map internal errors to public error codes/messages Do not leak sensitive details to clients </boundary_mapping> <anti_patterns> Avoid unwrap/expect for normal flow Avoid plain String errors without structure Do not panic on validation failures </anti_patterns>