Skip to content

Commit b7a3af4

Browse files
author
palPalani
committed
Error handling for invalid payloads
1 parent 7e5277b commit b7a3af4

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/Sqs/Queue.php

+25-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Queue\Jobs\SqsJob;
99
use Illuminate\Queue\SqsQueue;
1010
use Illuminate\Support\Facades\Config;
11+
use Illuminate\Support\Facades\Log;
1112
use Illuminate\Support\Str;
1213
use JsonException;
1314
use palPalani\SqsQueueReader\Jobs\DispatcherJob;
@@ -119,13 +120,11 @@ private function modifySinglePayload(array | string $payload, string $class): ar
119120

120121
$body = \json_decode($payload['Body'], true, 512, JSON_THROW_ON_ERROR);
121122

122-
$body = [
123+
$payload['Body'] = \json_encode([
123124
'uuid' => (string) Str::uuid(),
124125
'job' => $class . '@handle',
125126
'data' => $body['data'] ?? $body,
126-
];
127-
128-
$payload['Body'] = \json_encode($body, JSON_THROW_ON_ERROR);
127+
], JSON_THROW_ON_ERROR);
129128

130129
return $payload;
131130
}
@@ -148,29 +147,33 @@ private function modifyMultiplePayload(array | string $payload, string $class):
148147
$receiptHandle = null;
149148

150149
foreach ($payload as $k => $item) {
151-
$body[$k] = [
152-
'messages' => \json_decode($item['Body'], true, 512, JSON_THROW_ON_ERROR),
153-
'attributes' => $item['Attributes'],
154-
'batchIds' => [
155-
'Id' => $item['MessageId'],
156-
'ReceiptHandle' => $item['ReceiptHandle'],
157-
],
158-
];
159-
$attributes = $item['Attributes'];
160-
$messageId = $item['MessageId'];
161-
$receiptHandle = $item['ReceiptHandle'];
150+
try {
151+
$body[$k] = [
152+
'messages' => \json_decode($item['Body'], true, 512, JSON_THROW_ON_ERROR),
153+
'attributes' => $item['Attributes'],
154+
'batchIds' => [
155+
'Id' => $item['MessageId'],
156+
'ReceiptHandle' => $item['ReceiptHandle'],
157+
],
158+
];
159+
$attributes = $item['Attributes'];
160+
$messageId = $item['MessageId'];
161+
$receiptHandle = $item['ReceiptHandle'];
162+
} catch (JsonException $e) {
163+
Log::warning('Invalid payload!', [$item]);
164+
165+
continue;
166+
}
162167
}
163168

164-
$body = [
165-
'uuid' => (string) Str::uuid(),
166-
'job' => $class . '@handle',
167-
'data' => $body,
168-
];
169-
170169
return [
171170
'MessageId' => $messageId,
172171
'ReceiptHandle' => $receiptHandle,
173-
'Body' => \json_encode($body, JSON_THROW_ON_ERROR),
172+
'Body' => \json_encode([
173+
'uuid' => (string) Str::uuid(),
174+
'job' => $class . '@handle',
175+
'data' => $body,
176+
], JSON_THROW_ON_ERROR),
174177
'Attributes' => $attributes,
175178
];
176179
}

0 commit comments

Comments
 (0)