Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
refactor(email-service): Improved Get Threads endpoint logic (#895)
Evan Hutnikab835058a219
diff --git a/rust/cloud-storage/email_db_client/src/threads/get.rs b/rust/cloud-storage/email_db_client/src/threads/get.rsindex 93f1250adf..05d13ea3ca 100644--- a/rust/cloud-storage/email_db_client/src/threads/get.rs+++ b/rust/cloud-storage/email_db_client/src/threads/get.rs@@ -44,11 +44,9 @@ pub async fn fetch_thread_with_messages_paginated(thread_db_id: Uuid,offset: i64,limit: i64,-) -> anyhow::Result<thread::Thread> {+) -> anyhow::Result<Option<thread::Thread>> {if offset < 0 || limit <= 0 {- return Err(anyhow!(- "Offset must be non-negative and limit must be positive"- ));+ anyhow::bail!("Offset must be non-negative and limit must be positive");}let db_thread = sqlx::query_as!(@@ -64,8 +62,11 @@ pub async fn fetch_thread_with_messages_paginated().fetch_optional(pool).await- .with_context(|| format!("Failed to fetch thread with DB ID {}", thread_db_id))?- .ok_or_else(|| anyhow!("Thread with ID {} not found", thread_db_id,))?;+ .with_context(|| format!("Failed to fetch thread with DB ID {}", thread_db_id))?;++ let Some(db_thread) = db_thread else {+ return Ok(None);+ };let db_messages = sqlx::query_as!(db::message::Message,@@ -167,7 +168,7 @@ pub async fn fetch_thread_with_messages_paginated(let full_thread = db_to_service::map_db_thread_to_service(db_thread, processed_messages);- Ok(full_thread)+ Ok(Some(full_thread))}/// get the ids of the latest-updated threads for the user.diff --git a/rust/cloud-storage/email_service/src/api/email/threads/get.rs b/rust/cloud-storage/email_service/src/api/email/threads/get.rsindex df2ea6918e..fdbf542b35 100644--- a/rust/cloud-storage/email_service/src/api/email/threads/get.rs+++ b/rust/cloud-storage/email_service/src/api/email/threads/get.rs@@ -32,6 +32,28 @@ pub enum GetThreadError {#[error("Unauthorized")]Unauthorized,++ #[error("Validation error: {0}")]+ ValidationError(String),+}++impl GetThreadError {+ /// Returns true if this error should be logged+ fn should_log(&self) -> bool {+ !matches!(+ self,+ GetThreadError::ThreadNotFound+ | GetThreadError::Unauthorized+ | GetThreadError::ValidationError(_)+ )+ }++ /// Log the error if appropriate+ fn log_if_needed(&self) {+ if self.should_log() {+ tracing::error!(error = ?self, variant = self.as_ref(), "GetThreadError");+ }+ }}impl IntoResponse for GetThreadError {@@ -39,20 +61,19 @@ impl IntoResponse for GetThreadError {let status_code = match &self {GetThreadError::ThreadNotFound => StatusCode::NOT_FOUND,GetThreadError::Unauthorized => StatusCode::UNAUTHORIZED,+ GetThreadError::ValidationError(_) => StatusCode::BAD_REQUEST,GetThreadError::DatabaseError(_) | GetThreadError::QueryError(_) => {StatusCode::INTERNAL_SERVER_ERROR}};- if status_code.is_server_error() {- tracing::error!(- nested_error = ?self,- error_type = "GetThreadError",- variant = self.as_ref(),- "Internal server error");- }-- (status_code, self.to_string()).into_response()+ (+ status_code,+ Json(ErrorResponse {+ message: self.to_string().as_str(),+ }),+ )+ .into_response()}}@@ -115,20 +136,28 @@ pub async fn get_thread_handler<U: EmailService>(link: OptionalEmailLinkExtractor<U>,Path(PathParams { id: thread_id }): Path<PathParams>,extract::Query(query_params): extract::Query<GetThreadParams>,+) -> Result<Response, GetThreadError> {+ get_thread_handler_inner(ctx, user_context, link, thread_id, query_params)+ .await+ .inspect_err(|e| e.log_if_needed())+}++async fn get_thread_handler_inner<U: EmailService>(+ ctx: ApiContext,+ user_context: Extension<UserContext>,+ link: OptionalEmailLinkExtractor<U>,+ thread_id: Uuid,+ query_params: GetThreadParams,) -> Result<Response, GetThreadError> {let link = link.0;- let p = process_get_thread_params(&query_params);+ let p = process_get_thread_params(&query_params)?;let mut thread = email_db_client::threads::get::fetch_thread_with_messages_paginated(&ctx.db, thread_id, p.offset, p.limit,).await- .context("Failed to fetch thread with messages")?;-- // Check if thread was found- if thread.db_id.is_none() {- return Err(GetThreadError::ThreadNotFound);- }+ .context("Failed to fetch thread with messages")?+ .ok_or(GetThreadError::ThreadNotFound)?;// Check if user owns the thread via their linklet is_owner = link@@ -202,7 +231,7 @@ pub async fn get_thread_messages_handler(Path(PathParams { id }): Path<PathParams>,extract::Query(query_params): extract::Query<GetThreadParams>,) -> Result<Response, GetThreadError> {- let p = process_get_thread_params(&query_params);+ let p = process_get_thread_params(&query_params)?;let messages =email_db_client::messages::get_parsed::get_paginated_parsed_messages_by_thread_id(@@ -224,13 +253,33 @@ pub async fn get_thread_messages_handler(}/// Extracts pagination parameters from query params, using defaults when not specified-fn process_get_thread_params(params: &GetThreadParams) -> GetThreadPaginationParams {- GetThreadPaginationParams {- offset: params.offset.filter(|&offset| offset >= 0).unwrap_or(0),-- limit: params- .limit- .filter(|&limit| 0 < limit && limit <= MESSAGE_MAX)- .unwrap_or(DEFAULT_MESSAGE_LIMIT),+fn process_get_thread_params(+ params: &GetThreadParams,+) -> Result<GetThreadPaginationParams, GetThreadError> {+ if let Some(offset) = params.offset+ && offset < 0+ {+ return Err(GetThreadError::ValidationError(+ "offset must be non-negative".to_string(),+ ));+ }++ if let Some(limit) = params.limit {+ if limit <= 0 {+ return Err(GetThreadError::ValidationError(+ "limit must be positive".to_string(),+ ));+ }+ if limit > MESSAGE_MAX {+ return Err(GetThreadError::ValidationError(format!(+ "limit must not exceed {}",+ MESSAGE_MAX+ )));+ }}++ Ok(GetThreadPaginationParams {+ offset: params.offset.unwrap_or(0),+ limit: params.limit.unwrap_or(DEFAULT_MESSAGE_LIMIT),+ })}