Skip to content

Commit

Permalink
SQS - Improve delete_message_batch() error responses (#4765)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjakthol authored Jan 15, 2022
1 parent 8f63ce0 commit ca327e3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
32 changes: 26 additions & 6 deletions moto/sqs/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,26 @@ def delete_message_batch(self):
raise BatchEntryIdsNotDistinct(receipt_and_id["msg_user_id"])
receipt_seen.add(receipt)

success = []
errors = []
for receipt_and_id in receipts:
self.sqs_backend.delete_message(
queue_name, receipt_and_id["receipt_handle"]
)
try:
self.sqs_backend.delete_message(
queue_name, receipt_and_id["receipt_handle"]
)
success.append(receipt_and_id["msg_user_id"])
except ReceiptHandleIsInvalid:
errors.append(
{
"Id": receipt_and_id["msg_user_id"],
"SenderFault": "true",
"Code": "ReceiptHandleIsInvalid",
"Message": f'The input receipt handle "{receipt_and_id["receipt_handle"]}" is not a valid receipt handle.',
}
)

message_ids = [r["msg_user_id"] for r in receipts]
template = self.response_template(DELETE_MESSAGE_BATCH_RESPONSE)
return template.render(message_ids=message_ids)
return template.render(success=success, errors=errors)

def purge_queue(self):
queue_name = self._get_queue_name()
Expand Down Expand Up @@ -669,11 +681,19 @@ def list_queue_tags(self):

DELETE_MESSAGE_BATCH_RESPONSE = """<DeleteMessageBatchResponse>
<DeleteMessageBatchResult>
{% for message_id in message_ids %}
{% for message_id in success %}
<DeleteMessageBatchResultEntry>
<Id>{{ message_id }}</Id>
</DeleteMessageBatchResultEntry>
{% endfor %}
{% for error_dict in errors %}
<BatchResultErrorEntry>
<Id>{{ error_dict['Id'] }}</Id>
<Code>{{ error_dict['Code'] }}</Code>
<Message>{{ error_dict['Message'] }}</Message>
<SenderFault>{{ error_dict['SenderFault'] }}</SenderFault>
</BatchResultErrorEntry>
{% endfor %}
</DeleteMessageBatchResult>
<ResponseMetadata>
<RequestId></RequestId>
Expand Down
40 changes: 40 additions & 0 deletions tests/test_sqs/test_sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,46 @@ def test_delete_message_batch_with_duplicates():
assert messages, "message still in the queue"


@mock_sqs
def test_delete_message_batch_with_invalid_receipt_id():
client = boto3.client("sqs", region_name="us-east-1")
response = client.create_queue(QueueName=str(uuid4())[0:6])
queue_url = response["QueueUrl"]
client.send_message(QueueUrl=queue_url, MessageBody="coucou")

messages = client.receive_message(
QueueUrl=queue_url, WaitTimeSeconds=0, VisibilityTimeout=0
)["Messages"]
assert messages, "at least one msg"

# Try to delete the message from SQS but also include two invalid delete requests
entries = [
{"Id": "fake-receipt-handle-1", "ReceiptHandle": "fake-receipt-handle-1"},
{"Id": messages[0]["MessageId"], "ReceiptHandle": messages[0]["ReceiptHandle"]},
{"Id": "fake-receipt-handle-2", "ReceiptHandle": "fake-receipt-handle-2"},
]
response = client.delete_message_batch(QueueUrl=queue_url, Entries=entries)

assert response["Successful"] == [
{"Id": messages[0]["MessageId"]}
], "delete ok for real message"

assert response["Failed"] == [
{
"Id": "fake-receipt-handle-1",
"SenderFault": True,
"Code": "ReceiptHandleIsInvalid",
"Message": 'The input receipt handle "fake-receipt-handle-1" is not a valid receipt handle.',
},
{
"Id": "fake-receipt-handle-2",
"SenderFault": True,
"Code": "ReceiptHandleIsInvalid",
"Message": 'The input receipt handle "fake-receipt-handle-2" is not a valid receipt handle.',
},
]


@mock_sqs
def test_message_attributes_in_receive_message():
sqs = boto3.resource("sqs", region_name="us-east-1")
Expand Down

0 comments on commit ca327e3

Please sign in to comment.