diff --git a/validation/api/src/internal.rs b/validation/api/src/internal.rs index 67b2a46..daaf37c 100644 --- a/validation/api/src/internal.rs +++ b/validation/api/src/internal.rs @@ -90,8 +90,9 @@ macro_rules! action{ let url_raw=format!(concat!("{}/submissions/{}/status/",$action),self.0.base_url,config.0); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; - self.0.post_empty_body(url).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)?; + crate::response_ok( + self.0.post_empty_body(url).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)?; Ok(()) } @@ -105,8 +106,9 @@ impl Context{ let url_raw=format!("{}/scripts/{}",self.0.base_url,config.ScriptID.0); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; - self.0.get(url).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)? + crate::response_ok( + self.0.get(url).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn create_script<'a>(&self,config:CreateScriptRequest<'a>)->Result{ @@ -115,16 +117,18 @@ impl Context{ let body=serde_json::to_string(&config).map_err(Error::JSON)?; - self.0.post(url,body).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)? + crate::response_ok( + self.0.post(url,body).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn get_script_policy_from_hash<'a>(&self,config:ScriptPolicyHashRequest<'a>)->Result{ let url_raw=format!("{}/script-policy/hash/{}",self.0.base_url,config.hash); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; - self.0.get(url).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)? + crate::response_ok( + self.0.get(url).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn create_script_policy(&self,config:CreateScriptPolicyRequest)->Result{ @@ -133,8 +137,9 @@ impl Context{ let body=serde_json::to_string(&config).map_err(Error::JSON)?; - self.0.post(url,body).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)? + crate::response_ok( + self.0.post(url,body).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn update_submission_model(&self,config:UpdateSubmissionModelRequest)->Result<(),Error>{ @@ -147,8 +152,9 @@ impl Context{ .append_pair("ModelVersion",config.ModelVersion.to_string().as_str()); } - self.0.post_empty_body(url).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)?; + crate::response_ok( + self.0.post_empty_body(url).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)?; Ok(()) } @@ -160,9 +166,9 @@ impl Context{ url.query_pairs_mut() .append_pair("TargetAssetID",target_asset_id.to_string().as_str()); } - - self.0.post_empty_body(url).await.map_err(Error::Reqwest)? - .error_for_status().map_err(Error::Reqwest)?; + crate::response_ok( + self.0.post_empty_body(url).await.map_err(Error::Reqwest)? + ).await.map_err(Error::Response)?; Ok(()) } diff --git a/validation/api/src/lib.rs b/validation/api/src/lib.rs index 0189263..7794f6c 100644 --- a/validation/api/src/lib.rs +++ b/validation/api/src/lib.rs @@ -10,6 +10,7 @@ pub type ReqwestError=reqwest::Error; pub enum Error{ ParseError(url::ParseError), Reqwest(reqwest::Error), + Response(ResponseError), JSON(serde_json::Error), } impl std::fmt::Display for Error{ @@ -18,3 +19,39 @@ impl std::fmt::Display for Error{ } } impl std::error::Error for Error{} + +#[allow(dead_code)] +#[derive(Debug)] +pub struct StatusCodeWithUrlAndBody{ + pub status_code:reqwest::StatusCode, + pub url:url::Url, + pub body:String, +} + +#[derive(Debug)] +pub enum ResponseError{ + Reqwest(reqwest::Error), + StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody), +} +impl std::fmt::Display for ResponseError{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{self:?}") + } +} +impl std::error::Error for ResponseError{} +// lazy function to draw out meaningful info from http response on failure +pub async fn response_ok(response:reqwest::Response)->Result{ + let status_code=response.status(); + if status_code.is_success(){ + Ok(response) + }else{ + let url=response.url().to_owned(); + let bytes=response.bytes().await.map_err(ResponseError::Reqwest)?; + let body=String::from_utf8_lossy(&bytes).to_string(); + Err(ResponseError::StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody{ + status_code, + url, + body, + })) + } +} diff --git a/validation/src/validator.rs b/validation/src/validator.rs index 0c0b243..8193298 100644 --- a/validation/src/validator.rs +++ b/validation/src/validator.rs @@ -24,12 +24,10 @@ fn interpret_get_script_policy_response(reponse:ResultOk(Some(script_policy)), Err(e)=>{ - if let submissions_api::Error::Reqwest(error)=&e{ - if let Some(status_code)=error.status(){ - if status_code.as_u16()==404{ - // wew we figured out that the resource does not exist - return Ok(None); - } + if let submissions_api::Error::Response(submissions_api::ResponseError::StatusCodeWithUrlAndBody(s))=&e{ + if s.status_code.as_u16()==404{ + // wew we figured out that the resource does not exist + return Ok(None); } } return Err(e);