1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

cli: replace ControlError with AppError for error handling

This commit is contained in:
NotAShelf 2025-05-17 07:42:38 +03:00 committed by raf
parent ef096705fc
commit b170fa0dd0
2 changed files with 12 additions and 9 deletions

View file

@ -219,13 +219,15 @@ fn get_kernel_info() -> Result<String, AppError> {
/// Get system uptime
fn get_system_uptime() -> Result<Duration, AppError> {
let uptime_str = fs::read_to_string("/proc/uptime").map_err(AppError::Io)?;
let uptime_str = fs::read_to_string("/proc/uptime").map_err(|e| AppError::Io(e))?;
let uptime_secs = uptime_str
.split_whitespace()
.next()
.ok_or_else(|| AppError::Generic("Invalid uptime format".to_string()))?
.ok_or_else(|| AppError::Generic("Invalid format in /proc/uptime file".to_string()))?
.parse::<f64>()
.map_err(|e| AppError::Generic(format!("Failed to parse uptime: {e}")))?;
.map_err(|e| {
AppError::Generic(format!("Failed to parse uptime from /proc/uptime: {}", e))
})?;
Ok(Duration::from_secs_f64(uptime_secs))
}

View file

@ -402,9 +402,9 @@ fn main() -> Result<(), AppError> {
error!(
"Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})"
);
Err(ControlError::InvalidValueError(format!(
Err(AppError::Generic(format!(
"Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})"
)).into())
)))
} else {
info!(
"Setting battery thresholds: start at {start_threshold}%, stop at {stop_threshold}%"
@ -464,14 +464,15 @@ fn init_logger() {
fn validate_freq(freq_mhz: u32, label: &str) -> Result<(), AppError> {
if freq_mhz == 0 {
error!("{label} frequency cannot be zero");
Err(ControlError::InvalidValueError(format!("{label} frequency cannot be zero")).into())
let err = ControlError::InvalidValueError(format!("{label} frequency cannot be zero"));
Err(err.into())
} else if freq_mhz > 10000 {
// Extremely high value unlikely to be valid
error!("{label} frequency ({freq_mhz} MHz) is unreasonably high");
Err(ControlError::InvalidValueError(format!(
let err = ControlError::InvalidValueError(format!(
"{label} frequency ({freq_mhz} MHz) is unreasonably high"
))
.into())
));
Err(err.into())
} else {
Ok(())
}