diff --git a/src/calendar/remote_calendar.rs b/src/calendar/remote_calendar.rs index a148162..60e9c13 100644 --- a/src/calendar/remote_calendar.rs +++ b/src/calendar/remote_calendar.rs @@ -51,7 +51,7 @@ impl BaseCalendar for RemoteCalendar { async fn add_item(&mut self, item: Item) -> Result> { let ical_text = crate::ical::build_from(&item)?; - let request = reqwest::Client::new() + let response = reqwest::Client::new() .put(item.id().as_url().clone()) .header("If-None-Match", "*") .header(CONTENT_TYPE, "text/calendar") @@ -61,7 +61,11 @@ impl BaseCalendar for RemoteCalendar { .send() .await?; - let reply_hdrs = request.headers(); + if response.status().is_success() == false { + return Err(format!("Unexpected HTTP status code {:?}", response.status()).into()); + } + + let reply_hdrs = response.headers(); match reply_hdrs.get("ETag") { None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()), Some(etag) => { @@ -91,6 +95,10 @@ impl BaseCalendar for RemoteCalendar { .send() .await?; + if request.status().is_success() == false { + return Err(format!("Unexpected HTTP status code {:?}", request.status()).into()); + } + let reply_hdrs = request.headers(); match reply_hdrs.get("ETag") { None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()), @@ -161,6 +169,10 @@ impl DavCalendar for RemoteCalendar { .send() .await?; + if res.status().is_success() == false { + return Err(format!("Unexpected HTTP status code {:?}", res.status()).into()); + } + let text = res.text().await?; // This is supposed to be cached @@ -175,11 +187,16 @@ impl DavCalendar for RemoteCalendar { } async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box> { - reqwest::Client::new() + let del_response = reqwest::Client::new() .delete(item_id.as_url().clone()) .basic_auth(self.resource.username(), Some(self.resource.password())) .send() .await?; + + if del_response.status().is_success() == false { + return Err(format!("Unexpected HTTP status code {:?}", del_response.status()).into()); + } + Ok(()) } } diff --git a/src/client.rs b/src/client.rs index 62aa557..2c7b2aa 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use async_trait::async_trait; -use reqwest::Method; +use reqwest::{Method, StatusCode}; use reqwest::header::CONTENT_TYPE; use minidom::Element; use url::Url; @@ -63,6 +63,10 @@ pub async fn sub_request(resource: &Resource, method: &str, body: String, depth: .send() .await?; + if res.status().is_success() == false { + return Err(format!("Unexpected HTTP status code {:?}", res.status()).into()); + } + let text = res.text().await?; Ok(text) } @@ -252,7 +256,7 @@ impl CalDavSource for Client { let creation_body = calendar_body(name, supported_components); - reqwest::Client::new() + let response = reqwest::Client::new() .request(Method::from_bytes(b"MKCALENDAR").unwrap(), id.clone()) .header(CONTENT_TYPE, "application/xml") .basic_auth(self.resource.username(), Some(self.resource.password())) @@ -260,6 +264,11 @@ impl CalDavSource for Client { .send() .await?; + let status = response.status(); + if status != StatusCode::CREATED { + return Err(format!("Unexpected HTTP status code. Expected CREATED, got {}", status.as_u16()).into()); + } + self.get_calendar(&id).await.ok_or(format!("Unable to insert calendar {:?}", id).into()) } }