Skip to content

Commit 2465d32

Browse files
zulinx86kalyazin
authored andcommitted
refactor(vmm): use thiserror for StartMicrovmError
Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 02a4a55 commit 2465d32

File tree

1 file changed

+30
-89
lines changed

1 file changed

+30
-89
lines changed

src/vmm/src/builder.rs

Lines changed: 30 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
#[cfg(target_arch = "x86_64")]
77
use std::convert::TryFrom;
8-
use std::fmt::{Display, Formatter};
98
use std::io::{self, Read, Seek, SeekFrom};
109
use std::os::unix::io::{AsRawFd, RawFd};
1110
use std::sync::{Arc, Mutex};
@@ -55,50 +54,76 @@ use crate::vstate::vm::Vm;
5554
use crate::{device_manager, Error, EventManager, RestoreVcpusError, Vmm, VmmEventsObserver};
5655

5756
/// Errors associated with starting the instance.
58-
#[derive(Debug)]
57+
#[derive(Debug, thiserror::Error)]
5958
pub enum StartMicrovmError {
60-
/// Error using CPU template to configure vCPUs
61-
CreateCpuConfig(GuestConfigError),
6259
/// Unable to attach block device to Vmm.
60+
#[error("Unable to attach block device to Vmm: {0}")]
6361
AttachBlockDevice(io::Error),
6462
/// This error is thrown by the minimal boot loader implementation.
63+
#[error("System configuration error: {0:?}")]
6564
ConfigureSystem(crate::arch::Error),
65+
/// Error using CPU template to configure vCPUs
66+
#[error("Unable to successfully create cpu configuration usable for guest vCPUs: {0:?}")]
67+
CreateCpuConfig(GuestConfigError),
6668
/// Internal errors are due to resource exhaustion.
69+
#[error("Cannot create network device. {}", format!("{:?}", .0).replace('\"', ""))]
6770
CreateNetDevice(devices::virtio::net::Error),
6871
/// Failed to create a `RateLimiter` object.
72+
#[error("Cannot create RateLimiter: {0}")]
6973
CreateRateLimiter(io::Error),
7074
/// Memory regions are overlapping or mmap fails.
75+
#[error("Invalid Memory Configuration: {}", format!("{:?}", .0).replace('\"', ""))]
7176
GuestMemoryMmap(utils::vm_memory::Error),
7277
/// Cannot load initrd due to an invalid memory configuration.
78+
#[error("Cannot load initrd due to an invalid memory configuration.")]
7379
InitrdLoad,
7480
/// Cannot load initrd due to an invalid image.
81+
#[error("Cannot load initrd due to an invalid image: {0}")]
7582
InitrdRead(io::Error),
7683
/// Internal error encountered while starting a microVM.
84+
#[error("Internal error while starting microVM: {0}")]
7785
Internal(Error),
7886
/// The kernel command line is invalid.
87+
#[error("Invalid kernel command line: {0}")]
7988
KernelCmdline(String),
8089
/// Cannot load kernel due to invalid memory configuration or invalid kernel image.
90+
#[error(
91+
"Cannot load kernel due to invalid memory configuration or invalid kernel image: {}",
92+
format!("{}", .0).replace('\"', "")
93+
)]
8194
KernelLoader(linux_loader::loader::Error),
8295
/// Cannot load command line string.
96+
#[error("Cannot load command line string: {}", format!("{}", .0).replace('\"', ""))]
8397
LoadCommandline(linux_loader::loader::Error),
8498
/// Cannot start the VM because the kernel builder was not configured.
99+
#[error("Cannot start microvm without kernel configuration.")]
85100
MissingKernelConfig,
86101
/// Cannot start the VM because the size of the guest memory was not specified.
102+
#[error("Cannot start microvm without guest mem_size config.")]
87103
MissingMemSizeConfig,
88104
/// The seccomp filter map is missing a key.
105+
#[error("No seccomp filter for thread category: {0}")]
89106
MissingSeccompFilters(String),
90107
/// The net device configuration is missing the tap device.
108+
#[error("The net device configuration is missing the tap device.")]
91109
NetDeviceNotConfigured,
92110
/// Cannot open the block device backing file.
111+
#[error("Cannot open the block device backing file: {}", format!("{:?}", .0).replace('\"', ""))]
93112
OpenBlockDevice(io::Error),
94113
/// Cannot initialize a MMIO Device or add a device to the MMIO Bus or cmdline.
114+
#[error(
115+
"Cannot initialize a MMIO Device or add a device to the MMIO Bus or cmdline: {}",
116+
format!("{}", .0).replace('\"', "")
117+
)]
95118
RegisterMmioDevice(device_manager::mmio::Error),
96119
/// Cannot restore microvm state.
120+
#[error("Cannot restore microvm state: {0}")]
97121
RestoreMicrovmState(MicrovmStateError),
98122
/// Unable to set VmResources.
123+
#[error("Cannot set vm resources: {0}")]
99124
SetVmResources(VmConfigError),
100125
}
101-
impl std::error::Error for StartMicrovmError {}
126+
102127
/// It's convenient to automatically convert `linux_loader::cmdline::Error`s
103128
/// to `StartMicrovmError`s.
104129
impl std::convert::From<linux_loader::cmdline::Error> for StartMicrovmError {
@@ -107,90 +132,6 @@ impl std::convert::From<linux_loader::cmdline::Error> for StartMicrovmError {
107132
}
108133
}
109134

110-
impl Display for StartMicrovmError {
111-
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
112-
use self::StartMicrovmError::*;
113-
match self {
114-
CreateCpuConfig(err) => {
115-
write!(
116-
f,
117-
"Unable to successfully create cpu configuration usable for guest vCPUs: {:?}",
118-
err
119-
)
120-
}
121-
AttachBlockDevice(err) => {
122-
write!(f, "Unable to attach block device to Vmm: {}", err)
123-
}
124-
ConfigureSystem(err) => write!(f, "System configuration error: {:?}", err),
125-
CreateRateLimiter(err) => write!(f, "Cannot create RateLimiter: {}", err),
126-
CreateNetDevice(err) => {
127-
let mut err_msg = format!("{:?}", err);
128-
err_msg = err_msg.replace('\"', "");
129-
130-
write!(f, "Cannot create network device. {}", err_msg)
131-
}
132-
GuestMemoryMmap(err) => {
133-
// Remove imbricated quotes from error message.
134-
let mut err_msg = format!("{:?}", err);
135-
err_msg = err_msg.replace('\"', "");
136-
write!(f, "Invalid Memory Configuration: {}", err_msg)
137-
}
138-
InitrdLoad => write!(
139-
f,
140-
"Cannot load initrd due to an invalid memory configuration."
141-
),
142-
InitrdRead(err) => write!(f, "Cannot load initrd due to an invalid image: {}", err),
143-
Internal(err) => write!(f, "Internal error while starting microVM: {}", err),
144-
KernelCmdline(err) => write!(f, "Invalid kernel command line: {}", err),
145-
KernelLoader(err) => {
146-
let mut err_msg = format!("{}", err);
147-
err_msg = err_msg.replace('\"', "");
148-
write!(
149-
f,
150-
"Cannot load kernel due to invalid memory configuration or invalid kernel \
151-
image. {}",
152-
err_msg
153-
)
154-
}
155-
LoadCommandline(err) => {
156-
let mut err_msg = format!("{}", err);
157-
err_msg = err_msg.replace('\"', "");
158-
write!(f, "Cannot load command line string. {}", err_msg)
159-
}
160-
MissingKernelConfig => write!(f, "Cannot start microvm without kernel configuration."),
161-
MissingMemSizeConfig => {
162-
write!(f, "Cannot start microvm without guest mem_size config.")
163-
}
164-
MissingSeccompFilters(thread_category) => write!(
165-
f,
166-
"No seccomp filter for thread category: {}",
167-
thread_category
168-
),
169-
NetDeviceNotConfigured => {
170-
write!(f, "The net device configuration is missing the tap device.")
171-
}
172-
OpenBlockDevice(err) => {
173-
let mut err_msg = format!("{:?}", err);
174-
err_msg = err_msg.replace('\"', "");
175-
176-
write!(f, "Cannot open the block device backing file. {}", err_msg)
177-
}
178-
RegisterMmioDevice(err) => {
179-
let mut err_msg = format!("{}", err);
180-
err_msg = err_msg.replace('\"', "");
181-
write!(
182-
f,
183-
"Cannot initialize a MMIO Device or add a device to the MMIO Bus or cmdline. \
184-
{}",
185-
err_msg
186-
)
187-
}
188-
RestoreMicrovmState(err) => write!(f, "Cannot restore microvm state. Error: {}", err),
189-
SetVmResources(err) => write!(f, "Cannot set vm resources. Error: {}", err),
190-
}
191-
}
192-
}
193-
194135
// Wrapper over io::Stdin that implements `Serial::ReadableFd` and `vmm::VmmEventsObserver`.
195136
pub(crate) struct SerialStdin(io::Stdin);
196137
impl SerialStdin {

0 commit comments

Comments
 (0)