Classify Retryable Errors with an Enum
Model transport, timeout, and validation failures in one error enum, then decide retryability without matching on strings.
Code
use std::error::Error;
use std::fmt;
#[derive(Debug)]
enum AppError {
Transport { endpoint: String, message: String },
Timeout { after_ms: u64 },
Validation { field: &'static str, message: String },
}
impl fmt::Display for AppError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppError::Transport { endpoint, message } => {
write!(formatter, "transport error for {endpoint}: {message}")
}
AppError::Timeout { after_ms } => {
write!(formatter, "timed out after {after_ms} ms")
}
AppError::Validation { field, message } => {
write!(formatter, "invalid {field}: {message}")
}
}
}
}
impl Error for AppError {}
impl AppError {
fn is_retryable(&self) -> bool {
matches!(
self,
AppError::Transport { .. } | AppError::Timeout { .. }
)
}
}
fn classify(error: &AppError) -> &'static str {
if error.is_retryable() {
"retry with backoff"
} else {
"return to caller"
}
}
fn main() {
let errors = [
AppError::Transport {
endpoint: "/orders".to_string(),
message: "connection reset".to_string(),
},
AppError::Timeout { after_ms: 250 },
AppError::Validation {
field: "email",
message: "missing @".to_string(),
},
];
for error in &errors {
println!("{error} -> {}", classify(error));
if let Some(source) = error.source() {
println!(" source: {source}");
}
}
}
How it works
Classify Retryable Errors with an Enum keeps everything in the functions fmt, is_retryable, classify and main.
The one repetition in this snippet is the loop for error in &errors. The match covers the value’s shapes one arm at a time — nothing falls to a default by accident.
Putting it to use
The primary signature is fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result
Running the file prints labeled results such as source: — each operation’s effect is visible in the output.
Running the file executes main, which drives classify in order.
Core lines
fn classify(error: &AppError) -> &'static str {
if error.is_retryable() {
"retry with backoff"
} else {
"return to caller"
}
}
AppError::Transport { endpoint, message } => {
write!(formatter, "transport error for {endpoint}: {message}")
}
AppError::Validation { field, message } => {
write!(formatter, "invalid {field}: {message}")
}
Interactive Tools for This Topic
FAQ
- What does Classify Retryable Errors with an Enum print when run?
- Invoke the file as-is — the `source:` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.