Replace panic-prone operations like `unwrap()` and `expect()` with graceful error handling that provides meaningful feedback. When encountering unexpected conditions, prefer logging warnings and falling back to reasonable defaults rather than crashing the application.
Replace panic-prone operations like unwrap()
and expect()
with graceful error handling that provides meaningful feedback. When encountering unexpected conditions, prefer logging warnings and falling back to reasonable defaults rather than crashing the application.
Key practices:
unwrap()
with proper error handling and user-friendly messagesexpect()
only when the panic condition truly should never occurExample transformation:
// Avoid this - can panic on unexpected input
let path = output.canonicalize().unwrap().to_string_lossy().to_string();
// Prefer this - graceful error handling
let path = match output.canonicalize() {
Ok(path) => path.to_string_lossy().to_string(),
Err(err) => {
eprintln!("Failed to canonicalize path: {}", err);
return Err(err);
}
};
This approach prevents application crashes from unexpected conditions while still providing useful diagnostic information for debugging and user feedback.
Enter the URL of a public GitHub repository