diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2022-10-13 10:59:10 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2022-10-13 10:59:10 +0300 |
commit | 0ef4dde9392452f7cf7f18294f747fc6e0babb8d (patch) | |
tree | 1c9c596cc2d2f515e25542a48a0f77f337230481 | |
parent | 55ed962425ba25d2317946705ff6861a77eb770f (diff) | |
download | meli-0ef4dde9392452f7cf7f18294f747fc6e0babb8d.zip |
melib/jmap: wrap serde_json deserialize errors in human readable errors
-rw-r--r-- | melib/src/backends/jmap.rs | 45 | ||||
-rw-r--r-- | melib/src/backends/jmap/connection.rs | 9 | ||||
-rw-r--r-- | melib/src/backends/jmap/objects/email.rs | 3 | ||||
-rw-r--r-- | melib/src/backends/jmap/objects/email/import.rs | 3 | ||||
-rw-r--r-- | melib/src/backends/jmap/protocol.rs | 27 | ||||
-rw-r--r-- | melib/src/backends/jmap/rfc8620.rs | 12 |
6 files changed, 84 insertions, 15 deletions
diff --git a/melib/src/backends/jmap.rs b/melib/src/backends/jmap.rs index 141e040d..003c2e0c 100644 --- a/melib/src/backends/jmap.rs +++ b/melib/src/backends/jmap.rs @@ -453,7 +453,14 @@ impl MailBackend for JmapType { }; let res_text = res.text().await?; - let upload_response: UploadResponse = serde_json::from_str(&res_text)?; + let upload_response: UploadResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; let mut req = Request::new(conn.request_no.clone()); let creation_id: Id<EmailObject> = "1".to_string().into(); let mut email_imports = HashMap::default(); @@ -477,7 +484,14 @@ impl MailBackend for JmapType { .await?; let res_text = res.text().await?; - let mut v: MethodResponse = serde_json::from_str(&res_text)?; + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; let m = ImportResponse::try_from(v.method_responses.remove(0)).or_else(|err| { let ierr: Result<ImportError> = serde_json::from_str(&res_text).map_err(|err| err.into()); @@ -551,7 +565,14 @@ impl MailBackend for JmapType { .await?; let res_text = res.text().await?; - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; *store.online_status.lock().await = (std::time::Instant::now(), Ok(())); let m = QueryResponse::<EmailObject>::try_from(v.method_responses.remove(0))?; let QueryResponse::<EmailObject> { ids, .. } = m; @@ -647,7 +668,14 @@ impl MailBackend for JmapType { let res_text = res.text().await?; - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; *store.online_status.lock().await = (std::time::Instant::now(), Ok(())); let m = SetResponse::<EmailObject>::try_from(v.method_responses.remove(0))?; if let Some(ids) = m.not_updated { @@ -752,7 +780,14 @@ impl MailBackend for JmapType { *{"methodResponses":[["Email/set",{"notUpdated":null,"notDestroyed":null,"oldState":"86","newState":"87","accountId":"u148940c7","updated":{"M045926eed54b11423918f392":{"id":"M045926eed54b11423918f392"}},"created":null,"destroyed":null,"notCreated":null},"m3"]],"sessionState":"cyrus-0;p-5;vfs-0"} */ //debug!("res_text = {}", &res_text); - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; *store.online_status.lock().await = (std::time::Instant::now(), Ok(())); let m = SetResponse::<EmailObject>::try_from(v.method_responses.remove(0))?; if let Some(ids) = m.not_updated { diff --git a/melib/src/backends/jmap/connection.rs b/melib/src/backends/jmap/connection.rs index 02b41b09..56239e47 100644 --- a/melib/src/backends/jmap/connection.rs +++ b/melib/src/backends/jmap/connection.rs @@ -192,7 +192,14 @@ impl JmapConnection { let res_text = res.text().await?; debug!(&res_text); - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &self.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *self.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; let changes_response = ChangesResponse::<EmailObject>::try_from(v.method_responses.remove(0))?; if changes_response.new_state == current_state { diff --git a/melib/src/backends/jmap/objects/email.rs b/melib/src/backends/jmap/objects/email.rs index a9111c06..b1e2f3c4 100644 --- a/melib/src/backends/jmap/objects/email.rs +++ b/melib/src/backends/jmap/objects/email.rs @@ -842,7 +842,8 @@ pub struct EmailQueryChangesResponse { impl std::convert::TryFrom<&RawValue> for EmailQueryChangesResponse { type Error = crate::error::MeliError; fn try_from(t: &RawValue) -> Result<EmailQueryChangesResponse> { - let res: (String, EmailQueryChangesResponse, String) = serde_json::from_str(t.get())?; + let res: (String, EmailQueryChangesResponse, String) = + serde_json::from_str(t.get()).map_err(|err| crate::error::MeliError::new(format!("BUG: Could not deserialize server JSON response properly, please report this!\nReply from server: {}", &t)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug))?; assert_eq!(&res.0, "Email/queryChanges"); Ok(res.1) } diff --git a/melib/src/backends/jmap/objects/email/import.rs b/melib/src/backends/jmap/objects/email/import.rs index 638c58c3..7c360526 100644 --- a/melib/src/backends/jmap/objects/email/import.rs +++ b/melib/src/backends/jmap/objects/email/import.rs @@ -184,7 +184,8 @@ pub struct ImportResponse { impl std::convert::TryFrom<&RawValue> for ImportResponse { type Error = crate::error::MeliError; fn try_from(t: &RawValue) -> Result<ImportResponse> { - let res: (String, ImportResponse, String) = serde_json::from_str(t.get())?; + let res: (String, ImportResponse, String) = + serde_json::from_str(t.get()).map_err(|err| crate::error::MeliError::new(format!("BUG: Could not deserialize server JSON response properly, please report this!\nReply from server: {}", &t)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug))?; assert_eq!(&res.0, &ImportCall::NAME); Ok(res.1) } diff --git a/melib/src/backends/jmap/protocol.rs b/melib/src/backends/jmap/protocol.rs index a8bd2ff4..7eb7e640 100644 --- a/melib/src/backends/jmap/protocol.rs +++ b/melib/src/backends/jmap/protocol.rs @@ -102,7 +102,14 @@ pub async fn get_mailboxes(conn: &JmapConnection) -> Result<HashMap<MailboxHash, .await?; let res_text = res.text().await?; - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; *conn.store.online_status.lock().await = (std::time::Instant::now(), Ok(())); let m = GetResponse::<MailboxObject>::try_from(v.method_responses.remove(0))?; let GetResponse::<MailboxObject> { @@ -202,7 +209,14 @@ pub async fn get_message_list( .await?; let res_text = res.text().await?; - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; *conn.store.online_status.lock().await = (std::time::Instant::now(), Ok(())); let m = QueryResponse::<EmailObject>::try_from(v.method_responses.remove(0))?; let QueryResponse::<EmailObject> { ids, .. } = m; @@ -275,7 +289,14 @@ pub async fn fetch( let res_text = res.text().await?; - let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap(); + let mut v: MethodResponse = match serde_json::from_str(&res_text) { + Err(err) => { + let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug); + *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone())); + return Err(err); + } + Ok(s) => s, + }; let e = GetResponse::<EmailObject>::try_from(v.method_responses.pop().unwrap())?; let query_response = QueryResponse::<EmailObject>::try_from(v.method_responses.pop().unwrap())?; store diff --git a/melib/src/backends/jmap/rfc8620.rs b/melib/src/backends/jmap/rfc8620.rs index 9ce8d43f..5142435c 100644 --- a/melib/src/backends/jmap/rfc8620.rs +++ b/melib/src/backends/jmap/rfc8620.rs @@ -413,7 +413,8 @@ pub struct GetResponse<OBJ: Object> { impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for GetResponse<OBJ> { type Error = crate::error::MeliError; fn try_from(t: &RawValue) -> Result<GetResponse<OBJ>, crate::error::MeliError> { - let res: (String, GetResponse<OBJ>, String) = serde_json::from_str(t.get())?; + let res: (String, GetResponse<OBJ>, String) = + serde_json::from_str(t.get()).map_err(|err| crate::error::MeliError::new(format!("BUG: Could not deserialize server JSON response properly, please report this!\nReply from server: {}", &t)).set_source(Some(Arc::new(err))).set_kind(crate::error::ErrorKind::Bug))?; assert_eq!(&res.0, &format!("{}/get", OBJ::NAME)); Ok(res.1) } @@ -517,7 +518,8 @@ pub struct QueryResponse<OBJ: Object> { impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for QueryResponse<OBJ> { type Error = crate::error::MeliError; fn try_from(t: &RawValue) -> Result<QueryResponse<OBJ>, crate::error::MeliError> { - let res: (String, QueryResponse<OBJ>, String) = serde_json::from_str(t.get())?; + let res: (String, QueryResponse<OBJ>, String) = + serde_json::from_str(t.get()).map_err(|err| crate::error::MeliError::new(format!("BUG: Could not deserialize server JSON response properly, please report this!\nReply from server: {}", &t)).set_source(Some(Arc::new(err))).set_kind(crate::error::ErrorKind::Bug))?; assert_eq!(&res.0, &format!("{}/query", OBJ::NAME)); Ok(res.1) } @@ -651,7 +653,8 @@ pub struct ChangesResponse<OBJ: Object> { impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for ChangesResponse<OBJ> { type Error = crate::error::MeliError; fn try_from(t: &RawValue) -> Result<ChangesResponse<OBJ>, crate::error::MeliError> { - let res: (String, ChangesResponse<OBJ>, String) = serde_json::from_str(t.get())?; + let res: (String, ChangesResponse<OBJ>, String) = + serde_json::from_str(t.get()).map_err(|err| crate::error::MeliError::new(format!("BUG: Could not deserialize server JSON response properly, please report this!\nReply from server: {}", &t)).set_source(Some(Arc::new(err))).set_kind(crate::error::ErrorKind::Bug))?; assert_eq!(&res.0, &format!("{}/changes", OBJ::NAME)); Ok(res.1) } @@ -849,7 +852,8 @@ pub struct SetResponse<OBJ: Object> { impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for SetResponse<OBJ> { type Error = crate::error::MeliError; fn try_from(t: &RawValue) -> Result<SetResponse<OBJ>, crate::error::MeliError> { - let res: (String, SetResponse<OBJ>, String) = serde_json::from_str(t.get())?; + let res: (String, SetResponse<OBJ>, String) = + serde_json::from_str(t.get()).map_err(|err| crate::error::MeliError::new(format!("BUG: Could not deserialize server JSON response properly, please report this!\nReply from server: {}", &t)).set_source(Some(Arc::new(err))).set_kind(crate::error::ErrorKind::Bug))?; assert_eq!(&res.0, &format!("{}/set", OBJ::NAME)); Ok(res.1) } |