Improve errors reporting

This commit is contained in:
Pierre HUBERT 2023-12-12 01:32:18 +01:00
parent 2a52b5c035
commit bc15846b67
8 changed files with 68 additions and 16 deletions

View File

@ -14,11 +14,21 @@ pub async fn create(client: LibVirtReq, req: web::Json<NetworkInfo>) -> 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<NetworkID>) -> HttpResult
/// Start a network
pub async fn start(client: LibVirtReq, id: web::Path<NetworkID>) -> 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

View File

@ -24,10 +24,20 @@ pub async fn create(client: LibVirtReq, req: web::Json<VMInfo>) -> 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<SingleVMUUidReq>) -> 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}"))
}
})
}

View File

@ -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 ? (
<Route path="*" element={<BaseAuthenticatedPage />}>
<Route path="" element={<HomeRoute />} />
<Route path="iso" element={<IsoFilesRoute />} />
<Route path="vms" element={<VMListRoute />} />

View File

@ -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}`);
}
}

View File

@ -49,7 +49,10 @@ export function AlertDialogProvider(p: PropsWithChildren): React.ReactElement {
>
{title && <DialogTitle id="alert-dialog-title">{title}</DialogTitle>}
<DialogContent>
<DialogContentText id="alert-dialog-description">
<DialogContentText
id="alert-dialog-description"
style={{ whiteSpace: "pre" }}
>
{message}
</DialogContentText>
</DialogContent>

View File

@ -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}`);
}
};

View File

@ -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}`);
}
};

View File

@ -0,0 +1,13 @@
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
export function HomeRoute(): React.ReactElement {
return (
<VirtWebRouteContainer label="VirtWeb">
<p>VirtWeb is an Open Source web ui for Libvirt.</p>
<p>
You can use the menu on the left to access the different sections of the
application.
</p>
</VirtWebRouteContainer>
);
}