Refactor for better Error handling in mod proc

This commit is contained in:
Stephen Seo 2022-07-09 22:31:01 +09:00
parent b502241d22
commit 750cc7065c
2 changed files with 74 additions and 20 deletions

View file

@ -48,7 +48,7 @@ fn main() {
// network traffic
if let Some(net) = &mut net_obj {
if let Err(e) = net.update() {
println!("ERROR: {:?}", e);
println!("{}", e);
net_obj = None;
} else {
let netinfo_string = net.get_netstring();
@ -76,14 +76,28 @@ fn main() {
// meminfo
{
let meminfo_string = proc::get_meminfo().unwrap_or("MEMINFO ERROR".into());
let meminfo_result = proc::get_meminfo();
let meminfo_string: String;
if let Err(e) = meminfo_result {
println!("{}", e);
meminfo_string = String::from("MEMINFO ERROR");
} else {
meminfo_string = meminfo_result.unwrap();
}
let meminfo_obj = SwaybarObject::from_string(meminfo_string);
array.push_object(meminfo_obj);
}
// loadavg
{
let loadavg_string = proc::get_loadavg().unwrap_or("LOADAVG ERROR".into());
let loadavg_result = proc::get_loadavg();
let loadavg_string: String;
if let Err(e) = loadavg_result {
println!("{}", e);
loadavg_string = String::from("LOADAVG ERROR");
} else {
loadavg_string = loadavg_result.unwrap();
}
let loadavg_obj = SwaybarObject::from_string(loadavg_string);
array.push_object(loadavg_obj);
}

View file

@ -2,6 +2,51 @@ use std::fs::File;
use std::io;
use std::io::prelude::*;
#[derive(Debug)]
pub enum Error {
IOError(std::io::Error),
ParseIntError(std::num::ParseIntError),
GenericError(String),
}
impl From<std::io::Error> for Error {
fn from(io_error: std::io::Error) -> Self {
Self::IOError(io_error)
}
}
impl From<std::num::ParseIntError> for Error {
fn from(parse_error: std::num::ParseIntError) -> Self {
Self::ParseIntError(parse_error)
}
}
impl From<String> for Error {
fn from(string: String) -> Self {
Self::GenericError(string)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IOError(e) => e.fmt(f),
Error::ParseIntError(e) => e.fmt(f),
Error::GenericError(s) => f.write_str(s),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::IOError(e) => e.source(),
Error::ParseIntError(e) => e.source(),
_ => None,
}
}
}
pub struct NetInfo {
dev_name: String,
down: u64,
@ -21,7 +66,7 @@ impl NetInfo {
}
}
pub fn update(&mut self) -> io::Result<()> {
pub fn update(&mut self) -> Result<(), Error> {
let mut netdev_string = String::new();
{
let mut netdev_file: File = File::open("/proc/net/dev")?;
@ -39,19 +84,17 @@ impl NetInfo {
if let Some(line) = dev_line {
let entries: Vec<&str> = line.split_whitespace().collect();
if entries.len() < 10 {
return Err(io::Error::new(io::ErrorKind::Other, format!("NetInfo::update: Failed to parse /proc/net/dev, \"{}\" device line is too short", self.dev_name)));
return Err(format!("NetInfo::update: Failed to parse /proc/net/dev, \"{}\" device line is too short", self.dev_name).into());
}
self.down = entries[1].parse().map_err(|_| io::Error::new(io::ErrorKind::Other, format!("NetInfo::update: Failed to parse recv bytes in /proc/net/dev for device \"{}\"", self.dev_name)))?;
self.up = entries[9].parse().map_err(|_| io::Error::new(io::ErrorKind::Other, format!("NetInfo::update: Failed to parse recv bytes in /proc/net/dev for device \"{}\"", self.dev_name)))?;
self.down = entries[1].parse()?;
self.up = entries[9].parse()?;
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"NetInfo::update: Failed to parse /proc/net/dev, can't find net device \"{}\"",
self.dev_name
),
));
return Err(format!(
"NetInfo::update: Failed to parse /proc/net/dev, can't find net device \"{}\"",
self.dev_name
)
.into());
}
Ok(())
@ -84,7 +127,7 @@ impl NetInfo {
}
}
pub fn get_meminfo() -> io::Result<String> {
pub fn get_meminfo() -> Result<String, Error> {
let mut meminfo_string = String::new();
{
let mut meminfo: File = File::open("/proc/meminfo")?;
@ -148,7 +191,7 @@ pub fn get_meminfo() -> io::Result<String> {
}
}
pub fn get_loadavg() -> io::Result<String> {
pub fn get_loadavg() -> Result<String, Error> {
let mut loadavg_string = String::new();
{
let mut loadavg_file: File = File::open("/proc/loadavg")?;
@ -157,10 +200,7 @@ pub fn get_loadavg() -> io::Result<String> {
let loadavg_parts: Vec<&str> = loadavg_string.split_whitespace().collect();
if loadavg_parts.len() < 3 {
return Err(io::Error::new(
io::ErrorKind::Other,
"loadavg: failed to parse",
));
return Err("loadavg: failed to parse".to_owned().into());
}
Ok(format!(