From fbd1a6c5f159d1ce3322ebdf1b13ad2adce7a1b1 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Tue, 26 May 2020 15:20:59 -0700 Subject: [PATCH] vmm: api: Return complete error responses in handle_http_request() Instead of responding only headers with error code, we now return complete error responses to HTTP requests with errors (e.g. undefined endpoints and InternalSeverError). Fixes: #472 Signed-off-by: Bo Chen --- vmm/src/api/http.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/vmm/src/api/http.rs b/vmm/src/api/http.rs index f05c691c7..f8d4e3a82 100644 --- a/vmm/src/api/http.rs +++ b/vmm/src/api/http.rs @@ -26,6 +26,12 @@ pub enum HttpError { /// Attempt to access unsupported HTTP method BadRequest, + /// Undefined endpoints + NotFound, + + /// Internal Server Error + InternalServerError, + /// Could not create a VM VmCreate(ApiError), @@ -194,9 +200,12 @@ fn handle_http_request( let mut response = match HTTP_ROUTES.routes.get(&path) { Some(route) => match api_notifier.try_clone() { Ok(notifier) => route.handle_request(&request, notifier, api_sender.clone()), - Err(_) => Response::new(Version::Http11, StatusCode::InternalServerError), + Err(_) => error_response( + HttpError::InternalServerError, + StatusCode::InternalServerError, + ), }, - None => Response::new(Version::Http11, StatusCode::NotFound), + None => error_response(HttpError::NotFound, StatusCode::NotFound), }; response.set_server("Cloud Hypervisor API");