Handle HTTP errors to proposal API
@@ -4,14 +4,15 @@ use anyhow::Result;
pub async fn http_post<I: AsRef<str>>(url: I, body: String) -> Result<String> {
let url = url.as_ref();
info!("HTTP POST to {}", url);
- let resp = reqwest::Client::new()
- .post(url)
- .body(body)
- .send()
- .await?
- .text()
- .await?;
- Ok(resp)
+ let resp = reqwest::Client::new().post(url).body(body).send().await?;
+ let status = resp.status();
+ let text = resp.text().await?;
+ // With error_for_status{_ref}, it's unclear how to propagate errors and also get the error
+ // message from the body, so do this
+ if status.is_client_error() || status.is_server_error() {
+ bail!("HTTP error {}: {}", status, text);
+ }
+ Ok(text)
}
/// Performs an HTTP GET request and returns the raw response. Unlike the variations in
@@ -19,6 +20,6 @@ pub async fn http_post<I: AsRef<str>>(url: I, body: String) -> Result<String> {
pub async fn http_get<I: AsRef<str>>(url: I) -> Result<Vec<u8>> {
let url = url.as_ref();
info!("HTTP GET {}", url);
- let resp = reqwest::get(url).await?.bytes().await?;
+ let resp = reqwest::get(url).await?.error_for_status()?.bytes().await?;
Ok(resp.to_vec())
}
