Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): include status code and error body in Gmail API error messages (#2216)

Evan Hutnik8695384e5df5
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 1640383355..1593061dab 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -4374,6 +4374,7 @@ dependencies = [
"mail-builder",
"mockall",
"models_email",
+ "regex",
"reqwest",
"serde_json",
"tokio",
diff --git a/rust/cloud-storage/gmail_client/Cargo.toml b/rust/cloud-storage/gmail_client/Cargo.toml
index 4925fc5ee4..2817dc1492 100644
--- a/rust/cloud-storage/gmail_client/Cargo.toml
+++ b/rust/cloud-storage/gmail_client/Cargo.toml
@@ -16,6 +16,7 @@ jsonwebtoken = { workspace = true }
mail-builder = "0.4.3"
mockall = { workspace = true }
models_email = { path = "../models_email" }
+regex = { workspace = true }
reqwest = { workspace = true, features = ["stream"] }
serde_json = { workspace = true }
tokio = { workspace = true }
diff --git a/rust/cloud-storage/gmail_client/src/history.rs b/rust/cloud-storage/gmail_client/src/history.rs
index 52272ab3fe..92531d0713 100644
--- a/rust/cloud-storage/gmail_client/src/history.rs
+++ b/rust/cloud-storage/gmail_client/src/history.rs
@@ -43,12 +43,19 @@ pub(crate) async fn get_history(
)
})?;
- let response = response.error_for_status().with_context(|| {
- format!(
- "Gmail API returned an error status (list history), start_history_id: {}",
- start_history_id
- )
- })?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (list history) for start_history_id: {}: {}",
+ status,
+ start_history_id,
+ error_body
+ );
+ }
let page_response = response.json::<HistoryListResponse>()
.await
@@ -95,7 +102,18 @@ pub async fn get_current_history_id(
.await
.context("Failed to send request to Gmail API (get profile)")?;
- let response = response.error_for_status()?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (get current history id): {}",
+ status,
+ error_body
+ );
+ }
let profile_response = response
.json::<UserProfileResponse>()
diff --git a/rust/cloud-storage/gmail_client/src/labels.rs b/rust/cloud-storage/gmail_client/src/labels.rs
index 667ee2b6cb..97e6ef04eb 100644
--- a/rust/cloud-storage/gmail_client/src/labels.rs
+++ b/rust/cloud-storage/gmail_client/src/labels.rs
@@ -41,9 +41,19 @@ pub async fn modify_message_labels(
.await
.context("Failed to send request to Gmail API (modify message labels)")?;
- response
- .error_for_status()
- .context("Gmail API returned an error status (modify message labels)")?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (modify message labels) for provider_message_id: {}: {}",
+ status,
+ provider_message_id,
+ error_body
+ );
+ }
Ok(())
}
@@ -133,9 +143,14 @@ pub async fn fetch_user_labels(
.await
.context("Failed to send request to Gmail API (fetch labels)")?;
- let response = response
- .error_for_status()
- .context("Gmail API returned an error status (fetch labels)")?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!("Gmail API error {} (fetch labels): {}", status, error_body);
+ }
let labels_response = response
.json::<GmailLabelsResponse>()
diff --git a/rust/cloud-storage/gmail_client/src/lib.rs b/rust/cloud-storage/gmail_client/src/lib.rs
index 59742dbd7c..c31631045a 100644
--- a/rust/cloud-storage/gmail_client/src/lib.rs
+++ b/rust/cloud-storage/gmail_client/src/lib.rs
@@ -11,6 +11,24 @@ pub(crate) mod threads;
pub(crate) mod watch;
use crate::labels::delete_gmail_label;
+use regex::Regex;
+use std::sync::LazyLock;
+
+const MAX_ERROR_BODY_LEN: usize = 1024;
+
+static EMAIL_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}").unwrap());
+
+/// Sanitize a Gmail API error body: redact email addresses, cap length.
+pub(crate) fn sanitize_error_body(body: &str) -> String {
+ let redacted = EMAIL_REGEX.replace_all(body, "[REDACTED_EMAIL]");
+ let trimmed = redacted.trim();
+ if trimmed.len() <= MAX_ERROR_BODY_LEN {
+ trimmed.to_string()
+ } else {
+ format!("{}… (truncated)", &trimmed[..MAX_ERROR_BODY_LEN])
+ }
+}
use crate::auth::{fetch_google_public_keys, verify_google_jwt};
use crate::contacts::get_self_connection;
diff --git a/rust/cloud-storage/gmail_client/src/messages.rs b/rust/cloud-storage/gmail_client/src/messages.rs
index de0e56bbd6..ccbbe8b79a 100644
--- a/rust/cloud-storage/gmail_client/src/messages.rs
+++ b/rust/cloud-storage/gmail_client/src/messages.rs
@@ -38,12 +38,19 @@ pub(crate) async fn get_message(
return Ok(None);
}
- let response = response.error_for_status().with_context(|| {
- format!(
- "Gmail API returned an error status (get message) for message_provider_id: {}",
- message_provider_id
- )
- })?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (get message) for message_provider_id: {}: {}",
+ status,
+ message_provider_id,
+ error_body
+ );
+ }
let message_response = response
.json::<MessageResource>()
@@ -84,12 +91,19 @@ pub(crate) async fn get_message_thread_id(
return Ok(None);
}
- let response = response.error_for_status().with_context(|| {
- format!(
- "Gmail API returned an error status (get message) for message_provider_id: {}",
- message_provider_id
- )
- })?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (get message thread_id) for message_provider_id: {}: {}",
+ status,
+ message_provider_id,
+ error_body
+ );
+ }
let message_response = response
.json::<MinimalMessageResource>()
@@ -129,12 +143,19 @@ pub(crate) async fn get_message_label_ids(
return Ok(None);
}
- let response = response.error_for_status().with_context(|| {
- format!(
- "Gmail API returned an error status (get message) for message_provider_id: {}",
- message_provider_id
- )
- })?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (get message label_ids) for message_provider_id: {}: {}",
+ status,
+ message_provider_id,
+ error_body
+ );
+ }
let message_response = response
.json::<MinimalMessageResource>()
diff --git a/rust/cloud-storage/gmail_client/src/profile.rs b/rust/cloud-storage/gmail_client/src/profile.rs
index 843f030290..20629e8c10 100644
--- a/rust/cloud-storage/gmail_client/src/profile.rs
+++ b/rust/cloud-storage/gmail_client/src/profile.rs
@@ -17,9 +17,18 @@ pub async fn get_profile_threads_total(
.await
.context("Failed to send request to Gmail API (get user profile)")?;
- let response = response
- .error_for_status()
- .context("Gmail API returned an error status (get user profile)")?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!(
+ "Gmail API error {} (get user profile): {}",
+ status,
+ error_body
+ );
+ }
// Parse the response directly into our GmailUserProfile structure
let user_profile = response
diff --git a/rust/cloud-storage/gmail_client/src/threads.rs b/rust/cloud-storage/gmail_client/src/threads.rs
index fbcc5919c4..6011179ce1 100644
--- a/rust/cloud-storage/gmail_client/src/threads.rs
+++ b/rust/cloud-storage/gmail_client/src/threads.rs
@@ -1,4 +1,4 @@
-use crate::GmailClient;
+use crate::{GmailClient, sanitize_error_body};
use anyhow::{Context, anyhow};
use models_email::email::service::thread::{ThreadList, ThreadSummary};
use models_email::gmail::error::GmailError;
@@ -54,9 +54,15 @@ pub(crate) async fn list_threads(
.await
.context("Failed to send request to Gmail API (list threads)")?;
- let response = response
- .error_for_status()
- .context("Gmail API returned an error status (list threads)")?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ let sanitized = sanitize_error_body(&error_body);
+ anyhow::bail!("Gmail API error {} (list threads): {}", status, sanitized);
+ }
let gmail_response = response
.json::<ListThreadsResponse>()
@@ -241,10 +247,20 @@ pub(crate) async fn get_message_ids_for_thread(
thread_id
))?;
- let response = response.error_for_status().context(format!(
- "Gmail API returned an error status for thread {}",
- thread_id
- ))?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ let sanitized = sanitize_error_body(&error_body);
+ anyhow::bail!(
+ "Gmail API error {} (get message ids for thread) for thread_id: {}: {}",
+ status,
+ thread_id,
+ sanitized
+ );
+ }
let thread_resource = response
.json::<MinimalThreadResource>()
diff --git a/rust/cloud-storage/gmail_client/src/watch.rs b/rust/cloud-storage/gmail_client/src/watch.rs
index 139440a4b1..ebf2817870 100644
--- a/rust/cloud-storage/gmail_client/src/watch.rs
+++ b/rust/cloud-storage/gmail_client/src/watch.rs
@@ -69,9 +69,14 @@ pub(crate) async fn stop_watch(client: &GmailClient, access_token: &str) -> anyh
.await
.context("Failed to send request to Gmail API (stop watch)")?;
- response
- .error_for_status()
- .context("Gmail API returned an error status (stop watch)")?;
+ let status = response.status();
+ if !status.is_success() {
+ let error_body = response
+ .text()
+ .await
+ .unwrap_or_else(|_| "Failed to read error body".to_string());
+ anyhow::bail!("Gmail API error {} (stop watch): {}", status, error_body);
+ }
Ok(())
}