Skip to content

Commit 1b14aa4

Browse files
committed
fixup! feat: add DeleteNBatch method
as per review, simplifying error handling
1 parent 5d32eab commit 1b14aa4

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

aws/sqs/sqs.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,17 @@ func (d *DeleteBatchError) Error() string {
416416
return fmt.Sprintf("%v: %v messages failed to delete", d.Err, len(d.Info))
417417
}
418418

419+
type DeleteNBatchError struct {
420+
Errors []error
421+
Info []DeleteBatchErrorEntry
419422
}
420423

421-
func (d *DeleteBatchError) Info() []DeleteBatchErrorInfo {
422-
return d.info
424+
func (s *DeleteNBatchError) Error() string {
425+
var allErrors string
426+
for _, err := range s.Errors {
427+
allErrors += fmt.Sprintf("%s\n", err.Error())
428+
}
429+
return fmt.Sprintf("error(s) deleting batches:\n%s", allErrors)
423430
}
424431

425432
// DeleteBatch deletes multiple messages from an SQS queue in a single batch
@@ -471,7 +478,9 @@ func (s *SQS) DeleteNBatch(ctx context.Context, queueURL string, receiptHandles
471478
times = int(math.Ceil(float64(receiptCount) / float64(maxlen)))
472479
)
473480

474-
info := make([]DeleteBatchErrorInfo, 0)
481+
allErrors := make([]error, 0)
482+
allInfo := make([]DeleteBatchErrorEntry, 0)
483+
475484
batchesDeleted := 0
476485

477486
for i := 0; i < times; i++ {
@@ -484,14 +493,16 @@ func (s *SQS) DeleteNBatch(ctx context.Context, queueURL string, receiptHandles
484493
err := s.DeleteBatch(ctx, queueURL, receipt_batch)
485494
var dbe *DeleteBatchError
486495
if errors.As(err, &dbe) {
487-
info = append(info, dbe.Info()...)
496+
allErrors = append(allErrors, err)
497+
allInfo = append(allInfo, dbe.Info...)
488498
}
489499
batchesDeleted++
490500
}
491501

492-
if len(info) > 0 {
493-
return batchesDeleted, &DeleteBatchError{
494-
info: info,
502+
if len(allErrors) > 0 {
503+
return batchesDeleted, &DeleteNBatchError{
504+
Errors: allErrors,
505+
Info: allInfo,
495506
}
496507
}
497508
return batchesDeleted, nil

aws/sqs/sqs_integration_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,17 @@ func TestDeleteNBatch(t *testing.T) {
822822
for _, rm := range receivedMessages {
823823
receiptHandles = append(receiptHandles, rm.ReceiptHandle)
824824
}
825-
receiptHandles[0] = "invalid-receipt-handle" // Replace a valid receipt handle with an invalid one.
825+
invalidReceiptHandle := "invalid-receipt-handle"
826+
receiptHandles[0] = invalidReceiptHandle // Replace a valid receipt handle with an invalid one.
826827

827828
// ACTION
828829
batchesDeleted, err = client.DeleteNBatch(context.TODO(), awsCmdQueueURL(), receiptHandles)
829830
assert.NotNil(t, err)
830831

831-
var dbe *DeleteBatchError
832+
var dbe *DeleteNBatchError
832833
if errors.As(err, &dbe) {
833-
assert.Equal(t, 1, len(dbe.Info()))
834+
assert.Equal(t, 1, len(dbe.Info))
835+
assert.Equal(t, invalidReceiptHandle, dbe.Info[0].ReceiptHandle)
834836
} else {
835837
t.Error("unexpected error type")
836838
}

0 commit comments

Comments
 (0)