diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
| commit | 8abfce2091e9665af93a920aed246e3b04abc2aa (patch) | |
| tree | bd72ace84cdb6716170bb71fc7d7fbb45c5fb33d /src | |
| parent | c540b7ac4dedb94cffe9aaeae9f936aba2a36d79 (diff) | |
throw an exception when route handler does not return response
Diffstat (limited to 'src')
| -rw-r--r-- | src/BasicStrategy.php | 9 | ||||
| -rw-r--r-- | src/Exceptions/InvalidResponseException.php | 12 |
2 files changed, 20 insertions, 1 deletions
diff --git a/src/BasicStrategy.php b/src/BasicStrategy.php index 62bc48e..5043576 100644 --- a/src/BasicStrategy.php +++ b/src/BasicStrategy.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Lightscale\Router; use Lightscale\Router\Contracts\Strategy; +use Lightscale\Router\Exceptions\InvalidResponseException; use Lightscale\Router\Exceptions\NotFoundException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -13,7 +14,13 @@ class BasicStrategy implements Strategy { public function runRoute(RouteCall $call): ResponseInterface { - return ($call->route->getHandler())($call); + $result = ($call->route->getHandler())($call); + + if ($result instanceof ResponseInterface) { + return $result; + } + + throw new InvalidResponseException(); } public function notFound(RequestInterface $request): ResponseInterface diff --git a/src/Exceptions/InvalidResponseException.php b/src/Exceptions/InvalidResponseException.php new file mode 100644 index 0000000..98577a1 --- /dev/null +++ b/src/Exceptions/InvalidResponseException.php @@ -0,0 +1,12 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router\Exceptions; + +use RuntimeException; + +class InvalidResponseException extends RuntimeException +{ + protected $message = 'Does not return an instance of ResponseInterface'; +} |
