@@ -14,7 +14,6 @@ mod request;
14
14
15
15
use std:: path:: PathBuf ;
16
16
use std:: sync:: mpsc;
17
- use std:: { fmt, io} ;
18
17
19
18
use logger:: {
20
19
debug, error, info, update_metric_with_elapsed_time, warn, ProcessTimeReporter , METRICS ,
@@ -30,30 +29,18 @@ use vmm::rpc_interface::{VmmAction, VmmActionError, VmmData};
30
29
use vmm:: vmm_config:: snapshot:: SnapshotType ;
31
30
32
31
use crate :: parsed_request:: { ParsedRequest , RequestAction } ;
32
+ use crate :: Error :: ServerCreation ;
33
33
34
34
/// Shorthand type for a request containing a boxed VmmAction.
35
35
pub type ApiRequest = Box < VmmAction > ;
36
36
/// Shorthand type for a response containing a boxed Result.
37
37
pub type ApiResponse = Box < std:: result:: Result < VmmData , VmmActionError > > ;
38
38
39
39
/// Errors thrown when binding the API server to the socket path.
40
- #[ derive( thiserror :: Error ) ]
40
+ #[ derive( Debug ) ]
41
41
pub enum Error {
42
- /// IO related error.
43
- #[ error( "IO error: {0}" ) ]
44
- Io ( io:: Error ) ,
45
- /// EventFD related error.
46
- #[ error( "EventFd error: {0}" ) ]
47
- Eventfd ( io:: Error ) ,
48
- }
49
-
50
- impl fmt:: Debug for Error {
51
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
52
- match * self {
53
- Error :: Io ( ref err) => write ! ( f, "IO error: {}" , err) ,
54
- Error :: Eventfd ( ref err) => write ! ( f, "EventFd error: {}" , err) ,
55
- }
56
- }
42
+ /// HTTP Server creation related error.
43
+ ServerCreation ( ServerError ) ,
57
44
}
58
45
59
46
type Result < T > = std:: result:: Result < T , Error > ;
@@ -137,7 +124,7 @@ impl ApiServer {
137
124
/// .spawn(move || {
138
125
/// ApiServer::new(api_request_sender, vmm_response_receiver, to_vmm_fd)
139
126
/// .bind_and_run(
140
- /// PathBuf::from(api_thread_path_to_socket),
127
+ /// & PathBuf::from(api_thread_path_to_socket),
141
128
/// time_reporter,
142
129
/// seccomp_filters.get("api").unwrap(),
143
130
/// payload_limit,
@@ -161,16 +148,14 @@ impl ApiServer {
161
148
/// ```
162
149
pub fn bind_and_run (
163
150
& mut self ,
164
- path : PathBuf ,
151
+ path : & PathBuf ,
165
152
process_time_reporter : ProcessTimeReporter ,
166
153
seccomp_filter : BpfProgramRef ,
167
154
api_payload_limit : usize ,
168
155
socket_ready : mpsc:: Sender < bool > ,
169
156
) -> Result < ( ) > {
170
- let mut server = HttpServer :: new ( path) . unwrap_or_else ( |err| {
171
- error ! ( "Error creating the HTTP server: {}" , err) ;
172
- std:: process:: exit ( vmm:: FcExitCode :: GenericError as i32 ) ;
173
- } ) ;
157
+ let mut server = HttpServer :: new ( path) . map_err ( ServerCreation ) ?;
158
+
174
159
// Announce main thread that the socket path was created.
175
160
// As per the doc, "A send operation can only fail if the receiving end of a channel is
176
161
// disconnected". so this means that the main thread has exited.
@@ -338,34 +323,6 @@ mod tests {
338
323
339
324
use super :: * ;
340
325
341
- #[ test]
342
- fn test_error_messages ( ) {
343
- let err = Error :: Io ( io:: Error :: from_raw_os_error ( 0 ) ) ;
344
- assert_eq ! (
345
- format!( "{}" , err) ,
346
- format!( "IO error: {}" , io:: Error :: from_raw_os_error( 0 ) )
347
- ) ;
348
- let err = Error :: Eventfd ( io:: Error :: from_raw_os_error ( 0 ) ) ;
349
- assert_eq ! (
350
- format!( "{}" , err) ,
351
- format!( "EventFd error: {}" , io:: Error :: from_raw_os_error( 0 ) )
352
- ) ;
353
- }
354
-
355
- #[ test]
356
- fn test_error_debug ( ) {
357
- let err = Error :: Io ( io:: Error :: from_raw_os_error ( 0 ) ) ;
358
- assert_eq ! (
359
- format!( "{:?}" , err) ,
360
- format!( "IO error: {}" , io:: Error :: from_raw_os_error( 0 ) )
361
- ) ;
362
- let err = Error :: Eventfd ( io:: Error :: from_raw_os_error ( 0 ) ) ;
363
- assert_eq ! (
364
- format!( "{:?}" , err) ,
365
- format!( "EventFd error: {}" , io:: Error :: from_raw_os_error( 0 ) )
366
- ) ;
367
- }
368
-
369
326
#[ test]
370
327
fn test_serve_vmm_action_request ( ) {
371
328
let to_vmm_fd = EventFd :: new ( libc:: EFD_NONBLOCK ) . unwrap ( ) ;
@@ -491,7 +448,7 @@ mod tests {
491
448
. spawn ( move || {
492
449
ApiServer :: new ( api_request_sender, vmm_response_receiver, to_vmm_fd)
493
450
. bind_and_run (
494
- PathBuf :: from ( api_thread_path_to_socket) ,
451
+ & PathBuf :: from ( api_thread_path_to_socket) ,
495
452
ProcessTimeReporter :: new ( Some ( 1 ) , Some ( 1 ) , Some ( 1 ) ) ,
496
453
seccomp_filters. get ( "api" ) . unwrap ( ) ,
497
454
vmm:: HTTP_MAX_PAYLOAD_SIZE ,
@@ -539,7 +496,7 @@ mod tests {
539
496
. spawn ( move || {
540
497
ApiServer :: new ( api_request_sender, vmm_response_receiver, to_vmm_fd)
541
498
. bind_and_run (
542
- PathBuf :: from ( api_thread_path_to_socket) ,
499
+ & PathBuf :: from ( & api_thread_path_to_socket) ,
543
500
ProcessTimeReporter :: new ( Some ( 1 ) , Some ( 1 ) , Some ( 1 ) ) ,
544
501
seccomp_filters. get ( "api" ) . unwrap ( ) ,
545
502
50 ,
0 commit comments