Improve errors reporting
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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}"))
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user