esplora: return the error code in case of bad responses
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
parent
026ad4decf
commit
f377c119cf
|
@ -40,6 +40,15 @@ impl EsploraAPI {
|
||||||
|
|
||||||
transfer.perform()?;
|
transfer.perform()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let response_code = easy.response_code()?;
|
||||||
|
|
||||||
|
// Check if the response code indicates an HTTP error
|
||||||
|
if response_code != 200 {
|
||||||
|
let mut err = Error::new(response_code);
|
||||||
|
unsafe { err.set_extra(String::from_utf8_unchecked(body)) };
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
Ok(body)
|
Ok(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +64,14 @@ impl EsploraAPI {
|
||||||
|
|
||||||
transfer.perform()?;
|
transfer.perform()?;
|
||||||
}
|
}
|
||||||
|
let response_code = easy.response_code()?;
|
||||||
|
|
||||||
|
// Check if the response code indicates an HTTP error
|
||||||
|
if response_code != 200 {
|
||||||
|
let mut err = Error::new(response_code);
|
||||||
|
unsafe { err.set_extra(String::from_utf8_unchecked(body)) };
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
Ok(body)
|
Ok(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,11 +95,16 @@ mod tests {
|
||||||
fn test_tip() -> Result<()> {
|
fn test_tip() -> Result<()> {
|
||||||
let api = EsploraAPI::new("https://blockstream.info/api")?;
|
let api = EsploraAPI::new("https://blockstream.info/api")?;
|
||||||
let hash = api.raw_call("blocks/tip/hash")?;
|
let hash = api.raw_call("blocks/tip/hash")?;
|
||||||
let hash = String::from_utf8(hash).unwrap();
|
let _ = String::from_utf8(hash).unwrap();
|
||||||
assert_eq!(
|
Ok(())
|
||||||
hash,
|
}
|
||||||
"0000000000000000000099819a9e23a5068a2a6f0e842e4f9568f53ede446300"
|
|
||||||
);
|
#[test]
|
||||||
|
fn test_return_error() -> Result<()> {
|
||||||
|
let api = EsploraAPI::new("https://blockstream.info/api")?;
|
||||||
|
let hash = api.raw_call("tx/12iu3i4u");
|
||||||
|
assert!(hash.is_err());
|
||||||
|
assert!(hash.err().unwrap().code() >= 400);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue