Skip to content

Commit

Permalink
Deserialize server err response from masp indexer client
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jul 3, 2024
1 parent 0a439b5 commit 300c51a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions crates/sdk/src/masp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,25 @@ impl IndexerMaspClient {
fn endpoint(&self, which: &str) -> String {
format!("{}{which}", self.indexer_api)
}

async fn get_server_error(
response: reqwest::Response,
) -> Result<String, Error> {
use serde::Deserialize;

#[derive(Deserialize)]
struct Response {
message: String,
}

let payload: Response = response.json().await.map_err(|err| {
Error::Other(format!(
"Could not deserialize server's error JSON response: {err}"
))
})?;

Ok(payload.message)
}
}

#[cfg(not(target_family = "wasm"))]
Expand Down Expand Up @@ -315,6 +334,12 @@ impl MaspClient for IndexerMaspClient {
"Failed to fetch latest block height: {err}"
))
})?;
if !response.status().is_success() {
let err = Self::get_server_error(response).await?;
return Err(Error::Other(format!(
"Failed to fetch last block height: {err}"
)));
}
let payload: Response = response.json().await.map_err(|err| {
Error::Other(format!(
"Could not deserialize latest block height JSON response: \
Expand Down Expand Up @@ -386,6 +411,13 @@ impl MaspClient for IndexerMaspClient {
{from_height}-{to_height}: {err}"
))
})?;
if !response.status().is_success() {
let err = Self::get_server_error(response).await?;
return Err(Error::Other(format!(
"Failed to fetch transactions in the range \
{from_height}-{to_height}: {err}"
)));
}
let payload: TxResponse =
response.json().await.map_err(|err| {
Error::Other(format!(
Expand Down Expand Up @@ -483,6 +515,12 @@ impl MaspClient for IndexerMaspClient {
"Failed to fetch commitment tree at height {height}: {err}"
))
})?;
if !response.status().is_success() {
let err = Self::get_server_error(response).await?;
return Err(Error::Other(format!(
"Failed to fetch commitment tree at height {height}: {err}"
)));
}
let payload: Response = response.json().await.map_err(|err| {
Error::Other(format!(
"Could not deserialize the commitment tree JSON response at \
Expand Down Expand Up @@ -531,6 +569,12 @@ impl MaspClient for IndexerMaspClient {
"Failed to fetch notes map at height {height}: {err}"
))
})?;
if !response.status().is_success() {
let err = Self::get_server_error(response).await?;
return Err(Error::Other(format!(
"Failed to fetch notes map at height {height}: {err}"
)));
}
let payload: Response = response.json().await.map_err(|err| {
Error::Other(format!(
"Could not deserialize the notes map JSON response at height \
Expand Down Expand Up @@ -587,6 +631,12 @@ impl MaspClient for IndexerMaspClient {
"Failed to fetch witness map at height {height}: {err}"
))
})?;
if !response.status().is_success() {
let err = Self::get_server_error(response).await?;
return Err(Error::Other(format!(
"Failed to fetch witness map at height {height}: {err}"
)));
}
let payload: WitnessMapResponse =
response.json().await.map_err(|err| {
Error::Other(format!(
Expand Down

0 comments on commit 300c51a

Please sign in to comment.