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.

2.3 KB
Raw Blame
use crate::pubsub::gmail_ops::worker::GmailOpsContext;
use models_email::gmail::gmail_ops::GmailOpsPubsubMessage;
use models_email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
use sqs_worker::cleanup_message;

/// Handles non-retryable errors by cleaning up the SQS message.
#[tracing::instrument(skip(ctx, message), err)]
pub async fn handle_non_retryable_error(
    ctx: &GmailOpsContext,
    message: &aws_sdk_sqs::types::Message,
    data: &GmailOpsPubsubMessage,
    e: &DetailedError,
) -> anyhow::Result<()> {
    // A deferred operation is only non-retryable here once it has been
    // re-enqueued to the retry queue, so deleting it loses nothing.
    if matches!(
        e.reason,
        FailureReason::GmailApiRateLimited
            | FailureReason::GmailApiUnavailable
            | FailureReason::NewerLabelWritePending
    ) {
        tracing::debug!(error = ?e, payload = ?data.operation, "Deferred gmail ops message moved to the retry queue. The message will be deleted.");
    } else {
        tracing::error!(error = ?e, payload = ?data.operation, "Non-retryable error processing gmail ops message. The message will be deleted.");
    }

    cleanup_message(&ctx.sqs_worker, message).await?;
    Ok(())
}

/// Handles retryable errors by leaving the message in the queue.
#[tracing::instrument(skip(data, e), err)]
pub async fn handle_retryable_error(
    data: &GmailOpsPubsubMessage,
    e: &DetailedError,
) -> anyhow::Result<()> {
    tracing::debug!(error = ?e, payload = ?data.operation, "Retryable error processing gmail ops message.");

    Ok(())
}

/// Adds an operation name prefix to a ProcessingError's source field.
pub fn prefix_error_source(error: ProcessingError, operation_name: &str) -> ProcessingError {
    match error {
        ProcessingError::Retryable(DetailedError { reason, source }) => {
            ProcessingError::Retryable(DetailedError {
                reason,
                source: anyhow::anyhow!("{}: {}", operation_name, source),
            })
        }
        ProcessingError::NonRetryable(DetailedError { reason, source }) => {
            ProcessingError::NonRetryable(DetailedError {
                reason,
                source: anyhow::anyhow!("{}: {}", operation_name, source),
            })
        }
    }
}