Skip to content

Commit ad2033a

Browse files
committed
Deserialize server err response from masp indexer client
1 parent 614a4d6 commit ad2033a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

crates/sdk/src/masp/utils.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,25 @@ impl IndexerMaspClient {
290290
fn endpoint(&self, which: &str) -> String {
291291
format!("{}{which}", self.indexer_api)
292292
}
293+
294+
async fn get_server_error(
295+
response: reqwest::Response,
296+
) -> Result<String, Error> {
297+
use serde::Deserialize;
298+
299+
#[derive(Deserialize)]
300+
struct Response {
301+
message: String,
302+
}
303+
304+
let payload: Response = response.json().await.map_err(|err| {
305+
Error::Other(format!(
306+
"Could not deserialize server's error JSON response: {err}"
307+
))
308+
})?;
309+
310+
Ok(payload.message)
311+
}
293312
}
294313

295314
#[cfg(not(target_family = "wasm"))]
@@ -318,6 +337,12 @@ impl MaspClient for IndexerMaspClient {
318337
"Failed to fetch latest block height: {err}"
319338
))
320339
})?;
340+
if !response.status().is_success() {
341+
let err = Self::get_server_error(response).await?;
342+
return Err(Error::Other(format!(
343+
"Failed to fetch last block height: {err}"
344+
)));
345+
}
321346
let payload: Response = response.json().await.map_err(|err| {
322347
Error::Other(format!(
323348
"Could not deserialize latest block height JSON response: \
@@ -389,6 +414,13 @@ impl MaspClient for IndexerMaspClient {
389414
{from_height}-{to_height}: {err}"
390415
))
391416
})?;
417+
if !response.status().is_success() {
418+
let err = Self::get_server_error(response).await?;
419+
return Err(Error::Other(format!(
420+
"Failed to fetch transactions in the range \
421+
{from_height}-{to_height}: {err}"
422+
)));
423+
}
392424
let payload: TxResponse =
393425
response.json().await.map_err(|err| {
394426
Error::Other(format!(
@@ -486,6 +518,12 @@ impl MaspClient for IndexerMaspClient {
486518
"Failed to fetch commitment tree at height {height}: {err}"
487519
))
488520
})?;
521+
if !response.status().is_success() {
522+
let err = Self::get_server_error(response).await?;
523+
return Err(Error::Other(format!(
524+
"Failed to fetch commitment tree at height {height}: {err}"
525+
)));
526+
}
489527
let payload: Response = response.json().await.map_err(|err| {
490528
Error::Other(format!(
491529
"Could not deserialize the commitment tree JSON response at \
@@ -534,6 +572,12 @@ impl MaspClient for IndexerMaspClient {
534572
"Failed to fetch notes map at height {height}: {err}"
535573
))
536574
})?;
575+
if !response.status().is_success() {
576+
let err = Self::get_server_error(response).await?;
577+
return Err(Error::Other(format!(
578+
"Failed to fetch notes map at height {height}: {err}"
579+
)));
580+
}
537581
let payload: Response = response.json().await.map_err(|err| {
538582
Error::Other(format!(
539583
"Could not deserialize the notes map JSON response at height \
@@ -590,6 +634,12 @@ impl MaspClient for IndexerMaspClient {
590634
"Failed to fetch witness map at height {height}: {err}"
591635
))
592636
})?;
637+
if !response.status().is_success() {
638+
let err = Self::get_server_error(response).await?;
639+
return Err(Error::Other(format!(
640+
"Failed to fetch witness map at height {height}: {err}"
641+
)));
642+
}
593643
let payload: WitnessMapResponse =
594644
response.json().await.map_err(|err| {
595645
Error::Other(format!(

0 commit comments

Comments
 (0)