diff --git a/Cargo.toml b/Cargo.toml index 39cb960..a5933db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/alby/api.rs b/src/alby/api.rs index 7f81ace..8b41003 100644 --- a/src/alby/api.rs +++ b/src/alby/api.rs @@ -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, @@ -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, @@ -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, diff --git a/src/alby/helpers.rs b/src/alby/helpers.rs index 9764592..100cfff 100644 --- a/src/alby/helpers.rs +++ b/src/alby/helpers.rs @@ -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). @@ -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) } @@ -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, @@ -98,12 +100,6 @@ impl From for RequestError { } } -impl From for RequestError { - fn from(error: serde_json::Error) -> Self { - RequestError::ResponseParse(error) - } -} - /// Arguments for making a request. pub struct RequestArgs<'a> { /// User agent string. @@ -139,15 +135,18 @@ pub async fn make_request(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 }),