diff --git a/src/ClusterProxy.php b/src/ClusterProxy.php index 5b149c7..5812c00 100644 --- a/src/ClusterProxy.php +++ b/src/ClusterProxy.php @@ -122,6 +122,13 @@ static function ( ProvidesResponseData $response ) } ); + $request->addFailureCallbacks( + static function ( Throwable $e ) + { + throw $e; + } + ); + $this->proxy->sendAsyncRequest( $request ); } diff --git a/src/Responses/PhpFpmStatusResponse.php b/src/Responses/PhpFpmStatusResponse.php index aa34c55..b489b36 100644 --- a/src/Responses/PhpFpmStatusResponse.php +++ b/src/Responses/PhpFpmStatusResponse.php @@ -9,6 +9,7 @@ use hollodotme\FastCGI\Interfaces\ProvidesServerStatus; use hollodotme\FastCGI\Responses\PhpFpm\Process; use hollodotme\FastCGI\Responses\PhpFpm\Status; +use RuntimeException; use function array_filter; use function array_shift; use function explode; @@ -33,6 +34,7 @@ final class PhpFpmStatusResponse implements ProvidesServerStatus * @param ProvidesResponseData $response * @param ConfiguresSocketConnection $connection * + * @throws RuntimeException * @throws Exception */ public function __construct( ProvidesResponseData $response, ConfiguresSocketConnection $connection ) @@ -41,9 +43,24 @@ public function __construct( ProvidesResponseData $response, ConfiguresSocketCon $this->connection = $connection; $this->processes = []; + $this->guardResponseIsValid(); $this->parseBody(); } + /** + * @throws RuntimeException + */ + private function guardResponseIsValid() : void + { + if ( 'File not found.' === trim( $this->response->getBody() ) ) + { + throw new RuntimeException( + "Could not find server's status path." + . " Please check for typos and if the status endpoint is enabled in your server's config." + ); + } + } + /** * @throws Exception */ diff --git a/tests/Integration/ClusterStatusTest.php b/tests/Integration/ClusterStatusTest.php index cfd2c0d..1a3797e 100644 --- a/tests/Integration/ClusterStatusTest.php +++ b/tests/Integration/ClusterStatusTest.php @@ -14,6 +14,7 @@ use hollodotme\FastCGI\Tests\Traits\SocketDataProviding; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; +use RuntimeException; use SebastianBergmann\RecursionContext\InvalidArgumentException; use Throwable; @@ -55,6 +56,7 @@ protected function tearDown() : void */ public function testGetStatus() : void { + /** @var array|PhpFpmStatusResponse[] $statusResponses */ $statusResponses = $this->clusterProxy->getStatus( '/status', PhpFpmStatusResponse::class @@ -70,4 +72,28 @@ public function testGetStatus() : void $this->assertSame( $expectedPoolNames, $poolNames ); } + + /** + * @throws ConnectException + * @throws ReadFailedException + * @throws Throwable + * @throws TimedoutException + * @throws WriteFailedException + */ + public function testThrowsExceptionIfStatusEndpointCannotBeFound() : void + { + $this->expectException( RuntimeException::class ); + $this->expectExceptionMessage( + "Could not find server's status path." + . " Please check for typos and if the status endpoint is enabled in your server's config." + ); + + /** @noinspection UnusedFunctionResultInspection */ + $this->clusterProxy->getStatus( + '/not-existing-status-endpoint', + PhpFpmStatusResponse::class + ); + + $this->fail( 'Expected runtime exception to be thrown.' ); + } }