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:
parent
ef096705fc
commit
b170fa0dd0
2 changed files with 12 additions and 9 deletions
|
@ -219,13 +219,15 @@ fn get_kernel_info() -> Result<String, AppError> {
|
||||||
|
|
||||||
/// Get system uptime
|
/// Get system uptime
|
||||||
fn get_system_uptime() -> Result<Duration, AppError> {
|
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
|
let uptime_secs = uptime_str
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.next()
|
.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>()
|
.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))
|
Ok(Duration::from_secs_f64(uptime_secs))
|
||||||
}
|
}
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -402,9 +402,9 @@ fn main() -> Result<(), AppError> {
|
||||||
error!(
|
error!(
|
||||||
"Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})"
|
"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})"
|
"Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})"
|
||||||
)).into())
|
)))
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"Setting battery thresholds: start at {start_threshold}%, stop at {stop_threshold}%"
|
"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> {
|
fn validate_freq(freq_mhz: u32, label: &str) -> Result<(), AppError> {
|
||||||
if freq_mhz == 0 {
|
if freq_mhz == 0 {
|
||||||
error!("{label} frequency cannot be zero");
|
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 {
|
} else if freq_mhz > 10000 {
|
||||||
// Extremely high value unlikely to be valid
|
// Extremely high value unlikely to be valid
|
||||||
error!("{label} frequency ({freq_mhz} MHz) is unreasonably high");
|
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"
|
"{label} frequency ({freq_mhz} MHz) is unreasonably high"
|
||||||
))
|
));
|
||||||
.into())
|
Err(err.into())
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue