Skip to content

Commit 4a1cbbd

Browse files
nayihzwearyzen
authored andcommitted
fix: use thiserror::Error in vmm_config
vmm: use thiserror::Error Signed-off-by: czybjtu <[email protected]>
1 parent 03262b2 commit 4a1cbbd

File tree

5 files changed

+18
-103
lines changed

5 files changed

+18
-103
lines changed

src/vmm/src/vmm_config/boot_source.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::fmt::{Display, Formatter, Result};
54
use std::fs::File;
65
use std::io;
76

@@ -38,33 +37,19 @@ pub struct BootSourceConfig {
3837
}
3938

4039
/// Errors associated with actions on `BootSourceConfig`.
41-
#[derive(Debug)]
40+
#[derive(Debug, thiserror::Error)]
4241
pub enum BootSourceConfigError {
4342
/// The kernel file cannot be opened.
43+
#[error("The kernel file cannot be opened: {0}")]
4444
InvalidKernelPath(io::Error),
4545
/// The initrd file cannot be opened.
46+
#[error("The initrd file cannot be opened due to invalid path or invalid permissions. {0}")]
4647
InvalidInitrdPath(io::Error),
4748
/// The kernel command line is invalid.
49+
#[error("The kernel command line is invalid: {0}")]
4850
InvalidKernelCommandLine(String),
4951
}
5052

51-
impl Display for BootSourceConfigError {
52-
fn fmt(&self, f: &mut Formatter) -> Result {
53-
use self::BootSourceConfigError::*;
54-
match *self {
55-
InvalidKernelPath(ref err) => write!(f, "The kernel file cannot be opened: {}", err),
56-
InvalidInitrdPath(ref err) => write!(
57-
f,
58-
"The initrd file cannot be opened due to invalid path or invalid permissions. {}",
59-
err,
60-
),
61-
InvalidKernelCommandLine(ref err) => {
62-
write!(f, "The kernel command line is invalid: {}", err.as_str())
63-
}
64-
}
65-
}
66-
}
67-
6853
/// Holds the kernel specification (both configuration as well as runtime details).
6954
#[derive(Default)]
7055
pub struct BootSource {

src/vmm/src/vmm_config/logger.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
//! Auxiliary module for configuring the logger.
5-
use std::fmt::{Display, Formatter};
65
use std::path::PathBuf;
76

87
use logger::{LevelFilter, LOGGER};
@@ -106,21 +105,13 @@ impl LoggerConfig {
106105
}
107106

108107
/// Errors associated with actions on the `LoggerConfig`.
109-
#[derive(Debug)]
108+
#[derive(Debug, thiserror::Error)]
110109
pub enum LoggerConfigError {
111110
/// Cannot initialize the logger due to bad user input.
111+
#[error("{}", format!("{:?}", .0).replace('\"', ""))]
112112
InitializationFailure(String),
113113
}
114114

115-
impl Display for LoggerConfigError {
116-
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
117-
use self::LoggerConfigError::*;
118-
match *self {
119-
InitializationFailure(ref err_msg) => write!(f, "{}", err_msg.replace('\"', "")),
120-
}
121-
}
122-
}
123-
124115
/// Configures the logger as described in `logger_cfg`.
125116
pub fn init_logger(
126117
logger_cfg: LoggerConfig,
@@ -215,19 +206,6 @@ mod tests {
215206
}
216207
}
217208

218-
#[test]
219-
fn test_error_display() {
220-
assert_eq!(
221-
format!(
222-
"{}",
223-
LoggerConfigError::InitializationFailure(String::from(
224-
"Failed to initialize logger"
225-
))
226-
),
227-
"Failed to initialize logger"
228-
);
229-
}
230-
231209
#[test]
232210
fn test_new_logger_config() {
233211
let logger_config =

src/vmm/src/vmm_config/metrics.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
//! Auxiliary module for configuring the metrics system.
5-
use std::fmt::{Display, Formatter};
65
use std::path::PathBuf;
76

87
use logger::METRICS;
@@ -18,21 +17,13 @@ pub struct MetricsConfig {
1817
}
1918

2019
/// Errors associated with actions on the `MetricsConfig`.
21-
#[derive(Debug)]
20+
#[derive(Debug, thiserror::Error)]
2221
pub enum MetricsConfigError {
2322
/// Cannot initialize the metrics system due to bad user input.
23+
#[error("{}", format!("{:?}", .0).replace('\"', ""))]
2424
InitializationFailure(String),
2525
}
2626

27-
impl Display for MetricsConfigError {
28-
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
29-
use self::MetricsConfigError::*;
30-
match *self {
31-
InitializationFailure(ref err_msg) => write!(f, "{}", err_msg.replace('\"', "")),
32-
}
33-
}
34-
}
35-
3627
/// Configures the metrics as described in `metrics_cfg`.
3728
pub fn init_metrics(metrics_cfg: MetricsConfig) -> std::result::Result<(), MetricsConfigError> {
3829
let writer = FcLineWriter::new(
@@ -67,17 +58,4 @@ mod tests {
6758
assert!(init_metrics(desc.clone()).is_ok());
6859
assert!(init_metrics(desc).is_err());
6960
}
70-
71-
#[test]
72-
fn test_error_display() {
73-
assert_eq!(
74-
format!(
75-
"{}",
76-
MetricsConfigError::InitializationFailure(String::from(
77-
"Failed to initialize metrics"
78-
))
79-
),
80-
"Failed to initialize metrics"
81-
);
82-
}
8361
}

src/vmm/src/vmm_config/mmds.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
4-
use std::fmt::{Display, Formatter, Result};
53
use std::net::Ipv4Addr;
64

75
use mmds::data_store;
@@ -40,46 +38,22 @@ impl MmdsConfig {
4038
}
4139

4240
/// MMDS configuration related errors.
43-
#[derive(Debug)]
41+
#[derive(Debug, thiserror::Error)]
4442
pub enum MmdsConfigError {
4543
/// The network interfaces list provided is empty.
44+
#[error("The list of network interface IDs that allow forwarding MMDS requests is empty.")]
4645
EmptyNetworkIfaceList,
4746
/// The provided IPv4 address is not link-local valid.
47+
#[error("The MMDS IPv4 address is not link local.")]
4848
InvalidIpv4Addr,
4949
/// The network interfaces list provided contains IDs that
5050
/// does not correspond to any existing network interface.
51+
#[error(
52+
"The list of network interface IDs provided contains at least one ID that does not \
53+
correspond to any existing network interface."
54+
)]
5155
InvalidNetworkInterfaceId,
5256
/// MMDS version could not be configured.
57+
#[error("The MMDS could not be configured to version {0}: {1}")]
5358
MmdsVersion(MmdsVersion, data_store::Error),
5459
}
55-
56-
impl Display for MmdsConfigError {
57-
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
58-
match self {
59-
MmdsConfigError::EmptyNetworkIfaceList => {
60-
write!(
61-
f,
62-
"The list of network interface IDs that allow forwarding MMDS requests is \
63-
empty."
64-
)
65-
}
66-
MmdsConfigError::InvalidIpv4Addr => {
67-
write!(f, "The MMDS IPv4 address is not link local.")
68-
}
69-
MmdsConfigError::InvalidNetworkInterfaceId => {
70-
write!(
71-
f,
72-
"The list of network interface IDs provided contains at least one ID that \
73-
does not correspond to any existing network interface."
74-
)
75-
}
76-
MmdsConfigError::MmdsVersion(version, err) => {
77-
write!(
78-
f,
79-
"The MMDS could not be configured to version {}: {}",
80-
version, err
81-
)
82-
}
83-
}
84-
}
85-
}

tests/integration_tests/build/test_coverage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def is_on_skylake():
2525
# Checkout the cpuid crate. In the future other
2626
# differences may appear.
2727
if utils.is_io_uring_supported():
28-
COVERAGE_DICT = {"Intel": 83.66, "AMD": 83.24, "ARM": 83.06}
28+
COVERAGE_DICT = {"Intel": 83.76, "AMD": 83.34, "ARM": 83.12}
2929
else:
30-
COVERAGE_DICT = {"Intel": 80.90, "AMD": 80.45, "ARM": 80.06}
30+
COVERAGE_DICT = {"Intel": 81.02, "AMD": 80.55, "ARM": 80.12}
3131

3232
PROC_MODEL = proc.proc_type()
3333

0 commit comments

Comments
 (0)