Avoid catch-all patterns and implicit error handling. Handle each error case explicitly, use the ? operator for error propagation instead of explicit returns, and never silently ignore errors.
Avoid catch-all patterns and implicit error handling. Handle each error case explicitly, use the ? operator for error propagation instead of explicit returns, and never silently ignore errors.
Key practices:
_
, unwrap_or_default()
) with explicit case handling?
operator instead of return Err(...)
for consistent error propagationattach_printable()
or change_context()
Example of problematic patterns:
// Avoid catch-all wildcards
match capture_method {
Some(CaptureMethod::Manual) => Status::Authorized,
_ => Status::Charged, // Too broad, handle each case explicitly
}
// Avoid explicit returns
if error.is_some() {
return Err(SomeError); // Use ? operator instead
}
// Avoid silent error handling
match result {
Ok(value) => value,
Err(_) => {} // Log the error at minimum
}
Better approach:
match capture_method {
Some(CaptureMethod::Manual) => Status::Authorized,
Some(CaptureMethod::Automatic) => Status::Charged,
Some(unsupported) => return Err(UnsupportedCaptureMethod(unsupported)),
None => return Err(MissingCaptureMethod),
}
// Use ? for error propagation
let result = operation().change_context(MyError::OperationFailed)?;
// Always handle errors meaningfully
match result {
Ok(value) => value,
Err(e) => {
logger::error!("Operation failed: {:?}", e);
return Err(e);
}
}
Enter the URL of a public GitHub repository