From 8f4ac0bd9ebfc2fff6397467c58fa7dfbbaca9d6 Mon Sep 17 00:00:00 2001 From: Sascha Eglau Date: Sat, 16 Mar 2024 00:39:32 +0800 Subject: [PATCH 1/2] feat: improve error handling for post requests --- src/api/mod.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 41ce845..6331bbe 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -63,13 +63,19 @@ impl APIClient { token: &str, name: &str, billing_email: &str, - ) -> Result { + ) -> anyhow::Result { let url = format!("{}/orgs", self.base_url); let mut body = HashMap::new(); body.insert("name", name); body.insert("billing_email", billing_email); let response = self.post(&url, token, &body)?; - response.json() + match response.status() { + StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?).with_context(|| "Failed to deserialize org")?), + StatusCode::UNAUTHORIZED => Err(anyhow!("Unauthorized, please login first")), + StatusCode::NOT_FOUND => Err(anyhow!("Org not found")), + StatusCode::BAD_REQUEST => Err(anyhow!("Bad request: {}", response.text()?)), + _ => Err(anyhow!("Failed to deploy service. API returned {} - {}", response.status(), response.text()?)) + } } pub fn get_environments( @@ -87,12 +93,18 @@ impl APIClient { token: &str, name: &str, org_name: &str - ) -> Result { + ) -> anyhow::Result { let url = format!("{}/orgs/{}/envs", self.base_url, org_name); let mut body = HashMap::new(); body.insert("name", name); let response = self.post(&url, token, &body)?; - response.json() + match response.status() { + StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?).with_context(|| "Failed to deserialize env")?), + StatusCode::UNAUTHORIZED => Err(anyhow!("Unauthorized, please login first")), + StatusCode::NOT_FOUND => Err(anyhow!("Org not found")), + StatusCode::BAD_REQUEST => Err(anyhow!("Bad request: {}", response.text()?)), + _ => Err(anyhow!("Failed to deploy service. API returned {} - {}", response.status(), response.text()?)) + } } pub fn deploy_service( @@ -208,8 +220,7 @@ impl APIClient { .header("Authorization", format!("Bearer {}", token)) .header("Content-Type", "application/json") .json(&body) - .send()? - .error_for_status(); + .send() } fn post_str(&self, url: &str, token: &str, body: String) -> Result { From 507d9125d4460d9a0a3462e37aa6b1b4cfd384be Mon Sep 17 00:00:00 2001 From: Sascha Eglau Date: Sat, 16 Mar 2024 00:40:16 +0800 Subject: [PATCH 2/2] refactor: do not get service if noconfirm is set --- src/commands/services.rs | 57 ++++++++++++---------------------------- 1 file changed, 17 insertions(+), 40 deletions(-) diff --git a/src/commands/services.rs b/src/commands/services.rs index 1c69c7b..de23467 100644 --- a/src/commands/services.rs +++ b/src/commands/services.rs @@ -92,24 +92,24 @@ impl Deploy { )); } - let response = base.api_client().get_service( - token, - &org_name, - &manifest.environment, - &manifest.service.name, - ); - - let existing_svc = match response? { - Some(svc) => svc, - None => return self.create_new_service(base, token, &org_name), - }; - if let Some(false) = self.no_confirm { - if existing_svc == manifest.service { - println!("no changes detected"); - return Ok(()); - } - let existing_svc_yaml = serde_yaml::to_string(&existing_svc)?; + let response = base.api_client().get_service( + token, + &org_name, + &manifest.environment, + &manifest.service.name, + ); + + let existing_svc_yaml = match response? { + Some(svc) => { + if svc == manifest.service { + println!("no changes detected"); + return Ok(()); + } + serde_yaml::to_string(&svc)? + } + None => "".to_string(), + }; let new_svc_yaml = serde_yaml::to_string(&manifest.service)?; self.render_diff(existing_svc_yaml, new_svc_yaml)?; let selection = self.user_confirmation(); @@ -129,29 +129,6 @@ impl Deploy { Ok(()) } - fn create_new_service(&self, base: &CommandBase, token: &str, org_name: &str) -> Result<()> { - let manifest = self.read_manifest()?; - if let Some(false) = self.no_confirm { - let new_svc_yaml = serde_yaml::to_string(&manifest.service)?; - self.render_diff("".to_string(), new_svc_yaml)?; - - let selection = self.user_confirmation(); - if selection == 0 { - println!("Cancelling..."); - return Ok(()); - } - } - - let result = base.api_client().deploy_service( - token, - org_name, - &manifest.environment, - manifest.service, - )?; - println!("Service {} deployed", result.name); - Ok(()) - } - fn read_manifest(&self) -> Result { let file_path = self.manifest.clone(); let mut file_content = String::new();