From 616a3b1c70d05970b84c852a65cea41f299595f0 Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Sat, 21 Feb 2026 23:21:32 +0700 Subject: [PATCH] fix(netlink): Improves error handling Refactors error handling for interface and queue discipline operations to provide more informative error messages. This aids in debugging and user experience. --- src/netlink.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/netlink.rs b/src/netlink.rs index 7e24f08..0ee82ba 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -28,8 +28,6 @@ pub enum NetlinkError { #[error("Error happened while parsing UTF-8 string")] Utf8Error(#[from] Utf8Error), - #[error("Invalid reply type")] - InvalidReply, } #[derive(Clone, Copy, Debug)] @@ -49,7 +47,8 @@ impl Netlink { request.encode().push_ifname_bytes(ifname.as_bytes()); let mut iter = socket.request(&request)?; - let (header, _) = iter.recv_one()?; + let (header, _) = iter.recv_one() + .map_err(|_| NetlinkError::InterfaceNotFound(ifname.to_string()))?; Ok(header.ifi_index()) } @@ -75,7 +74,7 @@ impl Netlink { Err(NetlinkError::NoInterfaceStatsFound(ifname.to_string())) } - pub fn qdisc_from_ifindex(ifindex: i32) -> Result { + pub fn qdisc_from_ifindex(ifindex: i32, ifname: &str) -> Result { let mut socket = NetlinkSocket::new(); let header = tc::PushTcmsg::new(); let request = tc::Request::new().op_getqdisc_dump_request(&header); @@ -104,12 +103,12 @@ impl Netlink { } } - Err(NetlinkError::NoQdiscFound(ifindex.to_string())) + Err(NetlinkError::NoQdiscFound(ifname.to_string())) } pub fn qdisc_from_ifname(ifname: &str) -> Result { let ifindex = Netlink::find_interface(ifname)?; - Netlink::qdisc_from_ifindex(ifindex) + Netlink::qdisc_from_ifindex(ifindex, ifname) } pub fn set_qdisc_rate(qdisc: Qdisc, bandwidth_kbit: u64) -> Result<(), NetlinkError> {