From c0e36b3ff61c0a811582c4eefa42f74c6a5162e8 Mon Sep 17 00:00:00 2001 From: Aidan Hobson Sayers Date: Sun, 25 Oct 2020 10:00:50 +0000 Subject: [PATCH] Add impls for Error trait for Rust reqwest (#7462) * Add impls for Error trait for Rust reqwest * Update Rust samples --- .../resources/rust/reqwest/api_mod.mustache | 25 +++++++++++++++++++ .../reqwest/petstore-async/src/apis/mod.rs | 25 +++++++++++++++++++ .../rust/reqwest/petstore/src/apis/mod.rs | 25 +++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache index aba222c337d..5bf951361d9 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache @@ -1,5 +1,7 @@ use reqwest; use serde_json; +use std::error; +use std::fmt; #[derive(Debug, Clone)] pub struct ResponseContent { @@ -16,6 +18,29 @@ pub enum Error { ResponseError(ResponseContent), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let (module, e) = match self { + Error::Reqwest(e) => ("reqwest", e.to_string()), + Error::Serde(e) => ("serde", e.to_string()), + Error::Io(e) => ("IO", e.to_string()), + Error::ResponseError(e) => ("response", format!("status code {}", e.status)), + }; + write!(f, "error in {}: {}", module, e) + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(match self { + Error::Reqwest(e) => e, + Error::Serde(e) => e, + Error::Io(e) => e, + Error::ResponseError(_) => return None, + }) + } +} + impl From for Error { fn from(e: reqwest::Error) -> Self { Error::Reqwest(e) diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs index 087b76befa4..181305dc4fe 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs @@ -1,5 +1,7 @@ use reqwest; use serde_json; +use std::error; +use std::fmt; #[derive(Debug, Clone)] pub struct ResponseContent { @@ -16,6 +18,29 @@ pub enum Error { ResponseError(ResponseContent), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let (module, e) = match self { + Error::Reqwest(e) => ("reqwest", e.to_string()), + Error::Serde(e) => ("serde", e.to_string()), + Error::Io(e) => ("IO", e.to_string()), + Error::ResponseError(e) => ("response", format!("status code {}", e.status)), + }; + write!(f, "error in {}: {}", module, e) + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(match self { + Error::Reqwest(e) => e, + Error::Serde(e) => e, + Error::Io(e) => e, + Error::ResponseError(_) => return None, + }) + } +} + impl From for Error { fn from(e: reqwest::Error) -> Self { Error::Reqwest(e) diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs index 087b76befa4..181305dc4fe 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs @@ -1,5 +1,7 @@ use reqwest; use serde_json; +use std::error; +use std::fmt; #[derive(Debug, Clone)] pub struct ResponseContent { @@ -16,6 +18,29 @@ pub enum Error { ResponseError(ResponseContent), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let (module, e) = match self { + Error::Reqwest(e) => ("reqwest", e.to_string()), + Error::Serde(e) => ("serde", e.to_string()), + Error::Io(e) => ("IO", e.to_string()), + Error::ResponseError(e) => ("response", format!("status code {}", e.status)), + }; + write!(f, "error in {}: {}", module, e) + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(match self { + Error::Reqwest(e) => e, + Error::Serde(e) => e, + Error::Io(e) => e, + Error::ResponseError(_) => return None, + }) + } +} + impl From for Error { fn from(e: reqwest::Error) -> Self { Error::Reqwest(e)