From 93b599e59e41f1ccb24fcf482f29037f7ff29f65 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Mon, 19 May 2025 09:50:15 +0200 Subject: [PATCH] misc: devices: streamline #[source] and Error This streamlines the code base to follow best practices for error handling in Rust: Each error struct implements std::error::Error (most due via thiserror::Error derive macro) and sets its source accordingly. This allows future work that nicely prints the error chains, for example. So far, the convention is that each error prints its sub error as part of its Display::fmt() impl. Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- devices/src/interrupt_controller.rs | 12 ++++++------ devices/src/legacy/gpio_pl061.rs | 2 +- devices/src/legacy/rtc_pl031.rs | 2 +- devices/src/legacy/uart_pl011.rs | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/devices/src/interrupt_controller.rs b/devices/src/interrupt_controller.rs index 92fcf3ba0..957dcb876 100644 --- a/devices/src/interrupt_controller.rs +++ b/devices/src/interrupt_controller.rs @@ -18,22 +18,22 @@ pub enum Error { InvalidDeliveryMode, /// Failed creating the interrupt source group. #[error("Failed creating the interrupt source group: {0}")] - CreateInterruptSourceGroup(io::Error), + CreateInterruptSourceGroup(#[source] io::Error), /// Failed triggering the interrupt. #[error("Failed triggering the interrupt: {0}")] - TriggerInterrupt(io::Error), + TriggerInterrupt(#[source] io::Error), /// Failed masking the interrupt. #[error("Failed masking the interrupt: {0}")] - MaskInterrupt(io::Error), + MaskInterrupt(#[source] io::Error), /// Failed unmasking the interrupt. #[error("Failed unmasking the interrupt: {0}")] - UnmaskInterrupt(io::Error), + UnmaskInterrupt(#[source] io::Error), /// Failed updating the interrupt. #[error("Failed updating the interrupt: {0}")] - UpdateInterrupt(io::Error), + UpdateInterrupt(#[source] io::Error), /// Failed enabling the interrupt. #[error("Failed enabling the interrupt: {0}")] - EnableInterrupt(io::Error), + EnableInterrupt(#[source] io::Error), #[cfg(target_arch = "aarch64")] /// Failed creating GIC device. #[error("Failed creating GIC device: {0}")] diff --git a/devices/src/legacy/gpio_pl061.rs b/devices/src/legacy/gpio_pl061.rs index e69cb62a5..9b2a351b9 100644 --- a/devices/src/legacy/gpio_pl061.rs +++ b/devices/src/legacy/gpio_pl061.rs @@ -46,7 +46,7 @@ pub enum Error { #[error("GPIO interrupt disabled by guest driver.")] GpioInterruptDisabled, #[error("Could not trigger GPIO interrupt: {0}.")] - GpioInterruptFailure(io::Error), + GpioInterruptFailure(#[source] io::Error), #[error("Invalid GPIO Input key triggered: {0}.")] GpioTriggerKeyFailure(u32), } diff --git a/devices/src/legacy/rtc_pl031.rs b/devices/src/legacy/rtc_pl031.rs index 0551d7e19..23e8e7854 100644 --- a/devices/src/legacy/rtc_pl031.rs +++ b/devices/src/legacy/rtc_pl031.rs @@ -46,7 +46,7 @@ pub enum Error { #[error("Bad Write Offset: {0}")] BadWriteOffset(u64), #[error("Failed to trigger interrupt: {0}")] - InterruptFailure(io::Error), + InterruptFailure(#[source] io::Error), } type Result = result::Result; diff --git a/devices/src/legacy/uart_pl011.rs b/devices/src/legacy/uart_pl011.rs index 84975d6aa..549d6b6a6 100644 --- a/devices/src/legacy/uart_pl011.rs +++ b/devices/src/legacy/uart_pl011.rs @@ -54,11 +54,11 @@ pub enum Error { #[error("pl011: DMA not implemented.")] DmaNotImplemented, #[error("Failed to trigger interrupt: {0}")] - InterruptFailure(io::Error), + InterruptFailure(#[source] io::Error), #[error("Failed to write: {0}")] - WriteAllFailure(io::Error), + WriteAllFailure(#[source] io::Error), #[error("Failed to flush: {0}")] - FlushFailure(io::Error), + FlushFailure(#[source] io::Error), } type Result = result::Result;