diff --git a/src/Mono.php b/src/Mono.php index 1a5a039..8438deb 100644 --- a/src/Mono.php +++ b/src/Mono.php @@ -100,8 +100,11 @@ public function run(): void default => $this->createResponse('500 Internal Server Error', 500) }; - if ($response instanceof ResponseInterface) { - (new SapiEmitter())->emit($response); + if (!$response instanceof ResponseInterface) { + throw new \RuntimeException('Invalid response received from route ' . $request->getUri()->getPath() . + '. Please return a valid PSR-7 response from your handler.'); } + + (new SapiEmitter())->emit($response); } } diff --git a/tests/MonoTest.php b/tests/MonoTest.php index 9b807d1..0235e95 100644 --- a/tests/MonoTest.php +++ b/tests/MonoTest.php @@ -83,8 +83,26 @@ public function testDependencyInjection(): void $demoClass = $mono->get(NodeTraverser::class); $this->assertInstanceOf(NodeTraverser::class, $demoClass); + + return $mono->createResponse('OK'); }); $mono->run(); } + + public function testInvalidResponseThrowError(): void + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = '/'; + + $mono = new Mono(__DIR__); + + $mono->addRoute('GET', '/', function () { + return 'OK'; + }); + + $this->expectException(\RuntimeException::class); + + $mono->run(); + } }