Replace process::exit() with Result propagation#500
Conversation
Return Result from CliInput::parse() and Defaults::retrieve() instead of calling process::exit() directly. Errors are now handled in main().
📝 WalkthroughWalkthroughThe changes refactor error handling from panic/exit patterns to explicit Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 3❌ Failed checks (3 warnings)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/cli_input.rs (1)
90-100:⚠️ Potential issue | 🟠 MajorPotential panic on empty string input.
If the user passes an empty string as the amount argument,
amount.chars().last()returnsNone, and the.unwrap()on line 93 will panic. This contradicts the PR's goal of replacing panics/exits with clean error handling.Suggested fix
Some(mut amount) => { + if amount.is_empty() { + return Err(InputError::new("Amount cannot be empty!")); + } + // check whether last character is an SI unit let mut multiplier = 1.0; let last_char = amount.chars().last().unwrap();Alternatively, handle the
Nonecase directly:// check whether last character is an SI unit let mut multiplier = 1.0; - let last_char = amount.chars().last().unwrap(); + let Some(last_char) = amount.chars().last() else { + return Err(InputError::new("Amount cannot be empty!")); + };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/cli_input.rs` around lines 90 - 100, The code currently unwraps amount.chars().last() which will panic if amount is empty; update the branch handling Some(mut amount) in the parsing logic to first check for an empty string (e.g., amount.is_empty()) or handle the Option from amount.chars().last() instead of calling unwrap(); if empty, return or propagate a clean error (or skip SI-prefix handling) rather than panicking, and then continue using SiUnitPrefix::parse_from_str(&last_char.to_string()) and multiplier as before once a valid last_char is available.
🧹 Nitpick comments (2)
src/cli_input.rs (1)
57-61: Remove deprecatedError::description()implementation.The
description()method has been deprecated since Rust 1.42.0. TheDisplayimplementation (lines 51-55) is sufficient for theErrortrait.Suggested change
-impl Error for InputError { - fn description(&self) -> &str { - &self.details - } -} +impl Error for InputError {}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/cli_input.rs` around lines 57 - 61, Remove the deprecated Error::description implementation from the impl Error for InputError block: delete the fn description(&self) -> &str { &self.details } method and leave either an empty impl Error for InputError {} or remove the impl block entirely if there are no other Error methods; keep the existing Display implementation for InputError as the canonical error message provider.src/defaults.rs (1)
69-74: Consider using Display formatting instead of Debug for the error.Line 72 uses
{e:?}(Debug format) which may produce verbose output with internal details. Using{e}(Display format) typically gives cleaner user-facing messages.Suggested change
fn setup(config: &HomeConfig) -> Result<(), Box<dyn Error>> { config .save_yaml(Self::load_defaults_template()) - .map_err(|e| format!("Failed to save default config: {e:?}"))?; + .map_err(|e| format!("Failed to save default config: {e}"))?; Ok(()) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/defaults.rs` around lines 69 - 74, In setup (fn setup) the map_err uses Debug formatting "{e:?}" for the save_yaml error; change the map_err closure to use Display formatting "{e}" so the error message is cleaner for users (i.e., replace format!("Failed to save default config: {e:?}") with format!("Failed to save default config: {e}") in the map_err on config.save_yaml).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/cli_input.rs`:
- Around line 90-100: The code currently unwraps amount.chars().last() which
will panic if amount is empty; update the branch handling Some(mut amount) in
the parsing logic to first check for an empty string (e.g., amount.is_empty())
or handle the Option from amount.chars().last() instead of calling unwrap(); if
empty, return or propagate a clean error (or skip SI-prefix handling) rather
than panicking, and then continue using
SiUnitPrefix::parse_from_str(&last_char.to_string()) and multiplier as before
once a valid last_char is available.
---
Nitpick comments:
In `@src/cli_input.rs`:
- Around line 57-61: Remove the deprecated Error::description implementation
from the impl Error for InputError block: delete the fn description(&self) ->
&str { &self.details } method and leave either an empty impl Error for
InputError {} or remove the impl block entirely if there are no other Error
methods; keep the existing Display implementation for InputError as the
canonical error message provider.
In `@src/defaults.rs`:
- Around line 69-74: In setup (fn setup) the map_err uses Debug formatting
"{e:?}" for the save_yaml error; change the map_err closure to use Display
formatting "{e}" so the error message is cleaner for users (i.e., replace
format!("Failed to save default config: {e:?}") with format!("Failed to save
default config: {e}") in the map_err on config.save_yaml).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3fe1ec34-2f1f-458e-a8d2-e14200ad90d9
📒 Files selected for processing (3)
src/cli_input.rssrc/defaults.rssrc/main.rs
Summary
CliInput::parse()now returnsResult<Self, InputError>instead of callingprocess::exit()on invalid inputDefaults::retrieve()and related methods now returnResultinstead of callingprocess::exit()on config errorsDefaults::setup()returnsResultinstead ofunwrap()-ing on config save (fixes Bump reqwest from 0.11.10 to 0.11.11 #6 too)main()Test plan
cargo testpassesSummary by CodeRabbit
Bug Fixes
Refactor