Handle network errors better

This commit is contained in:
Maximilian Ammann 2022-04-13 16:45:49 +02:00
parent 2bd1f75b1e
commit a19f0ddfc1
2 changed files with 11 additions and 5 deletions

View File

@ -30,7 +30,7 @@ impl HttpSourceClient {
Self { Self {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
inner_client: crate::platform::http_client::ReqwestHttpClient::new(Some( inner_client: crate::platform::http_client::ReqwestHttpClient::new(Some(
"/tmp/mapr-cache".to_string(), // TODO make path dynamic "./mapr-cache".to_string(), // TODO make path dynamic
)), )),
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
inner_client: crate::platform::http_client::WHATWGFetchHttpClient::new(), inner_client: crate::platform::http_client::WHATWGFetchHttpClient::new(),

View File

@ -38,10 +38,16 @@ impl ReqwestHttpClient {
pub async fn fetch(&self, url: &str) -> Result<Vec<u8>, Error> { pub async fn fetch(&self, url: &str) -> Result<Vec<u8>, Error> {
let response = self.client.get(url).send().await?; let response = self.client.get(url).send().await?;
if response.status() != StatusCode::OK { match response.error_for_status() {
return Err(Error::Network("response code not 200".to_string())); Ok(response) => {
if response.status() == StatusCode::NOT_MODIFIED {
log::info!("Using data from cache");
}
let body = response.bytes().await?;
Ok(Vec::from(body.as_ref()))
}
Err(e) => Err(Error::Network(e.to_string())),
} }
let body = response.bytes().await?;
Ok(Vec::from(body.as_ref()))
} }
} }