From 384804e562d9bd160ab7936035d5699943bc3e7f Mon Sep 17 00:00:00 2001 From: Manuel Sopena Ballesteros Date: Tue, 2 Jul 2024 14:32:23 +0200 Subject: [PATCH] fix: IMS job creation returns CSM error msg is request failt --- src/ims/job/http_client.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ims/job/http_client.rs b/src/ims/job/http_client.rs index 61f3401..17c3064 100644 --- a/src/ims/job/http_client.rs +++ b/src/ims/job/http_client.rs @@ -1,5 +1,7 @@ use serde_json::Value; +use crate::error::Error; + use super::{ r#struct::{JobPostRequest, SshContainer}, utils::wait_ims_job_to_finish, @@ -14,7 +16,7 @@ pub async fn post_customize( image_root_archive_name: &str, artifact_id: &str, public_key_id: &str, -) -> Result { +) -> Result { let ssh_container = SshContainer { name: "jail".to_string(), jail: true, @@ -54,15 +56,28 @@ pub async fn post_customize( let api_url = shasta_base_url.to_owned() + "/ims/v3/jobs"; - client + let response = client .post(api_url) .bearer_auth(shasta_token) .json(&ims_job) .send() - .await? - .error_for_status()? - .json() .await + .map_err(|error| Error::NetError(error))?; + + if response.status().is_success() { + // Make sure we return a vec if user requesting a single value + response + .json() + .await + .map_err(|error| Error::NetError(error)) + } else { + let payload = response + .json::() + .await + .map_err(|error| Error::NetError(error))?; + + Err(Error::CsmError(payload)) + } } /// Creates an IMS job, this method is asynchronous, meaning, it will returns when the server