From bc15846b674cc8c7bfcc993b6e7d031fd723b514 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 12 Dec 2023 01:32:18 +0100 Subject: [PATCH] Improve errors reporting --- .../src/controllers/network_controller.rs | 32 +++++++++++++++---- .../src/controllers/vm_controller.rs | 23 ++++++++++--- virtweb_frontend/src/App.tsx | 3 ++ virtweb_frontend/src/api/ApiClient.ts | 2 +- .../hooks/providers/AlertDialogProvider.tsx | 5 ++- .../src/routes/EditNetworkRoute.tsx | 4 +-- virtweb_frontend/src/routes/EditVMRoute.tsx | 2 +- virtweb_frontend/src/routes/HomeRoute.tsx | 13 ++++++++ 8 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 virtweb_frontend/src/routes/HomeRoute.tsx diff --git a/virtweb_backend/src/controllers/network_controller.rs b/virtweb_backend/src/controllers/network_controller.rs index 0bd9559..246d9e6 100644 --- a/virtweb_backend/src/controllers/network_controller.rs +++ b/virtweb_backend/src/controllers/network_controller.rs @@ -14,11 +14,21 @@ pub async fn create(client: LibVirtReq, req: web::Json) -> HttpResu Ok(d) => d, Err(e) => { log::error!("Failed to extract network info! {e}"); - return Ok(HttpResponse::BadRequest().body(e.to_string())); + return Ok( + HttpResponse::BadRequest().json(format!("Failed to extract network info! {e}")) + ); } }; - let uid = client.update_network(network).await?; + let uid = match client.update_network(network).await { + Ok(u) => u, + Err(e) => { + log::error!("Failed to update network! {e}"); + return Ok( + HttpResponse::InternalServerError().json(format!("Failed to update network! {e}")) + ); + } + }; Ok(HttpResponse::Ok().json(NetworkID { uid })) } @@ -56,7 +66,13 @@ pub async fn update( ) -> HttpResult { let mut network = body.0.to_virt_network()?; network.uuid = Some(path.uid); - client.update_network(network).await?; + + if let Err(e) = client.update_network(network).await { + log::error!("Failed to update network! {e}"); + return Ok( + HttpResponse::InternalServerError().json(format!("Failed to update network!\n${e}")) + ); + } Ok(HttpResponse::Ok().json("Network updated")) } @@ -115,9 +131,13 @@ pub async fn status(client: LibVirtReq, id: web::Path) -> HttpResult /// Start a network pub async fn start(client: LibVirtReq, id: web::Path) -> HttpResult { - client.start_network(id.uid).await?; - - Ok(HttpResponse::Accepted().json("Network started")) + match client.start_network(id.uid).await { + Ok(_) => Ok(HttpResponse::Accepted().json("Network started")), + Err(e) => { + log::error!("Failed to start a network! {e}: {:?}", id.uid); + Ok(HttpResponse::InternalServerError().json(format!("Failed to start network! {e}"))) + } + } } /// Stop a network diff --git a/virtweb_backend/src/controllers/vm_controller.rs b/virtweb_backend/src/controllers/vm_controller.rs index 07c0bf8..490b389 100644 --- a/virtweb_backend/src/controllers/vm_controller.rs +++ b/virtweb_backend/src/controllers/vm_controller.rs @@ -24,10 +24,20 @@ pub async fn create(client: LibVirtReq, req: web::Json) -> HttpResult { Ok(d) => d, Err(e) => { log::error!("Failed to extract domain info! {e}"); - return Ok(HttpResponse::BadRequest().body(e.to_string())); + return Ok( + HttpResponse::BadRequest().json(format!("Failed to extract domain info!\n{e}")) + ); + } + }; + let id = match client.update_domain(domain).await { + Ok(i) => i, + Err(e) => { + log::error!("Failed to update domain info! {e}"); + return Ok( + HttpResponse::InternalServerError().json(format!("Failed to create domain!\n{e}")) + ); } }; - let id = client.update_domain(domain).await?; Ok(HttpResponse::Ok().json(VMUuid { uuid: id })) } @@ -96,11 +106,14 @@ pub async fn update( ) -> HttpResult { let mut domain = req.0.to_domain().map_err(|e| { log::error!("Failed to extract domain info! {e}"); - HttpResponse::BadRequest().body(e.to_string()) + HttpResponse::BadRequest().json(format!("Failed to extract domain info! {e}")) })?; domain.uuid = Some(id.uid); - client.update_domain(domain).await?; + if let Err(e) = client.update_domain(domain).await { + log::error!("Failed to update domain info! {e}"); + return Ok(HttpResponse::BadRequest().json(format!("Failed to update domain info!\n{e}"))); + } Ok(HttpResponse::Ok().finish()) } @@ -159,7 +172,7 @@ pub async fn start(client: LibVirtReq, id: web::Path) -> HttpRe Ok(_) => HttpResponse::Ok().json("Domain started"), Err(e) => { log::error!("Failed to start domain {:?} ! {e}", id.uid); - HttpResponse::InternalServerError().json("Failed to start domain!") + HttpResponse::InternalServerError().json(format!("Failed to start domain! - {e}")) } }) } diff --git a/virtweb_frontend/src/App.tsx b/virtweb_frontend/src/App.tsx index 977649c..cec2626 100644 --- a/virtweb_frontend/src/App.tsx +++ b/virtweb_frontend/src/App.tsx @@ -27,6 +27,7 @@ import { BaseLoginPage } from "./widgets/BaseLoginPage"; import { ViewNetworkRoute } from "./routes/ViewNetworkRoute"; import { VMXMLRoute } from "./routes/VMXMLRoute"; import { NetXMLRoute } from "./routes/NetXMLRoute"; +import { HomeRoute } from "./routes/HomeRoute"; interface AuthContext { signedIn: boolean; @@ -47,6 +48,8 @@ export function App() { createRoutesFromElements( signedIn || ServerApi.Config.auth_disabled ? ( }> + } /> + } /> } /> diff --git a/virtweb_frontend/src/api/ApiClient.ts b/virtweb_frontend/src/api/ApiClient.ts index 33114bb..c113fc0 100644 --- a/virtweb_frontend/src/api/ApiClient.ts +++ b/virtweb_frontend/src/api/ApiClient.ts @@ -17,7 +17,7 @@ interface APIResponse { export class ApiError extends Error { constructor(message: string, public code: number, public data: any) { - super(message); + super(`HTTP status: ${code}\nMessage: ${message}\nData=${data}`); } } diff --git a/virtweb_frontend/src/hooks/providers/AlertDialogProvider.tsx b/virtweb_frontend/src/hooks/providers/AlertDialogProvider.tsx index d0d996a..ec6a756 100644 --- a/virtweb_frontend/src/hooks/providers/AlertDialogProvider.tsx +++ b/virtweb_frontend/src/hooks/providers/AlertDialogProvider.tsx @@ -49,7 +49,10 @@ export function AlertDialogProvider(p: PropsWithChildren): React.ReactElement { > {title && {title}} - + {message} diff --git a/virtweb_frontend/src/routes/EditNetworkRoute.tsx b/virtweb_frontend/src/routes/EditNetworkRoute.tsx index c016a7f..8f8a3ed 100644 --- a/virtweb_frontend/src/routes/EditNetworkRoute.tsx +++ b/virtweb_frontend/src/routes/EditNetworkRoute.tsx @@ -25,7 +25,7 @@ export function CreateNetworkRoute(): React.ReactElement { navigate(`/net/${res.uid}`); } catch (e) { console.error(e); - alert("Failed to create network!"); + alert(`Failed to create network!\n${e}`); } }; @@ -59,7 +59,7 @@ export function EditNetworkRoute(): React.ReactElement { navigate(NetworkURL(network!)); } catch (e) { console.error(e); - alert("Failed to update network!"); + alert(`Failed to update network!\n${e}`); } }; diff --git a/virtweb_frontend/src/routes/EditVMRoute.tsx b/virtweb_frontend/src/routes/EditVMRoute.tsx index 2f409a7..1c8d35f 100644 --- a/virtweb_frontend/src/routes/EditVMRoute.tsx +++ b/virtweb_frontend/src/routes/EditVMRoute.tsx @@ -23,7 +23,7 @@ export function CreateVMRoute(): React.ReactElement { navigate(v.ViewURL); } catch (e) { console.error(e); - alert("Failed to create VM!"); + alert(`Failed to create VM!\n${e}`); } }; diff --git a/virtweb_frontend/src/routes/HomeRoute.tsx b/virtweb_frontend/src/routes/HomeRoute.tsx new file mode 100644 index 0000000..71bbfdc --- /dev/null +++ b/virtweb_frontend/src/routes/HomeRoute.tsx @@ -0,0 +1,13 @@ +import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; + +export function HomeRoute(): React.ReactElement { + return ( + +

VirtWeb is an Open Source web ui for Libvirt.

+

+ You can use the menu on the left to access the different sections of the + application. +

+
+ ); +}