Skip to content

Commit 3236ae6

Browse files
joaolealJonathan Wollett-Light
authored andcommitted
refactor: Warn on clippy::error_impl_error
Enables warning on `clippy::error_impl_error` and fixes the produced warnings. Signed-off-by: joaoleal <[email protected]> Co-authored-by: Jonathan Wollett-Light <[email protected]>
1 parent d4a264c commit 3236ae6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+421
-352
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cast_sign_loss = "warn"
1616
exit = "warn"
1717
tests_outside_test_module = "warn"
1818
assertions_on_result_states = "warn"
19+
error_impl_error = "warn"
1920

2021
[profile.dev]
2122
panic = "abort"

src/firecracker/src/api_server/parsed_request.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(crate) struct ParsedRequest {
5858
}
5959

6060
impl TryFrom<&Request> for ParsedRequest {
61-
type Error = Error;
61+
type Error = RequestError;
6262
fn try_from(request: &Request) -> Result<Self, Self::Error> {
6363
let request_uri = request.uri().get_abs_path().to_string();
6464
let description = describe(
@@ -109,9 +109,10 @@ impl TryFrom<&Request> for ParsedRequest {
109109
}
110110
(Method::Patch, "vm", Some(body)) => parse_patch_vm_state(body),
111111
(Method::Patch, _, None) => method_to_error(Method::Patch),
112-
(method, unknown_uri, _) => {
113-
Err(Error::InvalidPathMethod(unknown_uri.to_string(), method))
114-
}
112+
(method, unknown_uri, _) => Err(RequestError::InvalidPathMethod(
113+
unknown_uri.to_string(),
114+
method,
115+
)),
115116
}
116117
}
117118
}
@@ -246,25 +247,25 @@ fn describe_with_body(method: Method, path: &str, payload_value: &Body) -> Strin
246247
}
247248

248249
/// Generates a `GenericError` for each request method.
249-
pub(crate) fn method_to_error(method: Method) -> Result<ParsedRequest, Error> {
250+
pub(crate) fn method_to_error(method: Method) -> Result<ParsedRequest, RequestError> {
250251
match method {
251-
Method::Get => Err(Error::Generic(
252+
Method::Get => Err(RequestError::Generic(
252253
StatusCode::BadRequest,
253254
"GET request cannot have a body.".to_string(),
254255
)),
255-
Method::Put => Err(Error::Generic(
256+
Method::Put => Err(RequestError::Generic(
256257
StatusCode::BadRequest,
257258
"Empty PUT request.".to_string(),
258259
)),
259-
Method::Patch => Err(Error::Generic(
260+
Method::Patch => Err(RequestError::Generic(
260261
StatusCode::BadRequest,
261262
"Empty PATCH request.".to_string(),
262263
)),
263264
}
264265
}
265266

266267
#[derive(Debug, thiserror::Error)]
267-
pub(crate) enum Error {
268+
pub(crate) enum RequestError {
268269
// The resource ID is empty.
269270
#[error("The ID cannot be empty.")]
270271
EmptyID,
@@ -283,30 +284,30 @@ pub(crate) enum Error {
283284
}
284285

285286
// It's convenient to turn errors into HTTP responses directly.
286-
impl From<Error> for Response {
287-
fn from(err: Error) -> Self {
287+
impl From<RequestError> for Response {
288+
fn from(err: RequestError) -> Self {
288289
let msg = ApiServer::json_fault_message(format!("{}", err));
289290
match err {
290-
Error::Generic(status, _) => ApiServer::json_response(status, msg),
291-
Error::EmptyID
292-
| Error::InvalidID
293-
| Error::InvalidPathMethod(_, _)
294-
| Error::SerdeJson(_) => ApiServer::json_response(StatusCode::BadRequest, msg),
291+
RequestError::Generic(status, _) => ApiServer::json_response(status, msg),
292+
RequestError::EmptyID
293+
| RequestError::InvalidID
294+
| RequestError::InvalidPathMethod(_, _)
295+
| RequestError::SerdeJson(_) => ApiServer::json_response(StatusCode::BadRequest, msg),
295296
}
296297
}
297298
}
298299

299300
// This function is supposed to do id validation for requests.
300-
pub(crate) fn checked_id(id: &str) -> Result<&str, Error> {
301+
pub(crate) fn checked_id(id: &str) -> Result<&str, RequestError> {
301302
// todo: are there any checks we want to do on id's?
302303
// not allow them to be empty strings maybe?
303304
// check: ensure string is not empty
304305
if id.is_empty() {
305-
return Err(Error::EmptyID);
306+
return Err(RequestError::EmptyID);
306307
}
307308
// check: ensure string is alphanumeric
308309
if !id.chars().all(|c| c == '_' || c.is_alphanumeric()) {
309-
return Err(Error::InvalidID);
310+
return Err(RequestError::InvalidID);
310311
}
311312
Ok(id)
312313
}
@@ -435,7 +436,7 @@ pub mod tests {
435436
let parsed_request = ParsedRequest::try_from(&req);
436437
assert!(matches!(
437438
&parsed_request,
438-
Err(Error::Generic(StatusCode::BadRequest, s)) if s == "GET request cannot have a body.",
439+
Err(RequestError::Generic(StatusCode::BadRequest, s)) if s == "GET request cannot have a body.",
439440
));
440441
}
441442

@@ -451,7 +452,7 @@ pub mod tests {
451452
let parsed_request = ParsedRequest::try_from(&req);
452453
assert!(matches!(
453454
&parsed_request,
454-
Err(Error::Generic(StatusCode::BadRequest, s)) if s == "Empty PUT request.",
455+
Err(RequestError::Generic(StatusCode::BadRequest, s)) if s == "Empty PUT request.",
455456
));
456457
}
457458

@@ -467,7 +468,7 @@ pub mod tests {
467468
let parsed_request = ParsedRequest::try_from(&req);
468469
assert!(matches!(
469470
&parsed_request,
470-
Err(Error::Generic(StatusCode::BadRequest, s)) if s == "Empty PATCH request.",
471+
Err(RequestError::Generic(StatusCode::BadRequest, s)) if s == "Empty PATCH request.",
471472
));
472473
}
473474

@@ -476,23 +477,23 @@ pub mod tests {
476477
// Generic error.
477478
let mut buf = Cursor::new(vec![0]);
478479
let response: Response =
479-
Error::Generic(StatusCode::BadRequest, "message".to_string()).into();
480+
RequestError::Generic(StatusCode::BadRequest, "message".to_string()).into();
480481
response.write_all(&mut buf).unwrap();
481482
let body = ApiServer::json_fault_message("message");
482483
let expected_response = http_response(&body, 400);
483484
assert_eq!(buf.into_inner(), expected_response.as_bytes());
484485

485486
// Empty ID error.
486487
let mut buf = Cursor::new(vec![0]);
487-
let response: Response = Error::EmptyID.into();
488+
let response: Response = RequestError::EmptyID.into();
488489
response.write_all(&mut buf).unwrap();
489490
let body = ApiServer::json_fault_message("The ID cannot be empty.");
490491
let expected_response = http_response(&body, 400);
491492
assert_eq!(buf.into_inner(), expected_response.as_bytes());
492493

493494
// Invalid ID error.
494495
let mut buf = Cursor::new(vec![0]);
495-
let response: Response = Error::InvalidID.into();
496+
let response: Response = RequestError::InvalidID.into();
496497
response.write_all(&mut buf).unwrap();
497498
let body = ApiServer::json_fault_message(
498499
"API Resource IDs can only contain alphanumeric characters and underscores.",
@@ -502,7 +503,8 @@ pub mod tests {
502503

503504
// Invalid path or method error.
504505
let mut buf = Cursor::new(vec![0]);
505-
let response: Response = Error::InvalidPathMethod("path".to_string(), Method::Get).into();
506+
let response: Response =
507+
RequestError::InvalidPathMethod("path".to_string(), Method::Get).into();
506508
response.write_all(&mut buf).unwrap();
507509
let body = ApiServer::json_fault_message(format!(
508510
"Invalid request method and/or path: {} {}.",
@@ -515,7 +517,7 @@ pub mod tests {
515517
// Serde error.
516518
let mut buf = Cursor::new(vec![0]);
517519
let serde_error = serde_json::Value::from_str("").unwrap_err();
518-
let response: Response = Error::SerdeJson(serde_error).into();
520+
let response: Response = RequestError::SerdeJson(serde_error).into();
519521
response.write_all(&mut buf).unwrap();
520522
let body = ApiServer::json_fault_message(
521523
"An error occurred when deserializing the json body of a request: EOF while parsing a \

src/firecracker/src/api_server/request/actions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55
use vmm::logger::{IncMetric, METRICS};
66
use vmm::rpc_interface::VmmAction;
77

8-
use super::super::parsed_request::{Error, ParsedRequest};
8+
use super::super::parsed_request::{ParsedRequest, RequestError};
99
use super::Body;
1010
#[cfg(target_arch = "aarch64")]
1111
use super::StatusCode;
@@ -28,7 +28,7 @@ struct ActionBody {
2828
action_type: ActionType,
2929
}
3030

31-
pub(crate) fn parse_put_actions(body: &Body) -> Result<ParsedRequest, Error> {
31+
pub(crate) fn parse_put_actions(body: &Body) -> Result<ParsedRequest, RequestError> {
3232
METRICS.put_api_requests.actions_count.inc();
3333
let action_body = serde_json::from_slice::<ActionBody>(body.raw()).map_err(|err| {
3434
METRICS.put_api_requests.actions_fails.inc();
@@ -41,7 +41,7 @@ pub(crate) fn parse_put_actions(body: &Body) -> Result<ParsedRequest, Error> {
4141
ActionType::SendCtrlAltDel => {
4242
// SendCtrlAltDel not supported on aarch64.
4343
#[cfg(target_arch = "aarch64")]
44-
return Err(Error::Generic(
44+
return Err(RequestError::Generic(
4545
StatusCode::BadRequest,
4646
"SendCtrlAltDel does not supported on aarch64.".to_string(),
4747
));

src/firecracker/src/api_server/request/balloon.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ use vmm::vmm_config::balloon::{
77
BalloonDeviceConfig, BalloonUpdateConfig, BalloonUpdateStatsConfig,
88
};
99

10-
use super::super::parsed_request::{Error, ParsedRequest};
10+
use super::super::parsed_request::{ParsedRequest, RequestError};
1111
use super::Body;
1212

13-
pub(crate) fn parse_get_balloon(path_second_token: Option<&str>) -> Result<ParsedRequest, Error> {
13+
pub(crate) fn parse_get_balloon(
14+
path_second_token: Option<&str>,
15+
) -> Result<ParsedRequest, RequestError> {
1416
match path_second_token {
1517
Some(stats_path) => match stats_path {
1618
"statistics" => Ok(ParsedRequest::new_sync(VmmAction::GetBalloonStats)),
17-
_ => Err(Error::Generic(
19+
_ => Err(RequestError::Generic(
1820
StatusCode::BadRequest,
1921
format!("Unrecognized GET request path `{}`.", stats_path),
2022
)),
@@ -23,7 +25,7 @@ pub(crate) fn parse_get_balloon(path_second_token: Option<&str>) -> Result<Parse
2325
}
2426
}
2527

26-
pub(crate) fn parse_put_balloon(body: &Body) -> Result<ParsedRequest, Error> {
28+
pub(crate) fn parse_put_balloon(body: &Body) -> Result<ParsedRequest, RequestError> {
2729
Ok(ParsedRequest::new_sync(VmmAction::SetBalloonDevice(
2830
serde_json::from_slice::<BalloonDeviceConfig>(body.raw())?,
2931
)))
@@ -32,13 +34,13 @@ pub(crate) fn parse_put_balloon(body: &Body) -> Result<ParsedRequest, Error> {
3234
pub(crate) fn parse_patch_balloon(
3335
body: &Body,
3436
path_second_token: Option<&str>,
35-
) -> Result<ParsedRequest, Error> {
37+
) -> Result<ParsedRequest, RequestError> {
3638
match path_second_token {
3739
Some(config_path) => match config_path {
3840
"statistics" => Ok(ParsedRequest::new_sync(VmmAction::UpdateBalloonStatistics(
3941
serde_json::from_slice::<BalloonUpdateStatsConfig>(body.raw())?,
4042
))),
41-
_ => Err(Error::Generic(
43+
_ => Err(RequestError::Generic(
4244
StatusCode::BadRequest,
4345
format!("Unrecognized PATCH request path `{}`.", config_path),
4446
)),

src/firecracker/src/api_server/request/boot_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use vmm::logger::{IncMetric, METRICS};
55
use vmm::rpc_interface::VmmAction;
66
use vmm::vmm_config::boot_source::BootSourceConfig;
77

8-
use super::super::parsed_request::{Error, ParsedRequest};
8+
use super::super::parsed_request::{ParsedRequest, RequestError};
99
use super::Body;
1010

11-
pub(crate) fn parse_put_boot_source(body: &Body) -> Result<ParsedRequest, Error> {
11+
pub(crate) fn parse_put_boot_source(body: &Body) -> Result<ParsedRequest, RequestError> {
1212
METRICS.put_api_requests.boot_source_count.inc();
1313
Ok(ParsedRequest::new_sync(VmmAction::ConfigureBootSource(
1414
serde_json::from_slice::<BootSourceConfig>(body.raw()).map_err(|err| {

src/firecracker/src/api_server/request/cpu_configuration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use vmm::cpu_config::templates::CustomCpuTemplate;
55
use vmm::logger::{IncMetric, METRICS};
66
use vmm::rpc_interface::VmmAction;
77

8-
use super::super::parsed_request::{Error, ParsedRequest};
8+
use super::super::parsed_request::{ParsedRequest, RequestError};
99
use super::Body;
1010

11-
pub(crate) fn parse_put_cpu_config(body: &Body) -> Result<ParsedRequest, Error> {
11+
pub(crate) fn parse_put_cpu_config(body: &Body) -> Result<ParsedRequest, RequestError> {
1212
METRICS.put_api_requests.cpu_cfg_count.inc();
1313

1414
// Convert the API request into a a deserialized/binary format
1515
Ok(ParsedRequest::new_sync(VmmAction::PutCpuConfiguration(
1616
CustomCpuTemplate::try_from(body.raw()).map_err(|err| {
1717
METRICS.put_api_requests.cpu_cfg_fails.inc();
18-
Error::SerdeJson(err)
18+
RequestError::SerdeJson(err)
1919
})?,
2020
)))
2121
}
@@ -83,7 +83,7 @@ mod tests {
8383
expected_err_count
8484
);
8585
assert!(
86-
matches!(invalid_put_result, Err(Error::SerdeJson(_))),
86+
matches!(invalid_put_result, Err(RequestError::SerdeJson(_))),
8787
"{:?}",
8888
invalid_put_result
8989
);

src/firecracker/src/api_server/request/drive.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ use vmm::logger::{IncMetric, METRICS};
55
use vmm::rpc_interface::VmmAction;
66
use vmm::vmm_config::drive::{BlockDeviceConfig, BlockDeviceUpdateConfig};
77

8-
use super::super::parsed_request::{checked_id, Error, ParsedRequest};
8+
use super::super::parsed_request::{checked_id, ParsedRequest, RequestError};
99
use super::{Body, StatusCode};
1010

1111
pub(crate) fn parse_put_drive(
1212
body: &Body,
1313
id_from_path: Option<&str>,
14-
) -> Result<ParsedRequest, Error> {
14+
) -> Result<ParsedRequest, RequestError> {
1515
METRICS.put_api_requests.drive_count.inc();
1616
let id = if let Some(id) = id_from_path {
1717
checked_id(id)?
1818
} else {
1919
METRICS.put_api_requests.drive_fails.inc();
20-
return Err(Error::EmptyID);
20+
return Err(RequestError::EmptyID);
2121
};
2222

2323
let device_cfg = serde_json::from_slice::<BlockDeviceConfig>(body.raw()).map_err(|err| {
@@ -27,7 +27,7 @@ pub(crate) fn parse_put_drive(
2727

2828
if id != device_cfg.drive_id {
2929
METRICS.put_api_requests.drive_fails.inc();
30-
Err(Error::Generic(
30+
Err(RequestError::Generic(
3131
StatusCode::BadRequest,
3232
"The id from the path does not match the id from the body!".to_string(),
3333
))
@@ -41,13 +41,13 @@ pub(crate) fn parse_put_drive(
4141
pub(crate) fn parse_patch_drive(
4242
body: &Body,
4343
id_from_path: Option<&str>,
44-
) -> Result<ParsedRequest, Error> {
44+
) -> Result<ParsedRequest, RequestError> {
4545
METRICS.patch_api_requests.drive_count.inc();
4646
let id = if let Some(id) = id_from_path {
4747
checked_id(id)?
4848
} else {
4949
METRICS.patch_api_requests.drive_fails.inc();
50-
return Err(Error::EmptyID);
50+
return Err(RequestError::EmptyID);
5151
};
5252

5353
let block_device_update_cfg: BlockDeviceUpdateConfig =
@@ -58,7 +58,7 @@ pub(crate) fn parse_patch_drive(
5858

5959
if id != block_device_update_cfg.drive_id {
6060
METRICS.patch_api_requests.drive_fails.inc();
61-
return Err(Error::Generic(
61+
return Err(RequestError::Generic(
6262
StatusCode::BadRequest,
6363
String::from("The id from the path does not match the id from the body!"),
6464
));

src/firecracker/src/api_server/request/entropy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
use vmm::rpc_interface::VmmAction;
55
use vmm::vmm_config::entropy::EntropyDeviceConfig;
66

7-
use super::super::parsed_request::{Error, ParsedRequest};
7+
use super::super::parsed_request::{ParsedRequest, RequestError};
88
use super::Body;
99

10-
pub(crate) fn parse_put_entropy(body: &Body) -> Result<ParsedRequest, Error> {
10+
pub(crate) fn parse_put_entropy(body: &Body) -> Result<ParsedRequest, RequestError> {
1111
let cfg = serde_json::from_slice::<EntropyDeviceConfig>(body.raw())?;
1212
Ok(ParsedRequest::new_sync(VmmAction::SetEntropyDevice(cfg)))
1313
}

src/firecracker/src/api_server/request/instance_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
use vmm::logger::{IncMetric, METRICS};
55
use vmm::rpc_interface::VmmAction;
66

7-
use super::super::parsed_request::{Error, ParsedRequest};
7+
use super::super::parsed_request::{ParsedRequest, RequestError};
88

9-
pub(crate) fn parse_get_instance_info() -> Result<ParsedRequest, Error> {
9+
pub(crate) fn parse_get_instance_info() -> Result<ParsedRequest, RequestError> {
1010
METRICS.get_api_requests.instance_info_count.inc();
1111
Ok(ParsedRequest::new_sync(VmmAction::GetVmInstanceInfo))
1212
}

src/firecracker/src/api_server/request/logger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
use vmm::logger::{IncMetric, METRICS};
55
use vmm::rpc_interface::VmmAction;
66

7-
use super::super::parsed_request::{Error, ParsedRequest};
7+
use super::super::parsed_request::{ParsedRequest, RequestError};
88
use super::Body;
99

10-
pub(crate) fn parse_put_logger(body: &Body) -> Result<ParsedRequest, Error> {
10+
pub(crate) fn parse_put_logger(body: &Body) -> Result<ParsedRequest, RequestError> {
1111
METRICS.put_api_requests.logger_count.inc();
1212
let res = serde_json::from_slice::<vmm::logger::LoggerConfig>(body.raw());
1313
let config = res.map_err(|err| {

0 commit comments

Comments
 (0)