Skip to content

Commit

Permalink
refactor: Improve errors for failed action operations
Browse files Browse the repository at this point in the history
  • Loading branch information
LeanderBB committed Dec 31, 2024
1 parent 69d7c34 commit 8aec182
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
12 changes: 12 additions & 0 deletions proton/proton-api/src/domain/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl APIError {
}
}
}

/// Create a new instance with an `http_status` code.
#[must_use]
pub fn new(http_status: u16) -> Self {
Expand All @@ -137,4 +138,15 @@ impl APIError {
details: None,
}
}

/// Create a new instance with an `http_status` code and `description`.
#[must_use]
pub fn with_desc(http_status: u16, description: APIErrorDesc) -> Self {
Self {
http_code: http_status,
api_code: description.code,
message: description.error,
details: description.details,
}
}
}
15 changes: 14 additions & 1 deletion proton/proton-api/src/requests/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::domain::errors::{APIErrorDesc, OPERATION_SUCCESS};
use crate::domain::errors::{APIError, APIErrorDesc, OPERATION_SUCCESS};
use crate::domain::label;
use crate::domain::message::Id;
use http::{Method, RequestBuilder};
Expand Down Expand Up @@ -33,6 +33,19 @@ impl OperationResponse {
pub fn is_success(&self) -> bool {
self.response.code == OPERATION_SUCCESS
}

/// Convert the current response into a result type.
///
/// # Errors
///
/// Converts into an `APIError` if the operation did not succeed.
pub fn into_result(self) -> Result<(), APIError> {
if self.is_success() {
Ok(())
} else {
Err(APIError::with_desc(200, self.response))
}
}
}

#[derive(Debug, Deserialize)]
Expand Down
28 changes: 25 additions & 3 deletions youhavemail/src/backend/proton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::backend::{Action, Error as BackendError, Error, NewEmail, Result as BackendResult};
use crate::state::Account;
use crate::yhm::{IntoAccount, Yhm};
use anyhow::anyhow;
use http::{Client, Proxy};
use parking_lot::Mutex;
use proton_api::auth::{new_thread_safe_store, Auth as ProtonAuth, InMemoryStore, StoreError};
Expand Down Expand Up @@ -264,27 +265,48 @@ impl crate::backend::Poller for Poller {
match action {
AccountAction::MarkMessageRead(id) => {
debug!("Marking {id} as read");
self.session
let response = self
.session
.execute_with_auth(PutMarkMessageReadRequest::new([id]))
.inspect_err(|e| {
error!("Failed to mark message as read: {e}");
})?;
for response in response.responses {
response.into_result().map_err(|e| {
error!("Failed to mark message as read: {}", e);
Error::Unknown(anyhow!("Failed to mark message as read"))
})?;
}
}
AccountAction::MoveMessageToTrash(id) => {
debug!("Moving {id} to trash");
self.session
let response = self
.session
.execute_with_auth(PutLabelMessageRequest::new(label::Id::trash(), [id]))
.inspect_err(|e| {
error!("Failed to move message to trash: {e}");
})?;
for response in response.responses {
response.into_result().map_err(|e| {
error!("Failed to move message to trash: {}", e);
Error::Unknown(anyhow!("Failed to move message to trash"))
})?;
}
}
AccountAction::MoveMessageToSpam(id) => {
debug!("Moving {id} to spam");
self.session
let response = self
.session
.execute_with_auth(PutLabelMessageRequest::new(label::Id::spam(), [id]))
.inspect_err(|e| {
error!("Failed to move message to spam: {e}");
})?;
for response in response.responses {
response.into_result().map_err(|e| {
error!("Failed to move message to spam: {}", e);
Error::Unknown(anyhow!("Failed to move message to spam"))
})?;
}
}
}

Expand Down

0 comments on commit 8aec182

Please sign in to comment.