forked from loafle/openapi-generator-original
Add impls for Error trait for Rust reqwest (#7462)
* Add impls for Error trait for Rust reqwest * Update Rust samples
This commit is contained in:
parent
e11a427cf5
commit
c0e36b3ff6
@ -1,5 +1,7 @@
|
|||||||
use reqwest;
|
use reqwest;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::error;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ResponseContent<T> {
|
pub struct ResponseContent<T> {
|
||||||
@ -16,6 +18,29 @@ pub enum Error<T> {
|
|||||||
ResponseError(ResponseContent<T>),
|
ResponseError(ResponseContent<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl <T> fmt::Display for Error<T> {
|
||||||
|
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 <T: fmt::Debug> error::Error for Error<T> {
|
||||||
|
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 <T> From<reqwest::Error> for Error<T> {
|
impl <T> From<reqwest::Error> for Error<T> {
|
||||||
fn from(e: reqwest::Error) -> Self {
|
fn from(e: reqwest::Error) -> Self {
|
||||||
Error::Reqwest(e)
|
Error::Reqwest(e)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use reqwest;
|
use reqwest;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::error;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ResponseContent<T> {
|
pub struct ResponseContent<T> {
|
||||||
@ -16,6 +18,29 @@ pub enum Error<T> {
|
|||||||
ResponseError(ResponseContent<T>),
|
ResponseError(ResponseContent<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl <T> fmt::Display for Error<T> {
|
||||||
|
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 <T: fmt::Debug> error::Error for Error<T> {
|
||||||
|
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 <T> From<reqwest::Error> for Error<T> {
|
impl <T> From<reqwest::Error> for Error<T> {
|
||||||
fn from(e: reqwest::Error) -> Self {
|
fn from(e: reqwest::Error) -> Self {
|
||||||
Error::Reqwest(e)
|
Error::Reqwest(e)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use reqwest;
|
use reqwest;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::error;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ResponseContent<T> {
|
pub struct ResponseContent<T> {
|
||||||
@ -16,6 +18,29 @@ pub enum Error<T> {
|
|||||||
ResponseError(ResponseContent<T>),
|
ResponseError(ResponseContent<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl <T> fmt::Display for Error<T> {
|
||||||
|
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 <T: fmt::Debug> error::Error for Error<T> {
|
||||||
|
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 <T> From<reqwest::Error> for Error<T> {
|
impl <T> From<reqwest::Error> for Error<T> {
|
||||||
fn from(e: reqwest::Error) -> Self {
|
fn from(e: reqwest::Error) -> Self {
|
||||||
Error::Reqwest(e)
|
Error::Reqwest(e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user