Skip to content

Commit ecdc5c6

Browse files
committed
chore: better docs
1 parent a80c1e5 commit ecdc5c6

File tree

1 file changed

+48
-1
lines changed
  • lambda-events/src/event/sqs

1 file changed

+48
-1
lines changed

lambda-events/src/event/sqs/mod.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,54 @@ pub struct SqsBatchResponse {
155155
}
156156

157157
impl SqsBatchResponse {
158-
/// Add a failed message ID to the batch response
158+
/// Add a failed message ID to the batch response.
159+
///
160+
/// When processing SQS messages in batches, you can use this helper method to
161+
/// register individual message failures. Lambda will automatically return failed
162+
/// messages to the queue for reprocessing while successfully processed messages
163+
/// will be deleted.
164+
///
165+
/// **Important**: This feature requires `FunctionResponseTypes: ReportBatchItemFailures`
166+
/// to be enabled in your Lambda function's SQS event source mapping configuration.
167+
/// Without this setting, Lambda will retry the entire batch on any failure.
168+
///
169+
/// # Example
170+
///
171+
/// ```rust,no_run
172+
/// use aws_lambda_events::event::sqs::{SqsEvent, SqsBatchResponse};
173+
/// use lambda_runtime::{service_fn, Error, LambdaEvent};
174+
///
175+
/// async fn function_handler(
176+
/// event: LambdaEvent<SqsEvent>,
177+
/// ) -> Result<SqsBatchResponse, Error> {
178+
/// // Start from a default response
179+
/// let mut response = SqsBatchResponse::default();
180+
///
181+
/// for record in event.payload.records {
182+
/// let message_id = record.message_id.clone().unwrap_or_default();
183+
///
184+
/// // Try to process the message
185+
/// if let Err(e) = process_record(&record).await {
186+
/// println!("Failed to process message {}: {}", message_id, e);
187+
///
188+
/// // Use the helper to register the failure
189+
/// response.add_failure(message_id);
190+
/// }
191+
/// }
192+
///
193+
/// Ok(response)
194+
/// }
195+
///
196+
/// async fn process_record(record: &aws_lambda_events::event::sqs::SqsMessage) -> Result<(), Error> {
197+
/// // Your message processing logic here
198+
/// Ok(())
199+
/// }
200+
///
201+
/// #[tokio::main]
202+
/// async fn main() -> Result<(), Error> {
203+
/// lambda_runtime::run(service_fn(function_handler)).await
204+
/// }
205+
/// ```
159206
pub fn add_failure(&mut self, message_id: String) {
160207
self.batch_item_failures.push(BatchItemFailure {
161208
item_identifier: message_id,

0 commit comments

Comments
 (0)