Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(connections/gmail): surface upstream Google API status+body instead of collapsing to 500

Louis Beaumonta4cbac050f48
diff --git a/crates/screenpipe-engine/src/connections_api.rs b/crates/screenpipe-engine/src/connections_api.rs
index a81d4a441..129568246 100644
--- a/crates/screenpipe-engine/src/connections_api.rs
+++ b/crates/screenpipe-engine/src/connections_api.rs
@@ -721,15 +721,8 @@ async fn gmail_list_messages_inner(
pairs.append_pair("pageToken", pt);
}
}
- let data: Value = client
- .get(url)
- .bearer_auth(&token)
- .send()
- .await?
- .error_for_status()?
- .json()
- .await?;
- Ok(data)
+ let resp = client.get(url).bearer_auth(&token).send().await?;
+ gmail_json_or_upstream(resp).await
}
/// GET /connections/gmail/messages/:id — read a full Gmail message.
@@ -756,14 +749,8 @@ async fn gmail_get_message_inner(
"https://gmail.googleapis.com/gmail/v1/users/me/messages/{}?format=full",
id
);
- let msg: Value = client
- .get(&url)
- .bearer_auth(&token)
- .send()
- .await?
- .error_for_status()?
- .json()
- .await?;
+ let resp = client.get(&url).bearer_auth(&token).send().await?;
+ let msg = gmail_json_or_upstream(resp).await?;
Ok(parse_gmail_message(&msg))
}
@@ -790,16 +777,13 @@ async fn gmail_send_inner(
let from = body.from.unwrap_or_default();
let raw = build_rfc2822_message(&from, &body.to, &body.subject, &body.body);
let encoded = URL_SAFE_NO_PAD.encode(raw.as_bytes());
- let data: Value = client
+ let resp = client
.post("https://gmail.googleapis.com/gmail/v1/users/me/messages/send")
.bearer_auth(&token)
.json(&json!({ "raw": encoded }))
.send()
- .await?
- .error_for_status()?
- .json()
.await?;
- Ok(data)
+ gmail_json_or_upstream(resp).await
}
/// Retrieve a valid Gmail OAuth token or return an error.
@@ -834,8 +818,51 @@ async fn gmail_list_instances(State(state): State<ConnectionsState>) -> (StatusC
(StatusCode::OK, Json(json!({ "data": accounts })))
}
-/// Convert an anyhow error into the standard `(StatusCode, Json)` handler return.
+/// Carries the upstream Gmail API status + body so [`gmail_err`] can
+/// surface a meaningful status to the caller instead of collapsing every
+/// failure to 500. Without this, a stale OAuth token (401) is
+/// indistinguishable from a real internal bug from the chat UI.
+#[derive(Debug)]
+struct GmailUpstreamError {
+ status: reqwest::StatusCode,
+ body: String,
+}
+
+impl std::fmt::Display for GmailUpstreamError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "gmail api {}: {}", self.status.as_u16(), self.body)
+ }
+}
+
+impl std::error::Error for GmailUpstreamError {}
+
+/// Parse a Gmail API response, preserving the upstream status/body when
+/// the call fails. Replaces ad-hoc `error_for_status()?` + `json()` chains
+/// that swallowed the response body before anyone could read it.
+async fn gmail_json_or_upstream(resp: reqwest::Response) -> anyhow::Result<Value> {
+ let status = resp.status();
+ if status.is_success() {
+ let v: Value = resp.json().await?;
+ return Ok(v);
+ }
+ let body = resp.text().await.unwrap_or_default();
+ Err(GmailUpstreamError { status, body }.into())
+}
+
+/// Convert an anyhow error into the standard `(StatusCode, Json)` handler
+/// return, forwarding the upstream Gmail status when present.
fn gmail_err(e: anyhow::Error) -> (StatusCode, Json<Value>) {
+ if let Some(up) = e.downcast_ref::<GmailUpstreamError>() {
+ let status = StatusCode::from_u16(up.status.as_u16())
+ .unwrap_or(StatusCode::BAD_GATEWAY);
+ return (
+ status,
+ Json(json!({
+ "error": up.body,
+ "upstream_status": up.status.as_u16(),
+ })),
+ );
+ }
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({ "error": e.to_string() })),