diff options
author | fx <[email protected]> | 2023-10-19 09:39:28 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-19 09:39:28 +0200 |
commit | eb0a092fc53964e02a09da2d92a1f8a3042a1360 (patch) | |
tree | 4f5bdf69e4973dad81506a60760417c1132e6e75 | |
parent | 527d2c7abc1f83f76342fa21a8d4f417582d53a6 (diff) | |
download | webol-cli-eb0a092fc53964e02a09da2d92a1f8a3042a1360.tar webol-cli-eb0a092fc53964e02a09da2d92a1f8a3042a1360.tar.gz webol-cli-eb0a092fc53964e02a09da2d92a1f8a3042a1360.zip |
added error message on server != 200
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/requests/start.rs | 27 |
3 files changed, 27 insertions, 8 deletions
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 { | |||
4 | Reqwest(reqwest::Error), | 4 | Reqwest(reqwest::Error), |
5 | Config(config::ConfigError), | 5 | Config(config::ConfigError), |
6 | Serde(serde_json::Error), | 6 | Serde(serde_json::Error), |
7 | // Cli(Box<dyn std::error::Error>), | ||
8 | } | 7 | } |
9 | 8 | ||
10 | impl Debug for CliError { | 9 | impl Debug for CliError { |
@@ -13,7 +12,6 @@ impl Debug for CliError { | |||
13 | Self::Reqwest(err) => { err.fmt(f) }, | 12 | Self::Reqwest(err) => { err.fmt(f) }, |
14 | Self::Config(err) => { err.fmt(f) }, | 13 | Self::Config(err) => { err.fmt(f) }, |
15 | Self::Serde(err) => { err.fmt(f) }, | 14 | Self::Serde(err) => { err.fmt(f) }, |
16 | // Self::Cli(err) => {err.fmt(f)}, | ||
17 | } | 15 | } |
18 | } | 16 | } |
19 | } | 17 | } |
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; | |||
3 | use error::CliError; | 3 | use error::CliError; |
4 | use requests::{start::start, get::get}; | 4 | use requests::{start::start, get::get}; |
5 | use reqwest::header::{HeaderMap, HeaderValue}; | 5 | use reqwest::header::{HeaderMap, HeaderValue}; |
6 | use serde::Deserialize; | ||
6 | 7 | ||
7 | mod config; | 8 | mod config; |
8 | mod error; | 9 | mod error; |
@@ -58,3 +59,8 @@ fn default_headers() -> Result<HeaderMap, CliError> { | |||
58 | Ok(map) | 59 | Ok(map) |
59 | 60 | ||
60 | } | 61 | } |
62 | |||
63 | #[derive(Debug, Deserialize)] | ||
64 | struct ErrorResponse { | ||
65 | error: String | ||
66 | } | ||
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 @@ | |||
1 | use reqwest::StatusCode; | ||
1 | use serde::Deserialize; | 2 | use serde::Deserialize; |
2 | 3 | ||
3 | use crate::{config::SETTINGS, error::CliError, default_headers}; | 4 | use crate::{config::SETTINGS, error::CliError, default_headers, ErrorResponse}; |
4 | 5 | ||
5 | pub fn start(id: String) -> Result<(), CliError> { | 6 | pub fn start(id: String) -> Result<(), CliError> { |
6 | let res = reqwest::blocking::Client::new() | 7 | let res = reqwest::blocking::Client::new() |
@@ -15,13 +16,27 @@ pub fn start(id: String) -> Result<(), CliError> { | |||
15 | format!(r#"{{"id": "{}"}}"#, id) | 16 | format!(r#"{{"id": "{}"}}"#, id) |
16 | ) | 17 | ) |
17 | .send() | 18 | .send() |
18 | .map_err(CliError::Reqwest)? | 19 | .map_err(CliError::Reqwest)?; |
19 | .text(); | ||
20 | 20 | ||
21 | let res = serde_json::from_str::<StartResponse>(&res.map_err(CliError::Reqwest)?).map_err(CliError::Serde)?; | 21 | match res.status() { |
22 | StatusCode::OK => { | ||
23 | let body = serde_json::from_str::<StartResponse>( | ||
24 | &res.text().map_err(CliError::Reqwest)? | ||
25 | ) | ||
26 | .map_err(CliError::Serde)?; | ||
27 | |||
28 | if body.boot { | ||
29 | println!("successfully started {}", body.id); | ||
30 | } | ||
31 | }, | ||
32 | _ => { | ||
33 | let body = serde_json::from_str::<ErrorResponse>( | ||
34 | &res.text().map_err(CliError::Reqwest)? | ||
35 | ) | ||
36 | .map_err(CliError::Serde)?; | ||
22 | 37 | ||
23 | if res.boot { | 38 | println!("got error: {}", body.error); |
24 | println!("successfully started {}", res.id); | 39 | } |
25 | } | 40 | } |
26 | 41 | ||
27 | Ok(()) | 42 | Ok(()) |