Skip to content

Commit

Permalink
include body in JSON parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
joksas committed Aug 22, 2024
1 parent c19c878 commit bd4977f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["RSS Blue", "Dovydas Joksas"]
name = "v4v"
version = "0.5.6"
version = "0.5.7"
edition = "2021"
description = "Value-for-value helper utilities for Podcasting 2.0"
license = "MIT OR Apache-2.0"
Expand Down
9 changes: 6 additions & 3 deletions src/alby/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ pub mod invoices {
payer_name: args.payer_name.clone(),
};

let body = serde_json::to_string(&request_body).map_err(RequestError::ResponseParse)?;
let body = serde_json::to_string(&request_body)
.map_err(|e| RequestError::Unexpected(e.to_string()))?;

let request_args = RequestArgs {
user_agent: args.user_agent,
Expand Down Expand Up @@ -182,7 +183,8 @@ pub mod payments {
custom_records: args.custom_records.clone(),
};

let body = serde_json::to_string(&request_body).map_err(RequestError::ResponseParse)?;
let body = serde_json::to_string(&request_body)
.map_err(|e| RequestError::Unexpected(e.to_string()))?;

let request_args = RequestArgs {
user_agent: args.user_agent,
Expand Down Expand Up @@ -256,7 +258,8 @@ pub mod payments {

let request_body = MultiKeysendRequest { keysends };

let body = serde_json::to_string(&request_body).map_err(RequestError::ResponseParse)?;
let body = serde_json::to_string(&request_body)
.map_err(|e| RequestError::Unexpected(e.to_string()))?;

let request_args = RequestArgs {
user_agent: args.user_agent,
Expand Down
23 changes: 11 additions & 12 deletions src/alby/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum RequestError {
/// Failed to read response body.
ResponseBodyRead(reqwest::Error),
/// Failed to parse response body.
ResponseParse(serde_json::Error),
ResponseParse(serde_json::Error, String),
/// Bad request (400).
BadRequest(ErrorResponse),
/// Internal server error (500).
Expand All @@ -48,7 +48,9 @@ impl fmt::Display for RequestError {
RequestError::ClientCreation(e) => write!(f, "Failed to create reqwest client: {}", e),
RequestError::RequestSend(e) => write!(f, "Failed to send request: {}", e),
RequestError::ResponseBodyRead(e) => write!(f, "Failed to read response body: {}", e),
RequestError::ResponseParse(e) => write!(f, "Failed to parse response body: {}", e),
RequestError::ResponseParse(e, body) => {
write!(f, "Failed to parse response body ({}): {}", body, e)
}
RequestError::BadRequest(e) => {
write!(f, "Bad request (400): {} (code: {})", e.message, e.code)
}
Expand All @@ -72,7 +74,7 @@ impl std::error::Error for RequestError {
RequestError::ClientCreation(e) => Some(e),
RequestError::RequestSend(e) => Some(e),
RequestError::ResponseBodyRead(e) => Some(e),
RequestError::ResponseParse(e) => Some(e),
RequestError::ResponseParse(e, _body) => Some(e),
RequestError::BadRequest(_)
| RequestError::InternalServerError(_)
| RequestError::UnexpectedStatus { .. } => None,
Expand All @@ -98,12 +100,6 @@ impl From<reqwest::Error> for RequestError {
}
}

impl From<serde_json::Error> for RequestError {
fn from(error: serde_json::Error) -> Self {
RequestError::ResponseParse(error)
}
}

/// Arguments for making a request.
pub struct RequestArgs<'a> {
/// User agent string.
Expand Down Expand Up @@ -139,15 +135,18 @@ pub async fn make_request<T: DeserializeOwned>(args: RequestArgs<'_>) -> Result<

let status = response.status();
let body = response.text().await?;
println!("{}", body);

match status.as_u16() {
200 => Ok(serde_json::from_str(&body)?),
200 => Ok(serde_json::from_str(&body).map_err(|e| RequestError::ResponseParse(e, body))?),
400 => {
let error_response: ErrorResponse = serde_json::from_str(&body)?;
let error_response: ErrorResponse = serde_json::from_str(&body)
.map_err(|e| RequestError::ResponseParse(e, body.clone()))?;
Err(RequestError::BadRequest(error_response))
}
500 => {
let error_response: ErrorResponse = serde_json::from_str(&body)?;
let error_response: ErrorResponse = serde_json::from_str(&body)
.map_err(|e| RequestError::ResponseParse(e, body.clone()))?;
Err(RequestError::InternalServerError(error_response))
}
_ => Err(RequestError::UnexpectedStatus { status, body }),
Expand Down

0 comments on commit bd4977f

Please sign in to comment.