Skip to content

Commit c0e36b3

Browse files
authored
Add impls for Error trait for Rust reqwest (#7462)
* Add impls for Error trait for Rust reqwest * Update Rust samples
1 parent e11a427 commit c0e36b3

File tree

3 files changed

+75
-0
lines changed
  • modules/openapi-generator/src/main/resources/rust/reqwest
  • samples/client/petstore/rust/reqwest

3 files changed

+75
-0
lines changed

modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use reqwest;
22
use serde_json;
3+
use std::error;
4+
use std::fmt;
35

46
#[derive(Debug, Clone)]
57
pub struct ResponseContent<T> {
@@ -16,6 +18,29 @@ pub enum Error<T> {
1618
ResponseError(ResponseContent<T>),
1719
}
1820

21+
impl <T> fmt::Display for Error<T> {
22+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23+
let (module, e) = match self {
24+
Error::Reqwest(e) => ("reqwest", e.to_string()),
25+
Error::Serde(e) => ("serde", e.to_string()),
26+
Error::Io(e) => ("IO", e.to_string()),
27+
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
28+
};
29+
write!(f, "error in {}: {}", module, e)
30+
}
31+
}
32+
33+
impl <T: fmt::Debug> error::Error for Error<T> {
34+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
35+
Some(match self {
36+
Error::Reqwest(e) => e,
37+
Error::Serde(e) => e,
38+
Error::Io(e) => e,
39+
Error::ResponseError(_) => return None,
40+
})
41+
}
42+
}
43+
1944
impl <T> From<reqwest::Error> for Error<T> {
2045
fn from(e: reqwest::Error) -> Self {
2146
Error::Reqwest(e)

samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use reqwest;
22
use serde_json;
3+
use std::error;
4+
use std::fmt;
35

46
#[derive(Debug, Clone)]
57
pub struct ResponseContent<T> {
@@ -16,6 +18,29 @@ pub enum Error<T> {
1618
ResponseError(ResponseContent<T>),
1719
}
1820

21+
impl <T> fmt::Display for Error<T> {
22+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23+
let (module, e) = match self {
24+
Error::Reqwest(e) => ("reqwest", e.to_string()),
25+
Error::Serde(e) => ("serde", e.to_string()),
26+
Error::Io(e) => ("IO", e.to_string()),
27+
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
28+
};
29+
write!(f, "error in {}: {}", module, e)
30+
}
31+
}
32+
33+
impl <T: fmt::Debug> error::Error for Error<T> {
34+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
35+
Some(match self {
36+
Error::Reqwest(e) => e,
37+
Error::Serde(e) => e,
38+
Error::Io(e) => e,
39+
Error::ResponseError(_) => return None,
40+
})
41+
}
42+
}
43+
1944
impl <T> From<reqwest::Error> for Error<T> {
2045
fn from(e: reqwest::Error) -> Self {
2146
Error::Reqwest(e)

samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use reqwest;
22
use serde_json;
3+
use std::error;
4+
use std::fmt;
35

46
#[derive(Debug, Clone)]
57
pub struct ResponseContent<T> {
@@ -16,6 +18,29 @@ pub enum Error<T> {
1618
ResponseError(ResponseContent<T>),
1719
}
1820

21+
impl <T> fmt::Display for Error<T> {
22+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23+
let (module, e) = match self {
24+
Error::Reqwest(e) => ("reqwest", e.to_string()),
25+
Error::Serde(e) => ("serde", e.to_string()),
26+
Error::Io(e) => ("IO", e.to_string()),
27+
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
28+
};
29+
write!(f, "error in {}: {}", module, e)
30+
}
31+
}
32+
33+
impl <T: fmt::Debug> error::Error for Error<T> {
34+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
35+
Some(match self {
36+
Error::Reqwest(e) => e,
37+
Error::Serde(e) => e,
38+
Error::Io(e) => e,
39+
Error::ResponseError(_) => return None,
40+
})
41+
}
42+
}
43+
1944
impl <T> From<reqwest::Error> for Error<T> {
2045
fn from(e: reqwest::Error) -> Self {
2146
Error::Reqwest(e)

0 commit comments

Comments
 (0)