submissions-api: use goofy function to make errors include more information

This commit is contained in:
Quaternions 2024-12-17 19:31:45 -08:00
parent 299f994f32
commit e7234a614d
3 changed files with 62 additions and 21 deletions

View File

@ -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<ScriptIDResponse,Error>{
@ -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<ScriptPolicyResponse,Error>{
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<ScriptPolicyIDResponse,Error>{
@ -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(())
}

View File

@ -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<reqwest::Response,ResponseError>{
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,
}))
}
}

View File

@ -24,12 +24,10 @@ fn interpret_get_script_policy_response(reponse:Result<ScriptPolicyResponse,subm
match reponse{
Ok(script_policy)=>Ok(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);