Skip to content

Commit 8bd9a1f

Browse files
committed
vmstat&slabtop: fix error description
1 parent 1005960 commit 8bd9a1f

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/uu/slabtop/src/parse.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use std::{
7-
cmp::Ordering,
8-
fs,
9-
io::{Error, ErrorKind},
10-
};
6+
use std::io::Error;
7+
use std::{cmp::Ordering, fs, io::ErrorKind};
8+
use uucore::error::{UError, UResult, USimpleError};
119

1210
#[derive(Debug, Default)]
1311
pub struct SlabInfo {
@@ -18,10 +16,22 @@ pub struct SlabInfo {
1816
impl SlabInfo {
1917
// parse slabinfo from /proc/slabinfo
2018
// need root permission
21-
pub fn new() -> Result<SlabInfo, Error> {
22-
let content = fs::read_to_string("/proc/slabinfo")?;
19+
pub fn new() -> UResult<SlabInfo> {
20+
let error_wrapper = |e: Error| {
21+
USimpleError::new(
22+
1,
23+
format!(
24+
"Unable to create slabinfo structure: {}",
25+
Box::<dyn UError>::from(e) // We need Display impl of UError
26+
),
27+
)
28+
};
29+
30+
let content = fs::read_to_string("/proc/slabinfo").map_err(error_wrapper)?;
2331

24-
Self::parse(&content).ok_or(ErrorKind::Unsupported.into())
32+
Self::parse(&content)
33+
.ok_or(ErrorKind::Unsupported.into())
34+
.map_err(error_wrapper)
2535
}
2636

2737
pub fn parse(content: &str) -> Option<SlabInfo> {

0 commit comments

Comments
 (0)