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/error.rs | 2 -- src/main.rs | 6 ++++++ src/requests/start.rs | 27 +++++++++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index 9aa8c08..d35991b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,7 +4,6 @@ pub enum CliError { Reqwest(reqwest::Error), Config(config::ConfigError), Serde(serde_json::Error), - // Cli(Box), } impl Debug for CliError { @@ -13,7 +12,6 @@ impl Debug for CliError { Self::Reqwest(err) => { err.fmt(f) }, Self::Config(err) => { err.fmt(f) }, Self::Serde(err) => { err.fmt(f) }, - // Self::Cli(err) => {err.fmt(f)}, } } } diff --git a/src/main.rs b/src/main.rs index d2f0c3a..e6ca3ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use config::SETTINGS; use error::CliError; use requests::{start::start, get::get}; use reqwest::header::{HeaderMap, HeaderValue}; +use serde::Deserialize; mod config; mod error; @@ -58,3 +59,8 @@ fn default_headers() -> Result { Ok(map) } + +#[derive(Debug, Deserialize)] +struct ErrorResponse { + error: String +} 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