Skip to content

Commit 3b73384

Browse files
committed
refactor: create custom exception for sqs errors
1 parent b3a7da1 commit 3b73384

3 files changed

Lines changed: 51 additions & 14 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir PHP Runtime.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Runtime\Exception;
15+
16+
/**
17+
* Exception thrown when an SQS record fails to be processed.
18+
*/
19+
class SqsRecordProcessingException extends \RuntimeException implements ExceptionInterface
20+
{
21+
/**
22+
* Constructor.
23+
*/
24+
public function __construct(string $message, string $error = '')
25+
{
26+
$fullMessage = $message;
27+
28+
if (!empty($error)) {
29+
$fullMessage .= sprintf(': %s', $error);
30+
}
31+
32+
parent::__construct($fullMessage);
33+
}
34+
}

src/Lambda/Handler/Sqs/LaravelSqsHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Ymir\Runtime\Lambda\Handler\Sqs;
1515

1616
use Symfony\Component\Process\Process;
17+
use Ymir\Runtime\Exception\SqsRecordProcessingException;
1718
use Ymir\Runtime\Lambda\InvocationEvent\InvocationContext;
1819
use Ymir\Runtime\Lambda\InvocationEvent\SqsRecord;
1920
use Ymir\Runtime\Logger;
@@ -45,7 +46,7 @@ protected function processRecord(InvocationContext $context, SqsRecord $record):
4546
$message = json_encode($record);
4647

4748
if (empty($message) || JSON_ERROR_NONE !== json_last_error()) {
48-
throw new \RuntimeException(sprintf('Failed to encode SQS message [%s]: %s', $record->getMessageId(), json_last_error_msg()));
49+
throw new SqsRecordProcessingException(sprintf('Failed to encode SQS message [%s]', $record->getMessageId()), json_last_error_msg());
4950
}
5051

5152
$arguments = [
@@ -68,7 +69,7 @@ protected function processRecord(InvocationContext $context, SqsRecord $record):
6869
$process->run();
6970

7071
if (!$process->isSuccessful()) {
71-
throw new \RuntimeException(sprintf('Laravel queue job failed: %s', $process->getErrorOutput()));
72+
throw new SqsRecordProcessingException('Laravel queue job failed', $process->getErrorOutput());
7273
}
7374
}
7475

tests/Unit/Lambda/Handler/Sqs/LaravelSqsHandlerTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ protected function tearDown(): void
4343
$this->filesystem->remove($this->tempDir);
4444
}
4545

46-
public function testHandleThrowsExceptionForWrongEvent(): void
47-
{
48-
$this->expectException(InvalidHandlerEventException::class);
49-
$this->expectExceptionMessageMatches('/LaravelSqsHandler cannot handle Mock_InvocationEventInterface[^\s]* event/');
50-
51-
$handler = new LaravelSqsHandler($this->getLoggerMock(), $this->tempDir);
52-
53-
$handler->handle($this->getInvocationEventInterfaceMock());
54-
}
55-
5646
public function testCanHandleReturnsFalseForWrongEvent(): void
5747
{
5848
$handler = new LaravelSqsHandler($this->getLoggerMock(), '/tmp');
@@ -80,7 +70,8 @@ public function testHandleCollectsFailuresIfProcessFails(): void
8070

8171
$logger = $this->getLoggerMock();
8272
$logger->expects($this->once())
83-
->method('error');
73+
->method('error')
74+
->with($this->stringContains('Processing SQS message [id1] failed: Laravel queue job failed'));
8475

8576
$handler = new LaravelSqsHandler($logger, '/tmp');
8677
$response = $handler->handle($event);
@@ -102,7 +93,8 @@ public function testHandleCollectsFailuresOnJsonError(): void
10293

10394
$logger = $this->getLoggerMock();
10495
$logger->expects($this->once())
105-
->method('error');
96+
->method('error')
97+
->with($this->stringContains('Processing SQS message [id1] failed: Failed to encode SQS message [id1]'));
10698

10799
$handler = new LaravelSqsHandler($logger, '/tmp');
108100
$response = $handler->handle($event);
@@ -114,6 +106,16 @@ public function testHandleCollectsFailuresOnJsonError(): void
114106
], $response->getResponseData());
115107
}
116108

109+
public function testHandleThrowsExceptionForWrongEvent(): void
110+
{
111+
$this->expectException(InvalidHandlerEventException::class);
112+
$this->expectExceptionMessageMatches('/LaravelSqsHandler cannot handle Mock_InvocationEventInterface[^\s]* event/');
113+
114+
$handler = new LaravelSqsHandler($this->getLoggerMock(), $this->tempDir);
115+
116+
$handler->handle($this->getInvocationEventInterfaceMock());
117+
}
118+
117119
public function testHandleUsesEnvironmentVariables(): void
118120
{
119121
$_ENV['YMIR_QUEUE_CONNECTION'] = 'custom_connection';

0 commit comments

Comments
 (0)