Skip to content

Commit 2c948a8

Browse files
author
Harry Bragg
authored
add more logging to the failed acknowledge/enqueue exceptions (#47)
* add more logging to the failed acknowledge/reject/whatever exceptions * camelCase
1 parent d469f2a commit 2c948a8

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

src/Adapter/SqsAdapter.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function __construct(SqsClient $client, $name, array $options = [])
8686
public function acknowledge(array $messages)
8787
{
8888
$url = $this->getQueueUrl();
89-
$failed = [];
89+
$errors = [];
9090
$batches = array_chunk($this->createDeleteEntries($messages), self::BATCHSIZE_DELETE);
9191

9292
foreach ($batches as $batch) {
@@ -95,15 +95,15 @@ public function acknowledge(array $messages)
9595
'Entries' => $batch,
9696
]);
9797

98-
$map = function ($result) use ($messages) {
99-
return $messages[$result['Id']];
100-
};
101-
102-
$failed = array_merge($failed, array_map($map, $results->get('Failed') ?: []));
98+
$errors = array_merge($errors, $results->get('Failed') ?: []);
10399
}
100+
$map = function ($result) use ($messages) {
101+
return $messages[$result['Id']];
102+
};
103+
$failed = array_map($map, $errors);
104104

105105
if (!empty($failed)) {
106-
throw new FailedAcknowledgementException($this, $failed);
106+
throw new FailedAcknowledgementException($this, $failed, $errors);
107107
}
108108
}
109109

@@ -115,7 +115,7 @@ public function acknowledge(array $messages)
115115
public function reject(array $messages)
116116
{
117117
$url = $this->getQueueUrl();
118-
$failed = [];
118+
$errors = [];
119119
$batches = array_chunk($this->createRejectEntries($messages), self::BATCHSIZE_DELETE);
120120

121121
foreach ($batches as $batch) {
@@ -124,15 +124,15 @@ public function reject(array $messages)
124124
'Entries' => $batch,
125125
]);
126126

127-
$map = function ($result) use ($messages) {
128-
return $messages[$result['Id']];
129-
};
130-
131-
$failed = array_merge($failed, array_map($map, $results->get('Failed') ?: []));
127+
$errors = array_merge($errors, $results->get('Failed') ?: []);
132128
}
129+
$map = function ($result) use ($messages) {
130+
return $messages[$result['Id']];
131+
};
132+
$failed = array_map($map, $errors);
133133

134134
if (!empty($failed)) {
135-
throw new FailedAcknowledgementException($this, $failed);
135+
throw new FailedAcknowledgementException($this, $failed, $errors);
136136
}
137137
}
138138

@@ -193,7 +193,7 @@ public function dequeue(MessageFactoryInterface $factory, $limit)
193193
public function enqueue(array $messages)
194194
{
195195
$url = $this->getQueueUrl();
196-
$failed = [];
196+
$errors = [];
197197
$batches = array_chunk($this->createEnqueueEntries($messages), self::BATCHSIZE_SEND);
198198

199199
foreach ($batches as $batch) {
@@ -202,15 +202,15 @@ public function enqueue(array $messages)
202202
'Entries' => $batch,
203203
]);
204204

205-
$map = function ($result) use ($messages) {
206-
return $messages[$result['Id']];
207-
};
208-
209-
$failed = array_merge($failed, array_map($map, $results->get('Failed') ?: []));
205+
$errors = array_merge($errors, $results->get('Failed') ?: []);
210206
}
207+
$map = function ($result) use ($messages) {
208+
return $messages[$result['Id']];
209+
};
210+
$failed = array_map($map, $errors);
211211

212212
if (!empty($failed)) {
213-
throw new FailedEnqueueException($this, $failed);
213+
throw new FailedEnqueueException($this, $failed, $errors);
214214
}
215215
}
216216

tests/unit/Adapter/SqsAdapterTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Aws\ResultInterface;
1919
use Aws\Sqs\SqsClient;
2020
use Graze\DataStructure\Container\ContainerInterface;
21+
use Graze\Queue\Adapter\Exception\FailedAcknowledgementException;
2122
use Graze\Queue\Message\MessageFactoryInterface;
2223
use Graze\Queue\Message\MessageInterface;
2324
use Mockery as m;
@@ -138,6 +139,43 @@ public function testAcknowledge()
138139
$adapter->acknowledge($this->messages);
139140
}
140141

142+
public function testFailureToAcknowledgeForSomeMessages()
143+
{
144+
$adapter = new SqsAdapter($this->client, 'foo');
145+
$url = $this->stubCreateQueue('foo');
146+
147+
$this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');
148+
$this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar');
149+
$this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz');
150+
151+
$this->model->shouldReceive('get')->once()->with('Failed')->andReturn([
152+
['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone'],
153+
]);
154+
155+
$this->client->shouldReceive('deleteMessageBatch')->once()->with([
156+
'QueueUrl' => $url,
157+
'Entries' => [
158+
['Id' => 0, 'ReceiptHandle' => 'foo'],
159+
['Id' => 1, 'ReceiptHandle' => 'bar'],
160+
['Id' => 2, 'ReceiptHandle' => 'baz'],
161+
],
162+
])->andReturn($this->model);
163+
164+
$errorThrown = false;
165+
try {
166+
$adapter->acknowledge($this->messages);
167+
} catch (FailedAcknowledgementException $e) {
168+
assertThat($e->getMessages(), is(anArray([$this->messageC])));
169+
assertThat(
170+
$e->getDebug(),
171+
is(anArray([['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone']]))
172+
);
173+
$errorThrown = true;
174+
}
175+
176+
assertThat('an exception is thrown', $errorThrown);
177+
}
178+
141179
public function testReject()
142180
{
143181
$adapter = new SqsAdapter($this->client, 'foo');

0 commit comments

Comments
 (0)