Skip to content

Commit

Permalink
Stronger typing for http errors with Rust client. (OpenAPITools#5609).
Browse files Browse the repository at this point in the history
  • Loading branch information
bcourtine committed May 29, 2020
1 parent 92a350e commit 9e3565e
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,21 @@ impl {{{classname}}} for {{{classname}}}Client {

// send request
let req = req_builder.build()?;

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

{{/operation}}
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 @@ -48,8 +48,16 @@ impl DefaultApi for DefaultApiClient {

// send request
let req = req_builder.build()?;

Ok(client.execute(req)?.error_for_status()?.json()?)
let mut resp = client.execute(req)?;
if resp.status().is_success() {
Ok(resp.json()?)
} else {
let status = resp.status();
let content = resp.text()?;
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
@@ -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
8 changes: 8 additions & 0 deletions samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs
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
100 changes: 80 additions & 20 deletions samples/client/petstore/rust/reqwest/petstore/src/apis/pet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn delete_pet(&self, pet_id: i64, api_key: Option<&str>) -> Result<(), Error> {
Expand All @@ -83,9 +90,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn find_pets_by_status(&self, status: Vec<String>) -> Result<Vec<crate::models::Pet>, Error> {
Expand All @@ -105,8 +119,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn find_pets_by_tags(&self, tags: Vec<String>) -> Result<Vec<crate::models::Pet>, Error> {
Expand All @@ -126,8 +148,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn get_pet_by_id(&self, pet_id: i64) -> Result<crate::models::Pet, Error> {
Expand All @@ -151,8 +181,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn update_pet(&self, body: crate::models::Pet) -> Result<(), Error> {
Expand All @@ -172,9 +210,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn update_pet_with_form(&self, pet_id: i64, name: Option<&str>, status: Option<&str>) -> Result<(), Error> {
Expand All @@ -201,9 +246,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

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

fn upload_file(&self, pet_id: i64, additional_metadata: Option<&str>, file: Option<std::path::PathBuf>) -> Result<crate::models::ApiResponse, Error> {
Expand All @@ -230,8 +282,16 @@ impl PetApi for PetApiClient {

// send request
let req = req_builder.build()?;

Ok(client.execute(req)?.error_for_status()?.json()?)
let mut resp = client.execute(req)?;
if resp.status().is_success() {
Ok(resp.json()?)
} else {
let status = resp.status();
let content = resp.text()?;
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 @@ -51,9 +51,16 @@ impl StoreApi for StoreApiClient {

// send request
let req = req_builder.build()?;

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

fn get_inventory(&self, ) -> Result<::std::collections::HashMap<String, i32>, Error> {
Expand All @@ -77,8 +84,16 @@ impl StoreApi for StoreApiClient {

// send request
let req = req_builder.build()?;

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

fn get_order_by_id(&self, order_id: i64) -> Result<crate::models::Order, Error> {
Expand All @@ -94,8 +109,16 @@ impl StoreApi for StoreApiClient {

// send request
let req = req_builder.build()?;

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

fn place_order(&self, body: crate::models::Order) -> Result<crate::models::Order, Error> {
Expand All @@ -112,8 +135,16 @@ impl StoreApi for StoreApiClient {

// send request
let req = req_builder.build()?;

Ok(client.execute(req)?.error_for_status()?.json()?)
let mut resp = client.execute(req)?;
if resp.status().is_success() {
Ok(resp.json()?)
} else {
let status = resp.status();
let content = resp.text()?;
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

0 comments on commit 9e3565e

Please sign in to comment.