Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Created November 21, 2025 13:56
Show Gist options
  • Select an option

  • Save ebuildy/191120f9bd27455118e6c28bc0f90fb2 to your computer and use it in GitHub Desktop.

Select an option

Save ebuildy/191120f9bd27455118e6c28bc0f90fb2 to your computer and use it in GitHub Desktop.
Rust zbus error to string
use zbus::Error as ZbusError;
/// Convert a zbus::Error to a human-readable string
///
/// This function provides detailed error messages for different types of zbus errors,
/// making it easier to understand what went wrong when working with D-Bus operations.
///
/// # Arguments
///
/// * `error` - The zbus::Error to convert
///
/// # Returns
///
/// A String containing a human-readable error message
///
/// # Examples
///
/// ```no_run
/// use zbus::Error as ZbusError;
/// use rusty::utils::error_conversion::zbus_error_to_string;
///
/// let error = ZbusError::Unsupported;
/// let error_msg = zbus_error_to_string(&error);
/// println!("Error: {}", error_msg);
/// ```
pub fn zbus_error_to_string(error: &ZbusError) -> String {
match error {
ZbusError::InvalidReply => {
"Invalid reply received from D-Bus".to_string()
}
ZbusError::Unsupported => {
"Unsupported operation or feature".to_string()
}
ZbusError::FDO(fdo_err) => {
format!("D-Bus freedesktop error: {}", fdo_err)
}
ZbusError::NameTaken => {
"D-Bus name is already taken".to_string()
}
ZbusError::Address(msg) => {
format!("D-Bus address error: {}", msg)
}
ZbusError::InterfaceNotFound => {
"D-Bus interface not found".to_string()
}
ZbusError::InvalidGUID => {
"Invalid D-Bus GUID".to_string()
}
ZbusError::Handshake(msg) => {
format!("D-Bus handshake failed: {}", msg)
}
ZbusError::InvalidField => {
"Invalid field in D-Bus message".to_string()
}
ZbusError::Variant(err) => {
format!("D-Bus variant error: {}", err)
}
ZbusError::ExcessData => {
"Excess data in D-Bus message".to_string()
}
ZbusError::IncorrectEndian => {
"Incorrect endianness in D-Bus message".to_string()
}
ZbusError::InputOutput(io_err) => {
format!("D-Bus I/O error: {}", io_err)
}
_ => {
// Fallback for any other error variants
format!("D-Bus error: {}", error)
}
}
}
/// Extension trait to add convenient string conversion to zbus::Error
pub trait ZbusErrorExt {
/// Convert the error to a string
fn to_error_string(&self) -> String;
}
impl ZbusErrorExt for ZbusError {
fn to_error_string(&self) -> String {
zbus_error_to_string(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unsupported_error() {
let error = ZbusError::Unsupported;
let result = zbus_error_to_string(&error);
assert!(result.contains("Unsupported"));
}
#[test]
fn test_invalid_reply_error() {
let error = ZbusError::InvalidReply;
let result = zbus_error_to_string(&error);
assert!(result.contains("Invalid reply"));
}
#[test]
fn test_extension_trait() {
let error = ZbusError::NameTaken;
let result = error.to_error_string();
assert!(result.contains("already taken"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment