Skip to content

Commit

Permalink
feat: Add path to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ticklepoke committed Aug 11, 2022
1 parent afd7aaf commit 1548ee4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/client/base/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl AsyncStdClient {

Box::pin(async move {
let bytes = send_inner(&client, request, &strategy).await?;
serde_json::from_slice(&bytes).map_err(StripeError::from)
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
serde_path_to_error::deserialize(json_deserializer).map_err(StripeError::from)
})
}
}
Expand Down Expand Up @@ -99,7 +100,8 @@ async fn send_inner(

if !status.is_success() {
tries += 1;
last_error = serde_json::from_slice(&bytes)
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
last_error = serde_path_to_error::deserialize(json_deserializer)
.map(|mut e: ErrorResponse| {
e.error.http_status = status.into();
StripeError::from(e.error)
Expand Down
10 changes: 7 additions & 3 deletions src/client/base/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ impl TokioClient {

Box::pin(async move {
let bytes = send_inner(&client, request, &strategy).await?;
serde_json::from_slice(&bytes).map_err(StripeError::from)
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
serde_path_to_error::deserialize(json_deserializer).map_err(StripeError::from)
})
}
}
Expand Down Expand Up @@ -127,7 +128,8 @@ async fn send_inner(

if !status.is_success() {
tries += 1;
last_error = serde_json::from_slice(&bytes)
let json_deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
last_error = serde_path_to_error::deserialize(json_deserializer)
.map(|mut e: ErrorResponse| {
e.error.http_status = status.into();
StripeError::from(e.error)
Expand Down Expand Up @@ -297,7 +299,9 @@ mod tests {
mock.assert_hits_async(1).await;

match res {
Err(StripeError::JSONSerialize(x)) => println!("{:?}", x),
Err(StripeError::JSONSerialize(err)) => {
println!("Error: {:?} Path: {:?}", err.inner(), err.path().to_string())
}
_ => panic!("Expected stripe error {:?}", res),
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/client/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,15 @@ impl Client {
) -> Response<T> {
let url = self.url(path);
let mut req = self.create_request(Method::Post, url);
req.set_body(match serde_qs::to_string(&form) {
Err(e) => return err(StripeError::QueryStringSerialize(e)),
Ok(body) => Body::from_string(body),
});

let mut params_buffer = Vec::new();
let qs_ser = &mut serde_qs::Serializer::new(&mut params_buffer);
serde_path_to_error::serialize(&form, qs_ser).map_err(StripeError::from)?;

let body = std::str::from_utf8(params_buffer.as_slice()).unwrap().to_string();

req.set_body(Body::from_string(body));

req.insert_header("content-type", "application/x-www-form-urlencoded");
self.client.execute::<T>(req, &self.strategy)
}
Expand All @@ -148,7 +153,13 @@ impl Client {

fn url_with_params<P: Serialize>(&self, path: &str, params: P) -> Result<Url, StripeError> {
let mut url = self.url(path);
let params = serde_qs::to_string(&params).map_err(StripeError::from)?;

let mut params_buffer = Vec::new();
let qs_ser = &mut serde_qs::Serializer::new(&mut params_buffer);
serde_path_to_error::serialize(&params, qs_ser).map_err(StripeError::from)?;

let params = std::str::from_utf8(params_buffer.as_slice()).unwrap().to_string();

url.set_query(Some(&params));
Ok(url)
}
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub enum StripeError {
#[error("error reported by stripe: {0}")]
Stripe(#[from] RequestError),
#[error("error serializing or deserializing a querystring: {0}")]
QueryStringSerialize(#[from] serde_qs::Error),
#[error("error serializing or deserializing a request: {0}")]
JSONSerialize(#[from] serde_json::Error),
QueryStringSerialize(#[from] serde_path_to_error::Error<serde_qs::Error>),
#[error("error serializing or deserializing a request")]
JSONSerialize(#[from] serde_path_to_error::Error<serde_json::Error>),
#[error("attempted to access an unsupported version of the api")]
UnsupportedVersion,
#[error("error communicating with stripe: {0}")]
Expand Down

0 comments on commit 1548ee4

Please sign in to comment.