improved error handling
This commit is contained in:
parent
e61739a8fb
commit
21ac804759
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "paypal-rs"
|
||||
version = "0.2.0-alpha.3"
|
||||
version = "0.2.0-alpha.4"
|
||||
authors = ["Edgar <git@edgarluque.com>"]
|
||||
description = "A library that wraps the paypal api asynchronously."
|
||||
repository = "https://github.com/edg-l/paypal-rs/"
|
||||
|
|
|
@ -42,6 +42,38 @@ pub enum ResponseError {
|
|||
HttpError(reqwest::Error)
|
||||
}
|
||||
|
||||
impl fmt::Display for ResponseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ResponseError::ApiError(e) => write!(f, "{}", e),
|
||||
ResponseError::HttpError(e) => write!(f, "{}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for ResponseError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
match self {
|
||||
ResponseError::ApiError(e) => Some(e),
|
||||
ResponseError::HttpError(e) => Some(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implemented so we can use ? directly on it.
|
||||
impl From<PaypalError> for ResponseError {
|
||||
fn from(e: PaypalError) -> Self {
|
||||
ResponseError::ApiError(e)
|
||||
}
|
||||
}
|
||||
|
||||
// Implemented so we can use ? directly on it.
|
||||
impl From<reqwest::Error> for ResponseError {
|
||||
fn from(e: reqwest::Error) -> Self {
|
||||
ResponseError::HttpError(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// When a currency is invalid.
|
||||
#[derive(Debug)]
|
||||
pub struct InvalidCurrencyError(pub String);
|
||||
|
|
|
@ -737,13 +737,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let x = res.json::<HashMap<String, String>>().await.map_err(ResponseError::HttpError)?;
|
||||
let x = res.json::<HashMap<String, String>>().await?;
|
||||
Ok(x.get("invoice_number").expect("to have a invoice number").clone())
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -762,13 +762,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.json(&invoice).send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.json(&invoice).send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let x = res.json::<Invoice>().await.map_err(ResponseError::HttpError)?;
|
||||
let x = res.json::<Invoice>().await?;
|
||||
Ok(x)
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -786,13 +786,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let x = res.json::<Invoice>().await.map_err(ResponseError::HttpError)?;
|
||||
let x = res.json::<Invoice>().await?;
|
||||
Ok(x)
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -819,13 +819,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let x = res.json::<InvoiceList>().await.map_err(ResponseError::HttpError)?;
|
||||
let x = res.json::<InvoiceList>().await?;
|
||||
Ok(x)
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -843,12 +843,12 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -876,12 +876,12 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -900,12 +900,12 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.json(&reason).send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.json(&reason).send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -930,13 +930,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.json(¶ms).send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.json(¶ms).send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let b = res.bytes().await.map_err(ResponseError::HttpError)?;
|
||||
let b = res.bytes().await?;
|
||||
Ok(b)
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -955,13 +955,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = build.json(&payload).send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = build.json(&payload).send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let x = res.json::<HashMap<String, String>>().await.map_err(ResponseError::HttpError)?;
|
||||
let x = res.json::<HashMap<String, String>>().await?;
|
||||
Ok(x.get("payment_id").unwrap().to_owned())
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(res.json::<PaypalError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -752,13 +752,13 @@ impl super::Client {
|
|||
)
|
||||
.await
|
||||
};
|
||||
let res = builder.json(&order).send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = builder.json(&order).send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let order = res.json::<Order>().await.map_err(ResponseError::HttpError)?;
|
||||
let order = res.json::<Order>().await?;
|
||||
Ok(order)
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await?))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -782,13 +782,13 @@ impl super::Client {
|
|||
)
|
||||
.await;
|
||||
|
||||
let res = builder.send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = builder.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let order = res.json::<Order>().await.expect("error serializing json response");
|
||||
let order = res.json::<Order>().await?;
|
||||
Ok(order)
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await?))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -813,7 +813,7 @@ impl super::Client {
|
|||
let mut units_json = String::new();
|
||||
|
||||
for (i, unit) in p_units.iter().enumerate() {
|
||||
let unit_str = serde_json::to_string(&unit).expect("error deserializing PurchaseUnit json");
|
||||
let unit_str = serde_json::to_string(&unit).expect("error serializing purchase unit");
|
||||
let mut unit_json = format!(
|
||||
r#"
|
||||
{{
|
||||
|
@ -872,12 +872,12 @@ impl super::Client {
|
|||
.await
|
||||
};
|
||||
|
||||
let res = builder.body(final_json.clone()).send().await.map_err(ResponseError::HttpError)?;
|
||||
let res = builder.body(final_json.clone()).send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await.map_err(ResponseError::HttpError)?))
|
||||
Err(ResponseError::ApiError(res.json::<PaypalError>().await?))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue