Skip to content

Commit f9e983c

Browse files
committed
refactor: create custom exception for when a handler receives a wrong event type
1 parent b09f90b commit f9e983c

10 files changed

Lines changed: 53 additions & 13 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
use Ymir\Runtime\Lambda\Handler\LambdaEventHandlerInterface;
17+
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
18+
19+
/**
20+
* Exception thrown when a handler receives an event it cannot handle.
21+
*/
22+
class InvalidHandlerEventException extends \InvalidArgumentException implements ExceptionInterface
23+
{
24+
/**
25+
* Constructor.
26+
*/
27+
public function __construct(LambdaEventHandlerInterface $handler, InvocationEventInterface $event)
28+
{
29+
parent::__construct(sprintf('%s cannot handle %s event', (new \ReflectionClass($handler))->getShortName(), (new \ReflectionClass($event))->getShortName()));
30+
}
31+
}

src/Lambda/Handler/ConsoleCommandLambdaEventHandler.php

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

1616
use Symfony\Component\Process\Process;
17+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1718
use Ymir\Runtime\Lambda\InvocationEvent\ConsoleCommandEvent;
1819
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
1920
use Ymir\Runtime\Lambda\Response\ProcessResponse;
@@ -54,7 +55,7 @@ public function canHandle(InvocationEventInterface $event): bool
5455
public function handle(InvocationEventInterface $event): ResponseInterface
5556
{
5657
if (!$event instanceof ConsoleCommandEvent) {
57-
throw new \InvalidArgumentException('ConsoleCommandLambdaEventHandler can only handle ConsoleCommandEvent objects');
58+
throw new InvalidHandlerEventException($this, $event);
5859
}
5960

6061
$process = Process::fromShellCommandline("{$event->getCommand()} 2>&1");

src/Lambda/Handler/Http/AbstractHttpRequestEventHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Ymir\Runtime\Lambda\Handler\Http;
1515

16+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1617
use Ymir\Runtime\Lambda\Handler\LambdaEventHandlerInterface;
1718
use Ymir\Runtime\Lambda\InvocationEvent\HttpRequestEvent;
1819
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
@@ -55,7 +56,7 @@ public function canHandle(InvocationEventInterface $event): bool
5556
public function handle(InvocationEventInterface $event): ResponseInterface
5657
{
5758
if (!$event instanceof HttpRequestEvent || !$this->canHandle($event)) {
58-
throw new \InvalidArgumentException(sprintf('%s cannot handle the given invocation event object', (new \ReflectionClass(static::class))->getShortName()));
59+
throw new InvalidHandlerEventException($this, $event);
5960
}
6061

6162
$filePath = $this->getEventFilePath($event);

src/Lambda/Handler/PingLambdaEventHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Ymir\Runtime\Lambda\Handler;
1515

16+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1617
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
1718
use Ymir\Runtime\Lambda\InvocationEvent\PingEvent;
1819
use Ymir\Runtime\Lambda\Response\Http\HttpResponse;
@@ -37,7 +38,7 @@ public function canHandle(InvocationEventInterface $event): bool
3738
public function handle(InvocationEventInterface $event): ResponseInterface
3839
{
3940
if (!$event instanceof PingEvent) {
40-
throw new \InvalidArgumentException('PingLambdaEventHandler can only handle PingEvent objects');
41+
throw new InvalidHandlerEventException($this, $event);
4142
}
4243

4344
usleep(50 * 1000);

src/Lambda/Handler/Sqs/AbstractSqsHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Ymir\Runtime\Lambda\Handler\Sqs;
1515

16+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1617
use Ymir\Runtime\Lambda\Handler\LambdaEventHandlerInterface;
1718
use Ymir\Runtime\Lambda\InvocationEvent\InvocationContext;
1819
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
@@ -56,7 +57,7 @@ public function canHandle(InvocationEventInterface $event): bool
5657
public function handle(InvocationEventInterface $event): ResponseInterface
5758
{
5859
if (!$event instanceof SqsEvent || !$this->canHandle($event)) {
59-
throw new \InvalidArgumentException(sprintf('%s cannot handle the given invocation event object', (new \ReflectionClass(static::class))->getShortName()));
60+
throw new InvalidHandlerEventException($this, $event);
6061
}
6162

6263
$failedRecords = [];

src/Lambda/Handler/WarmUpEventHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use AsyncAws\Lambda\LambdaClient;
1717
use Ymir\Runtime\Exception\InvalidConfigurationException;
18+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1819
use Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface;
1920
use Ymir\Runtime\Lambda\InvocationEvent\WarmUpEvent;
2021
use Ymir\Runtime\Lambda\Response\Http\HttpResponse;
@@ -63,7 +64,7 @@ public function canHandle(InvocationEventInterface $event): bool
6364
public function handle(InvocationEventInterface $event): ResponseInterface
6465
{
6566
if (!$event instanceof WarmUpEvent) {
66-
throw new \InvalidArgumentException('WarmUpEventHandler can only handle WarmUpEvent objects');
67+
throw new InvalidHandlerEventException($this, $event);
6768
}
6869

6970
$concurrency = $event->getConcurrency();

tests/Unit/Lambda/Handler/ConsoleCommandLambdaEventHandlerTest.php

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

1616
use PHPUnit\Framework\TestCase;
17+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1718
use Ymir\Runtime\Lambda\Handler\ConsoleCommandLambdaEventHandler;
1819
use Ymir\Runtime\Lambda\Response\ProcessResponse;
1920
use Ymir\Runtime\Tests\Mock\ConsoleCommandEventMockTrait;
@@ -139,8 +140,8 @@ public function testHandleWithUnsuccessfulCommand(): void
139140

140141
public function testHandleWithWrongEventType(): void
141142
{
142-
$this->expectException(\InvalidArgumentException::class);
143-
$this->expectExceptionMessage('ConsoleCommandLambdaEventHandler can only handle ConsoleCommandEvent objects');
143+
$this->expectException(InvalidHandlerEventException::class);
144+
$this->expectExceptionMessageMatches('/ConsoleCommandLambdaEventHandler cannot handle Mock_InvocationEventInterface[^\s]* event/');
144145

145146
(new ConsoleCommandLambdaEventHandler($this->getLoggerMock()))->handle($this->getInvocationEventInterfaceMock());
146147
}

tests/Unit/Lambda/Handler/Http/AbstractHttpRequestEventHandlerTest.php

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

1616
use PHPUnit\Framework\TestCase;
17+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1718
use Ymir\Runtime\Lambda\Handler\Http\AbstractHttpRequestEventHandler;
1819
use Ymir\Runtime\Lambda\Response\Http\StaticFileHttpResponse;
1920
use Ymir\Runtime\Tests\Mock\HttpRequestEventMockTrait;
@@ -78,8 +79,8 @@ public function testHandleReturnsStaticFileHttpResponse(): void
7879

7980
public function testHandleWithWrongEventType(): void
8081
{
81-
$this->expectException(\InvalidArgumentException::class);
82-
$this->expectExceptionMessageMatches('/[^\s]* cannot handle the given invocation event object/');
82+
$this->expectException(InvalidHandlerEventException::class);
83+
$this->expectExceptionMessageMatches('/[^\s]* cannot handle Mock_InvocationEventInterface[^\s]* event/');
8384

8485
$handler = $this->getMockForAbstractClass(AbstractHttpRequestEventHandler::class, ['/']);
8586

tests/Unit/Lambda/Handler/PingLambdaEventHandlerTest.php

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

1616
use PHPUnit\Framework\TestCase;
17+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1718
use Ymir\Runtime\Lambda\Handler\PingLambdaEventHandler;
1819
use Ymir\Runtime\Lambda\Response\Http\HttpResponse;
1920
use Ymir\Runtime\Tests\Mock\InvocationEventInterfaceMockTrait;
@@ -47,8 +48,8 @@ public function testHandleWithPingEvent(): void
4748

4849
public function testHandleWithWrongEventType(): void
4950
{
50-
$this->expectException(\InvalidArgumentException::class);
51-
$this->expectExceptionMessage('PingLambdaEventHandler can only handle PingEvent objects');
51+
$this->expectException(InvalidHandlerEventException::class);
52+
$this->expectExceptionMessageMatches('/PingLambdaEventHandler cannot handle Mock_InvocationEventInterface[^\s]* event/');
5253

5354
$handler = new PingLambdaEventHandler();
5455

tests/Unit/Lambda/Handler/WarmUpEventHandlerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use PHPUnit\Framework\TestCase;
1717
use Ymir\Runtime\Exception\InvalidConfigurationException;
18+
use Ymir\Runtime\Exception\InvalidHandlerEventException;
1819
use Ymir\Runtime\Lambda\Handler\WarmUpEventHandler;
1920
use Ymir\Runtime\Lambda\Response\Http\HttpResponse;
2021
use Ymir\Runtime\Tests\Mock\FunctionMockTrait;
@@ -124,8 +125,8 @@ public function testHandleWithNoEnvironmentVariable(): void
124125

125126
public function testHandleWithWrongEventType(): void
126127
{
127-
$this->expectException(\InvalidArgumentException::class);
128-
$this->expectExceptionMessage('WarmUpEventHandler can only handle WarmUpEvent objects');
128+
$this->expectException(InvalidHandlerEventException::class);
129+
$this->expectExceptionMessageMatches('/WarmUpEventHandler cannot handle Mock_InvocationEventInterface[^\s]* event/');
129130

130131
(new WarmUpEventHandler($this->getLambdaClientMock(), $this->getLoggerMock()))->handle($this->getInvocationEventInterfaceMock());
131132
}

0 commit comments

Comments
 (0)