Skip to content

Commit 8fd3c79

Browse files
committed
refactor(vmm): use thiserror for vstate::vm::Error
Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 7ab4b62 commit 8fd3c79

File tree

1 file changed

+21
-46
lines changed

1 file changed

+21
-46
lines changed

src/vmm/src/vstate/vm.rs

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// Use of this source code is governed by a BSD-style license that can be
66
// found in the THIRD-PARTY file.
77

8-
use std::fmt::Formatter;
9-
use std::{fmt, result};
8+
use std::result;
109

1110
#[cfg(target_arch = "x86_64")]
1211
use kvm_bindings::{
@@ -26,43 +25,59 @@ use crate::arch::aarch64::gic::GICDevice;
2625
use crate::arch::aarch64::gic::GicState;
2726

2827
/// Errors associated with the wrappers over KVM ioctls.
29-
#[derive(Debug)]
28+
#[derive(Debug, thiserror::Error)]
3029
pub enum Error {
3130
#[cfg(target_arch = "x86_64")]
3231
/// Retrieving supported guest MSRs fails.
32+
#[error("Retrieving supported guest MSRs fails: {0:?}")]
3333
GuestMsrs(crate::arch::x86_64::msr::Error),
3434
/// The number of configured slots is bigger than the maximum reported by KVM.
35+
#[error("The number of configured slots is bigger than the maximum reported by KVM")]
3536
NotEnoughMemorySlots,
3637
/// Cannot set the memory regions.
38+
#[error("Cannot set the memory regions: {0}")]
3739
SetUserMemoryRegion(kvm_ioctls::Error),
3840
#[cfg(target_arch = "aarch64")]
39-
/// Cannot create the global interrupt controller..
41+
/// Cannot create the global interrupt controller.
42+
#[error("Error creating the global interrupt controller: {0:?}")]
4043
VmCreateGIC(crate::arch::aarch64::gic::Error),
4144
/// Cannot open the VM file descriptor.
45+
#[error("Cannot open the VM file descriptor: {0}")]
4246
VmFd(kvm_ioctls::Error),
4347
#[cfg(target_arch = "x86_64")]
4448
/// Failed to get KVM vm pit state.
49+
#[error("Failed to get KVM vm pit state: {0}")]
4550
VmGetPit2(kvm_ioctls::Error),
4651
#[cfg(target_arch = "x86_64")]
4752
/// Failed to get KVM vm clock.
53+
#[error("Failed to get KVM vm clock: {0}")]
4854
VmGetClock(kvm_ioctls::Error),
4955
#[cfg(target_arch = "x86_64")]
5056
/// Failed to get KVM vm irqchip.
57+
#[error("Failed to get KVM vm irqchip: {0}")]
5158
VmGetIrqChip(kvm_ioctls::Error),
5259
#[cfg(target_arch = "x86_64")]
5360
/// Failed to set KVM vm pit state.
61+
#[error("Failed to set KVM vm pit state: {0}")]
5462
VmSetPit2(kvm_ioctls::Error),
5563
#[cfg(target_arch = "x86_64")]
5664
/// Failed to set KVM vm clock.
65+
#[error("Failed to set KVM vm clock: {0}")]
5766
VmSetClock(kvm_ioctls::Error),
5867
#[cfg(target_arch = "x86_64")]
5968
/// Failed to set KVM vm irqchip.
69+
#[error("Failed to set KVM vm irqchip: {0}")]
6070
VmSetIrqChip(kvm_ioctls::Error),
6171
/// Cannot configure the microvm.
72+
#[error("Cannot configure the microvm: {0}")]
6273
VmSetup(kvm_ioctls::Error),
6374
#[cfg(target_arch = "aarch64")]
75+
/// Failed to save the VM's GIC state.
76+
#[error("Failed to save the VM's GIC state: {0:?}")]
6477
SaveGic(crate::arch::aarch64::gic::Error),
6578
#[cfg(target_arch = "aarch64")]
79+
/// Failed to restore the VM's GIC state.
80+
#[error("Failed to restore the VM's GIC state: {0:?}")]
6681
RestoreGic(crate::arch::aarch64::gic::Error),
6782
}
6883

@@ -87,54 +102,14 @@ pub enum RestoreStateError {
87102
#[derive(Debug, derive_more::From)]
88103
pub struct RestoreStateError(crate::arch::aarch64::gic::Error);
89104
#[cfg(target_arch = "aarch64")]
90-
impl fmt::Display for RestoreStateError {
91-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105+
impl std::fmt::Display for RestoreStateError {
106+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
92107
write!(f, "{}", self.0)
93108
}
94109
}
95110
#[cfg(target_arch = "aarch64")]
96111
impl std::error::Error for RestoreStateError {}
97112

98-
impl fmt::Display for Error {
99-
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
100-
use self::Error::*;
101-
102-
match self {
103-
#[cfg(target_arch = "x86_64")]
104-
GuestMsrs(err) => write!(f, "Retrieving supported guest MSRs fails: {:?}", err),
105-
#[cfg(target_arch = "aarch64")]
106-
VmCreateGIC(err) => write!(
107-
f,
108-
"Error creating the global interrupt controller: {:?}",
109-
err
110-
),
111-
VmFd(err) => write!(f, "Cannot open the VM file descriptor: {}", err),
112-
VmSetup(err) => write!(f, "Cannot configure the microvm: {}", err),
113-
NotEnoughMemorySlots => write!(
114-
f,
115-
"The number of configured slots is bigger than the maximum reported by KVM"
116-
),
117-
SetUserMemoryRegion(err) => write!(f, "Cannot set the memory regions: {}", err),
118-
#[cfg(target_arch = "x86_64")]
119-
VmGetPit2(err) => write!(f, "Failed to get KVM vm pit state: {}", err),
120-
#[cfg(target_arch = "x86_64")]
121-
VmGetClock(err) => write!(f, "Failed to get KVM vm clock: {}", err),
122-
#[cfg(target_arch = "x86_64")]
123-
VmGetIrqChip(err) => write!(f, "Failed to get KVM vm irqchip: {}", err),
124-
#[cfg(target_arch = "x86_64")]
125-
VmSetPit2(err) => write!(f, "Failed to set KVM vm pit state: {}", err),
126-
#[cfg(target_arch = "x86_64")]
127-
VmSetClock(err) => write!(f, "Failed to set KVM vm clock: {}", err),
128-
#[cfg(target_arch = "x86_64")]
129-
VmSetIrqChip(err) => write!(f, "Failed to set KVM vm irqchip: {}", err),
130-
#[cfg(target_arch = "aarch64")]
131-
SaveGic(err) => write!(f, "Failed to save the VM's GIC state: {:?}", err),
132-
#[cfg(target_arch = "aarch64")]
133-
RestoreGic(err) => write!(f, "Failed to restore the VM's GIC state: {:?}", err),
134-
}
135-
}
136-
}
137-
138113
pub type Result<T> = result::Result<T, Error>;
139114

140115
/// A wrapper around creating and using a VM.

0 commit comments

Comments
 (0)