From 8696bc6604b4c8068cb909f39bfa683a53ba0853 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Mon, 19 May 2025 09:50:25 +0200 Subject: [PATCH] misc: hypervisor: 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 --- hypervisor/src/arch/aarch64/gic.rs | 6 +++--- hypervisor/src/arch/emulator/mod.rs | 4 ++-- hypervisor/src/arch/riscv64/aia.rs | 6 +++--- hypervisor/src/hypervisor.rs | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/hypervisor/src/arch/aarch64/gic.rs b/hypervisor/src/arch/aarch64/gic.rs index 9e2b39bb1..33c10db49 100644 --- a/hypervisor/src/arch/aarch64/gic.rs +++ b/hypervisor/src/arch/aarch64/gic.rs @@ -17,13 +17,13 @@ use crate::{CpuState, HypervisorDeviceError, HypervisorVmError}; pub enum Error { /// Error while calling KVM ioctl for setting up the global interrupt controller. #[error("Failed creating GIC device: {0}")] - CreateGic(HypervisorVmError), + CreateGic(#[source] HypervisorVmError), /// Error while setting device attributes for the GIC. #[error("Failed setting device attributes for the GIC: {0}")] - SetDeviceAttribute(HypervisorDeviceError), + SetDeviceAttribute(#[source] HypervisorDeviceError), /// Error while getting device attributes for the GIC. #[error("Failed getting device attributes for the GIC: {0}")] - GetDeviceAttribute(HypervisorDeviceError), + GetDeviceAttribute(#[source] HypervisorDeviceError), } pub type Result = result::Result; diff --git a/hypervisor/src/arch/emulator/mod.rs b/hypervisor/src/arch/emulator/mod.rs index e62d5f636..a858346de 100644 --- a/hypervisor/src/arch/emulator/mod.rs +++ b/hypervisor/src/arch/emulator/mod.rs @@ -82,13 +82,13 @@ pub enum EmulationError { WrongNumberOperands(#[source] anyhow::Error), #[error("Instruction Exception: {0}")] - InstructionException(Exception), + InstructionException(#[source] Exception), #[error("Instruction fetching error: {0}")] InstructionFetchingError(#[source] anyhow::Error), #[error("Platform emulation error: {0}")] - PlatformEmulationError(PlatformError), + PlatformEmulationError(#[source] PlatformError), #[error(transparent)] EmulationError(#[from] anyhow::Error), diff --git a/hypervisor/src/arch/riscv64/aia.rs b/hypervisor/src/arch/riscv64/aia.rs index bf8181553..cf0a68f6e 100644 --- a/hypervisor/src/arch/riscv64/aia.rs +++ b/hypervisor/src/arch/riscv64/aia.rs @@ -14,13 +14,13 @@ use crate::{AiaState, HypervisorDeviceError, HypervisorVmError}; pub enum Error { /// Error while calling KVM ioctl for setting up the global interrupt controller. #[error("Failed creating AIA device: {0}")] - CreateAia(HypervisorVmError), + CreateAia(#[source] HypervisorVmError), /// Error while setting device attributes for the AIA. #[error("Failed setting device attributes for the AIA: {0}")] - SetDeviceAttribute(HypervisorDeviceError), + SetDeviceAttribute(#[source] HypervisorDeviceError), /// Error while getting device attributes for the AIA. #[error("Failed getting device attributes for the AIA: {0}")] - GetDeviceAttribute(HypervisorDeviceError), + GetDeviceAttribute(#[source] HypervisorDeviceError), } pub type Result = result::Result; diff --git a/hypervisor/src/hypervisor.rs b/hypervisor/src/hypervisor.rs index 001140a3b..6797ace99 100644 --- a/hypervisor/src/hypervisor.rs +++ b/hypervisor/src/hypervisor.rs @@ -67,22 +67,22 @@ pub enum HypervisorError { /// /// Checking extensions failed /// - #[error("Checking extensions:{0}")] + #[error("Checking extensions: {0}")] CheckExtensions(#[source] anyhow::Error), /// /// Failed to retrieve TDX capabilities /// - #[error("Failed to retrieve TDX capabilities:{0}")] + #[error("Failed to retrieve TDX capabilities: {0}")] TdxCapabilities(#[source] anyhow::Error), /// /// Failed to set partition property /// - #[error("Failed to set partition property:{0}")] + #[error("Failed to set partition property: {0}")] SetPartitionProperty(#[source] anyhow::Error), /// /// Running on an unsupported CPU /// - #[error("Unsupported CPU:{0}")] + #[error("Unsupported CPU: {0}")] UnsupportedCpu(#[source] anyhow::Error), /// /// Launching a VM with unsupported VM Type