From eb0a092fc53964e02a09da2d92a1f8a3042a1360 Mon Sep 17 00:00:00 2001 From: fx Date: Thu, 19 Oct 2023 09:39:28 +0200 Subject: added error message on server != 200 --- src/requests/start.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src/requests/start.rs') diff --git a/src/requests/start.rs b/src/requests/start.rs index 1a8cb6c..30f65b9 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -1,6 +1,7 @@ +use reqwest::StatusCode; use serde::Deserialize; -use crate::{config::SETTINGS, error::CliError, default_headers}; +use crate::{config::SETTINGS, error::CliError, default_headers, ErrorResponse}; pub fn start(id: String) -> Result<(), CliError> { let res = reqwest::blocking::Client::new() @@ -15,13 +16,27 @@ pub fn start(id: String) -> Result<(), CliError> { format!(r#"{{"id": "{}"}}"#, id) ) .send() - .map_err(CliError::Reqwest)? - .text(); + .map_err(CliError::Reqwest)?; - let res = serde_json::from_str::(&res.map_err(CliError::Reqwest)?).map_err(CliError::Serde)?; + match res.status() { + StatusCode::OK => { + let body = serde_json::from_str::( + &res.text().map_err(CliError::Reqwest)? + ) + .map_err(CliError::Serde)?; + + if body.boot { + println!("successfully started {}", body.id); + } + }, + _ => { + let body = serde_json::from_str::( + &res.text().map_err(CliError::Reqwest)? + ) + .map_err(CliError::Serde)?; - if res.boot { - println!("successfully started {}", res.id); + println!("got error: {}", body.error); + } } Ok(()) -- cgit v1.2.3