Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust][Client][Reqwest] Better http error handling #6481

Merged
Prev Previous commit
Next Next commit
Merge branch 'master' into 5609-rust-client-better-error-typing (supp…
…ort async with reqwest 0.10+).
  • Loading branch information
bcourtine committed May 29, 2020
commit 948d653806c6593cca5a96c59b6f53efdca255ee
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,17 @@ impl {{{classname}}} for {{{classname}}}Client {
{{/hasBodyParam}}

let req = req_builder.build()?;
let mut resp = client.execute(req)?;
let mut resp = client.execute(req){{#supportAsync}}.await{{/supportAsync}}?;
if resp.status().is_success() {
{{^returnType}}
Ok(())
{{/returnType}}
{{#returnType}}
Ok(resp.json()?)
Ok(resp.json{{#supportAsync}}::<{{{.}}}>().await{{/supportAsync}}{{^supportAsync}}(){{/supportAsync}}?)
{{/returnType}}
} else {
let status = resp.status();
let content = resp.text()?;
let content = resp.text(){{#supportAsync}}.await{{/supportAsync}}?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use reqwest;
use serde_json;

#[derive(Debug, Clone)]
pub struct ResponseErrorContent {
pub status: reqwest::StatusCode,
pub content: String,
pub entity: Option<serde_json::Value>,
}

#[derive(Debug)]
pub enum Error {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseErrorContent),
}

impl From<reqwest::Error> for Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ use super::{Error, configuration};
req_builder = req_builder.json(&body);

let req = req_builder.build()?;
client.execute(req).await?.error_for_status()?;
Ok(())
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(())
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn delete_pet(configuration: &configuration::Configuration, pet_id: i64, api_key: Option<&str>) -> Result<(), Error> {
Expand All @@ -54,8 +62,16 @@ use super::{Error, configuration};
};

let req = req_builder.build()?;
client.execute(req).await?.error_for_status()?;
Ok(())
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(())
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn find_pets_by_status(configuration: &configuration::Configuration, status: Vec<String>) -> Result<Vec<crate::models::Pet>, Error> {
Expand All @@ -73,7 +89,16 @@ use super::{Error, configuration};
};

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<Vec<crate::models::Pet>>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<Vec<crate::models::Pet>>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn find_pets_by_tags(configuration: &configuration::Configuration, tags: Vec<String>) -> Result<Vec<crate::models::Pet>, Error> {
Expand All @@ -91,7 +116,16 @@ use super::{Error, configuration};
};

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<Vec<crate::models::Pet>>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<Vec<crate::models::Pet>>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn get_pet_by_id(configuration: &configuration::Configuration, pet_id: i64) -> Result<crate::models::Pet, Error> {
Expand All @@ -113,7 +147,16 @@ use super::{Error, configuration};
};

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<crate::models::Pet>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<crate::models::Pet>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn update_pet(configuration: &configuration::Configuration, body: crate::models::Pet) -> Result<(), Error> {
Expand All @@ -131,8 +174,16 @@ use super::{Error, configuration};
req_builder = req_builder.json(&body);

let req = req_builder.build()?;
client.execute(req).await?.error_for_status()?;
Ok(())
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(())
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn update_pet_with_form(configuration: &configuration::Configuration, pet_id: i64, name: Option<&str>, status: Option<&str>) -> Result<(), Error> {
Expand All @@ -157,8 +208,16 @@ use super::{Error, configuration};
req_builder = req_builder.form(&form_params);

let req = req_builder.build()?;
client.execute(req).await?.error_for_status()?;
Ok(())
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(())
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn upload_file(configuration: &configuration::Configuration, pet_id: i64, additional_metadata: Option<&str>, file: Option<std::path::PathBuf>) -> Result<crate::models::ApiResponse, Error> {
Expand All @@ -181,6 +240,15 @@ use super::{Error, configuration};
req_builder = req_builder.multipart(form);

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<crate::models::ApiResponse>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<crate::models::ApiResponse>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ use super::{Error, configuration};
}

let req = req_builder.build()?;
client.execute(req).await?.error_for_status()?;
Ok(())
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(())
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn get_inventory(configuration: &configuration::Configuration, ) -> Result<::std::collections::HashMap<String, i32>, Error> {
Expand All @@ -52,7 +60,16 @@ use super::{Error, configuration};
};

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<::std::collections::HashMap<String, i32>>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<::std::collections::HashMap<String, i32>>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn get_order_by_id(configuration: &configuration::Configuration, order_id: i64) -> Result<crate::models::Order, Error> {
Expand All @@ -66,7 +83,16 @@ use super::{Error, configuration};
}

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<crate::models::Order>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<crate::models::Order>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

pub async fn place_order(configuration: &configuration::Configuration, body: crate::models::Order) -> Result<crate::models::Order, Error> {
Expand All @@ -81,6 +107,15 @@ use super::{Error, configuration};
req_builder = req_builder.json(&body);

let req = req_builder.build()?;
Ok(client.execute(req).await?.error_for_status()?.json::<crate::models::Order>().await?)
let mut resp = client.execute(req).await?;
if resp.status().is_success() {
Ok(resp.json::<crate::models::Order>().await?)
} else {
let status = resp.status();
let content = resp.text().await?;
let entity: Option<serde_json::Value> = serde_json::from_str(&content).ok();
let error = crate::apis::ResponseErrorContent { status, content, entity };
Err(Error::ResponseError(error))
}
}

Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.