diff --git a/src/Browser/Browser.php b/src/Browser/Browser.php index 9e9173f..13d0bac 100644 --- a/src/Browser/Browser.php +++ b/src/Browser/Browser.php @@ -11,6 +11,8 @@ namespace PlaywrightPHP\Browser; use PlaywrightPHP\Configuration\PlaywrightConfig; +use PlaywrightPHP\Exception\ProtocolErrorException; +use PlaywrightPHP\Exception\RuntimeException; use PlaywrightPHP\Page\PageInterface; use PlaywrightPHP\Transport\TransportInterface; @@ -42,7 +44,7 @@ public function __construct( public function context(): BrowserContextInterface { if (null === $this->defaultContext) { - throw new \RuntimeException('Default context is not available'); + throw new RuntimeException('Default context is not available'); } return $this->defaultContext; @@ -60,7 +62,7 @@ public function newContext(array $options = []): BrowserContextInterface ]); if (!is_string($response['contextId'])) { - throw new \RuntimeException('Invalid contextId returned from transport'); + throw new ProtocolErrorException('Invalid contextId returned from transport', 0); } $context = new BrowserContext($this->transport, $response['contextId'], $this->config); diff --git a/src/Browser/BrowserBuilder.php b/src/Browser/BrowserBuilder.php index de8c996..9ac6a66 100644 --- a/src/Browser/BrowserBuilder.php +++ b/src/Browser/BrowserBuilder.php @@ -18,7 +18,7 @@ /** * @author Simon André */ -class BrowserBuilder +final class BrowserBuilder { /** * @var array diff --git a/src/Browser/BrowserContext.php b/src/Browser/BrowserContext.php index b534b4b..066186d 100644 --- a/src/Browser/BrowserContext.php +++ b/src/Browser/BrowserContext.php @@ -12,6 +12,8 @@ use PlaywrightPHP\Configuration\PlaywrightConfig; use PlaywrightPHP\Event\EventDispatcherInterface; +use PlaywrightPHP\Exception\ProtocolErrorException; +use PlaywrightPHP\Exception\TransportException; use PlaywrightPHP\Network\NetworkThrottling; use PlaywrightPHP\Network\Route; use PlaywrightPHP\Page\Page; @@ -60,10 +62,10 @@ public function dispatchEvent(string $eventName, array $params): void { if ('route' === $eventName) { if (!is_string($params['routeId'])) { - throw new \RuntimeException('Invalid routeId in route event'); + throw new ProtocolErrorException('Invalid routeId in route event', 0); } if (!is_array($params['request'])) { - throw new \RuntimeException('Invalid request data in route event'); + throw new ProtocolErrorException('Invalid request data in route event', 0); } $route = new Route( $this->transport, @@ -126,11 +128,11 @@ public function newPage(array $options = []): PageInterface if (isset($response['error'])) { $errorMsg = is_string($response['error']) ? $response['error'] : 'Unknown transport error'; - throw new \RuntimeException('Transport error in newPage: '.$errorMsg); + throw new TransportException('Transport error in newPage: '.$errorMsg); } if (!isset($response['pageId']) || !is_string($response['pageId'])) { - throw new \RuntimeException('No valid pageId returned from transport in newPage'); + throw new ProtocolErrorException('No valid pageId returned from transport in newPage', 0); } $page = new Page($this->transport, $this, $response['pageId'], $this->config); @@ -147,7 +149,7 @@ public function clipboardText(): string ]); if (!is_string($response['value'])) { - throw new \RuntimeException('Invalid clipboard text response'); + throw new ProtocolErrorException('Invalid clipboard text response', 0); } return $response['value']; @@ -210,7 +212,7 @@ public function cookies(?array $urls = null): array ]); if (!is_array($response['cookies'])) { - throw new \RuntimeException('Invalid cookies response'); + throw new ProtocolErrorException('Invalid cookies response', 0); } /** @phpstan-var array> $cookies */ @@ -271,7 +273,7 @@ public function storageState(?string $path = null): array ]); if (!is_array($response['storageState'])) { - throw new \RuntimeException('Invalid storageState response'); + throw new ProtocolErrorException('Invalid storageState response', 0); } /** @phpstan-var array $storageState */ @@ -420,13 +422,13 @@ public function disableNetworkThrottling(): void private function validateTransportArray(mixed $data, string $context = ''): array { if (!is_array($data)) { - throw new \RuntimeException("Invalid {$context} data in transport response"); + throw new ProtocolErrorException("Invalid {$context} data in transport response", 0); } $result = []; foreach ($data as $key => $value) { if (!is_string($key)) { - throw new \RuntimeException("Invalid {$context} payload: non-string key in transport response"); + throw new ProtocolErrorException("Invalid {$context} payload: non-string key in transport response", 0); } $result[$key] = $value; } diff --git a/src/Browser/StorageState.php b/src/Browser/StorageState.php index 2e5f537..7d20c67 100644 --- a/src/Browser/StorageState.php +++ b/src/Browser/StorageState.php @@ -10,6 +10,8 @@ namespace PlaywrightPHP\Browser; +use PlaywrightPHP\Exception\RuntimeException; + /** * Storage state container for cookies and localStorage data. * @@ -64,7 +66,7 @@ public static function fromFile(string $filePath): self $content = \file_get_contents($filePath); if (false === $content) { - throw new \RuntimeException(\sprintf('Failed to read storage state file: %s', $filePath)); + throw new RuntimeException(\sprintf('Failed to read storage state file: %s', $filePath)); } return self::fromJson($content); @@ -111,12 +113,12 @@ public function saveToFile(string $filePath): void { $directory = \dirname($filePath); if (!\is_dir($directory) && !\mkdir($directory, 0755, true) && !\is_dir($directory)) { - throw new \RuntimeException(\sprintf('Failed to create directory: %s', $directory)); + throw new RuntimeException(\sprintf('Failed to create directory: %s', $directory)); } $result = \file_put_contents($filePath, $this->toJson(JSON_PRETTY_PRINT)); if (false === $result) { - throw new \RuntimeException(\sprintf('Failed to write storage state file: %s', $filePath)); + throw new RuntimeException(\sprintf('Failed to write storage state file: %s', $filePath)); } } diff --git a/src/Configuration/PlaywrightConfigBuilder.php b/src/Configuration/PlaywrightConfigBuilder.php index f1ae53f..2333057 100644 --- a/src/Configuration/PlaywrightConfigBuilder.php +++ b/src/Configuration/PlaywrightConfigBuilder.php @@ -265,16 +265,16 @@ public function build(): PlaywrightConfig if ($this->slowMoMs < 0) { throw new \InvalidArgumentException('slowMoMs must be >= 0'); } - if (null !== $this->channel && '' === $this->channel) { + if ('' === $this->channel) { $this->channel = null; } - if (null !== $this->downloadsDir && '' === $this->downloadsDir) { + if ('' === $this->downloadsDir) { $this->downloadsDir = null; } - if (null !== $this->videosDir && '' === $this->videosDir) { + if ('' === $this->videosDir) { $this->videosDir = null; } - if ($this->tracingEnabled && null !== $this->traceDir && '' === $this->traceDir) { + if ($this->tracingEnabled && '' === $this->traceDir) { $this->traceDir = null; } diff --git a/src/Console/ConsoleMessage.php b/src/Console/ConsoleMessage.php index 7fd126f..27eea0d 100644 --- a/src/Console/ConsoleMessage.php +++ b/src/Console/ConsoleMessage.php @@ -10,6 +10,8 @@ namespace PlaywrightPHP\Console; +use PlaywrightPHP\Exception\ProtocolErrorException; + /** * @author Simon André */ @@ -32,7 +34,7 @@ public function type(): string { $type = $this->data['type']; if (!is_string($type)) { - throw new \RuntimeException('Invalid console message type'); + throw new ProtocolErrorException('Invalid console message type', 0); } return $type; @@ -42,7 +44,7 @@ public function text(): string { $text = $this->data['text']; if (!is_string($text)) { - throw new \RuntimeException('Invalid console message text'); + throw new ProtocolErrorException('Invalid console message text', 0); } return $text; @@ -55,7 +57,7 @@ public function args(): array { $args = $this->data['args']; if (!is_array($args)) { - throw new \RuntimeException('Invalid console message args'); + throw new ProtocolErrorException('Invalid console message args', 0); } return $args; @@ -68,13 +70,13 @@ public function location(): array { $location = $this->data['location']; if (!is_array($location)) { - throw new \RuntimeException('Invalid console message location'); + throw new ProtocolErrorException('Invalid console message location', 0); } $result = []; foreach ($location as $key => $value) { if (!is_string($key)) { - throw new \RuntimeException('Invalid console message location: non-string key'); + throw new ProtocolErrorException('Invalid console message location: non-string key', 0); } $result[$key] = $value; } diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php new file mode 100644 index 0000000..f2ee17d --- /dev/null +++ b/src/Exception/RuntimeException.php @@ -0,0 +1,20 @@ + + */ +class RuntimeException extends PlaywrightException +{ +} diff --git a/src/Frame/Frame.php b/src/Frame/Frame.php index 6119b2a..4402eb3 100644 --- a/src/Frame/Frame.php +++ b/src/Frame/Frame.php @@ -11,6 +11,7 @@ namespace PlaywrightPHP\Frame; use PlaywrightPHP\Exception\PlaywrightException; +use PlaywrightPHP\Exception\ProtocolErrorException; use PlaywrightPHP\Locator\Locator; use PlaywrightPHP\Locator\LocatorInterface; use PlaywrightPHP\Transport\TransportInterface; @@ -75,7 +76,7 @@ public function name(): string $response = $this->sendCommand('frame.name'); $value = $response['value'] ?? null; if (!is_string($value)) { - throw new \RuntimeException('Invalid frame.name response'); + throw new ProtocolErrorException('Invalid frame.name response', 0); } return $value; @@ -86,7 +87,7 @@ public function url(): string $response = $this->sendCommand('frame.url'); $value = $response['value'] ?? null; if (!is_string($value)) { - throw new \RuntimeException('Invalid frame.url response'); + throw new ProtocolErrorException('Invalid frame.url response', 0); } return $value; diff --git a/src/Locator/Locator.php b/src/Locator/Locator.php index 52f6a18..015532e 100644 --- a/src/Locator/Locator.php +++ b/src/Locator/Locator.php @@ -11,6 +11,7 @@ namespace PlaywrightPHP\Locator; use PlaywrightPHP\Exception\PlaywrightException; +use PlaywrightPHP\Exception\ProtocolErrorException; use PlaywrightPHP\Exception\TimeoutException; use PlaywrightPHP\Frame\FrameLocator; use PlaywrightPHP\Frame\FrameLocatorInterface; @@ -138,7 +139,7 @@ public function innerHTML(): string $response = $this->sendCommand('locator.innerHTML'); $value = $response['value']; if (!is_string($value)) { - throw new \RuntimeException('Invalid innerHTML response'); + throw new ProtocolErrorException('Invalid innerHTML response', 0); } return $value; @@ -149,7 +150,7 @@ public function innerText(): string $response = $this->sendCommand('locator.innerText'); $value = $response['value']; if (!is_string($value)) { - throw new \RuntimeException('Invalid innerText response'); + throw new ProtocolErrorException('Invalid innerText response', 0); } return $value; @@ -160,7 +161,7 @@ public function inputValue(): string $response = $this->sendCommand('locator.inputValue'); $value = $response['value']; if (!is_string($value)) { - throw new \RuntimeException('Invalid inputValue response'); + throw new ProtocolErrorException('Invalid inputValue response', 0); } return $value; diff --git a/src/Network/Request.php b/src/Network/Request.php index 1a5237b..c5bd7cc 100644 --- a/src/Network/Request.php +++ b/src/Network/Request.php @@ -10,6 +10,8 @@ namespace PlaywrightPHP\Network; +use PlaywrightPHP\Exception\ProtocolErrorException; + /** * @author Simon André */ @@ -27,7 +29,7 @@ public function url(): string { $url = $this->data['url']; if (!is_string($url)) { - throw new \RuntimeException('Invalid URL in request data'); + throw new ProtocolErrorException('Invalid URL in request data', 0); } return $url; @@ -37,7 +39,7 @@ public function method(): string { $method = $this->data['method']; if (!is_string($method)) { - throw new \RuntimeException('Invalid method in request data'); + throw new ProtocolErrorException('Invalid method in request data', 0); } return $method; @@ -96,7 +98,7 @@ public function resourceType(): string { $resourceType = $this->data['resourceType']; if (!is_string($resourceType)) { - throw new \RuntimeException('Invalid resourceType in request data'); + throw new ProtocolErrorException('Invalid resourceType in request data', 0); } return $resourceType; diff --git a/src/Network/Response.php b/src/Network/Response.php index c037416..f39a918 100644 --- a/src/Network/Response.php +++ b/src/Network/Response.php @@ -10,6 +10,7 @@ namespace PlaywrightPHP\Network; +use PlaywrightPHP\Exception\ProtocolErrorException; use PlaywrightPHP\Transport\TransportInterface; /** @@ -35,7 +36,7 @@ public function url(): string { $url = $this->data['url']; if (!is_string($url)) { - throw new \RuntimeException('Invalid URL in response data'); + throw new ProtocolErrorException('Invalid URL in response data', 0); } return $url; @@ -45,7 +46,7 @@ public function status(): int { $status = $this->data['status']; if (!is_int($status)) { - throw new \RuntimeException('Invalid status in response data'); + throw new ProtocolErrorException('Invalid status in response data', 0); } return $status; @@ -55,7 +56,7 @@ public function statusText(): string { $statusText = $this->data['statusText']; if (!is_string($statusText)) { - throw new \RuntimeException('Invalid statusText in response data'); + throw new ProtocolErrorException('Invalid statusText in response data', 0); } return $statusText; @@ -94,11 +95,11 @@ public function body(): string ]); $binary = $response['binary']; if (!is_string($binary)) { - throw new \RuntimeException('Invalid binary response data'); + throw new ProtocolErrorException('Invalid binary response data', 0); } $decoded = base64_decode($binary); if (false === $decoded) { - throw new \RuntimeException('Failed to decode binary response data'); + throw new ProtocolErrorException('Failed to decode binary response data', 0); } $this->body = $decoded; } diff --git a/src/Page/Page.php b/src/Page/Page.php index 7bf0639..18c3a81 100644 --- a/src/Page/Page.php +++ b/src/Page/Page.php @@ -17,6 +17,8 @@ use PlaywrightPHP\Event\EventDispatcherInterface; use PlaywrightPHP\Exception\NetworkException; use PlaywrightPHP\Exception\PlaywrightException; +use PlaywrightPHP\Exception\ProtocolErrorException; +use PlaywrightPHP\Exception\RuntimeException; use PlaywrightPHP\Exception\TimeoutException; use PlaywrightPHP\Frame\Frame; use PlaywrightPHP\Frame\FrameInterface; @@ -178,7 +180,7 @@ public function screenshot(?string $path = null, array $options = []): string ); if (!is_string($finalPath)) { - throw new \RuntimeException('Invalid screenshot path generated'); + throw new RuntimeException('Invalid screenshot path generated'); } $this->logger->debug('Taking screenshot', ['path' => $finalPath, 'options' => $options]); @@ -477,7 +479,7 @@ public function url(): string $response = $this->sendCommand('url'); $url = $response['value']; if (!is_string($url)) { - throw new \RuntimeException('Invalid URL response from transport'); + throw new ProtocolErrorException('Invalid URL response from transport', 0); } return $url; @@ -488,7 +490,7 @@ public function title(): string $response = $this->sendCommand('title'); $title = $response['value']; if (!is_string($title)) { - throw new \RuntimeException('Invalid title response from transport'); + throw new ProtocolErrorException('Invalid title response from transport', 0); } return $title; @@ -512,7 +514,7 @@ public function viewportSize(): ?array || !is_int($viewport['width']) || !is_int($viewport['height']) ) { - throw new \RuntimeException('Invalid viewportSize response from transport'); + throw new ProtocolErrorException('Invalid viewportSize response from transport', 0); } /* @var array{width: int, height: int} $viewport */ @@ -729,12 +731,12 @@ private function createConsoleMessage(array $params): ConsoleMessage private function validateRequestData(mixed $data): array { if (!is_array($data)) { - throw new \RuntimeException('Invalid request data from transport'); + throw new ProtocolErrorException('Invalid request data from transport', 0); } $result = []; foreach ($data as $key => $value) { if (!is_string($key)) { - throw new \RuntimeException('Invalid request data from transport: non-string key'); + throw new ProtocolErrorException('Invalid request data from transport: non-string key', 0); } $result[$key] = $value; } @@ -750,12 +752,12 @@ private function validateRequestData(mixed $data): array private function validateResponseData(mixed $data): array { if (!is_array($data)) { - throw new \RuntimeException('Invalid response data from transport'); + throw new ProtocolErrorException('Invalid response data from transport', 0); } $result = []; foreach ($data as $key => $value) { if (!is_string($key)) { - throw new \RuntimeException('Invalid response data from transport: non-string key'); + throw new ProtocolErrorException('Invalid response data from transport: non-string key', 0); } $result[$key] = $value; } diff --git a/src/Support/Sanitizer.php b/src/Support/Sanitizer.php index 0cf4201..f214730 100644 --- a/src/Support/Sanitizer.php +++ b/src/Support/Sanitizer.php @@ -10,6 +10,8 @@ namespace PlaywrightPHP\Support; +use PlaywrightPHP\Exception\RuntimeException; + /** * Utility class for sanitizing sensitive data from logs and error messages. * @@ -93,11 +95,11 @@ private static function sanitizeObject(object $object): object { $json = json_encode($object); if (false === $json) { - throw new \RuntimeException('Failed to encode object to JSON'); + throw new RuntimeException('Failed to encode object to JSON'); } $array = json_decode($json, true); if (!is_array($array)) { - throw new \RuntimeException('Object could not be converted to array'); + throw new RuntimeException('Object could not be converted to array'); } $sanitized = self::sanitizeArray($array); @@ -126,7 +128,7 @@ private static function sanitizeString(string $string): string foreach ($patterns as $pattern => $replacement) { $result = preg_replace($pattern, $replacement, $string); if (null === $result) { - throw new \RuntimeException('preg_replace failed'); + throw new RuntimeException('preg_replace failed'); } $string = $result; } diff --git a/src/Support/ScreenshotHelper.php b/src/Support/ScreenshotHelper.php index 86fb49c..96d9b33 100644 --- a/src/Support/ScreenshotHelper.php +++ b/src/Support/ScreenshotHelper.php @@ -10,6 +10,8 @@ namespace PlaywrightPHP\Support; +use PlaywrightPHP\Exception\RuntimeException; + /** * Utility for generating screenshot filenames and managing screenshot directories. * @@ -93,7 +95,7 @@ public static function ensureDirectoryExists(string $directory): void } if (!mkdir($directory, 0755, true) && !is_dir($directory)) { - throw new \RuntimeException(sprintf('Failed to create screenshot directory: %s', $directory)); + throw new RuntimeException(sprintf('Failed to create screenshot directory: %s', $directory)); } } diff --git a/src/Testing/PlaywrightTestCaseTrait.php b/src/Testing/PlaywrightTestCaseTrait.php index 7dbf359..2f4bfd7 100644 --- a/src/Testing/PlaywrightTestCaseTrait.php +++ b/src/Testing/PlaywrightTestCaseTrait.php @@ -15,6 +15,7 @@ use PlaywrightPHP\Browser\BrowserContextInterface; use PlaywrightPHP\Browser\BrowserInterface; use PlaywrightPHP\Configuration\PlaywrightConfig; +use PlaywrightPHP\Exception\RuntimeException; use PlaywrightPHP\Locator\LocatorInterface; use PlaywrightPHP\Page\PageInterface; use PlaywrightPHP\PlaywrightClient; @@ -137,7 +138,7 @@ protected function tearDownPlaywright(): void $failuresDir = getcwd().'/test-failures'; if (!is_dir($failuresDir)) { if (!mkdir($failuresDir, 0777, true) && !is_dir($failuresDir)) { - throw new \RuntimeException(sprintf('Directory "%s" was not created', $failuresDir)); + throw new RuntimeException(sprintf('Directory "%s" was not created', $failuresDir)); } } $testName = 'test'; diff --git a/src/Transport/TransportFactory.php b/src/Transport/TransportFactory.php index ba4fb2a..515eab8 100644 --- a/src/Transport/TransportFactory.php +++ b/src/Transport/TransportFactory.php @@ -11,6 +11,7 @@ namespace PlaywrightPHP\Transport; use PlaywrightPHP\Configuration\PlaywrightConfig; +use PlaywrightPHP\Exception\RuntimeException; use PlaywrightPHP\Node\NodeBinaryResolver; use PlaywrightPHP\Transport\JsonRpc\JsonRpcTransport; use PlaywrightPHP\Transport\JsonRpc\ProcessLauncher; @@ -27,7 +28,7 @@ public function create(PlaywrightConfig $config, LoggerInterface $logger): Trans $serverScriptPath = $serverManager->getServerScriptPath(); if (!$serverScriptPath || !file_exists($serverScriptPath)) { - throw new \RuntimeException('playwright-server.js not found.'); + throw new RuntimeException('playwright-server.js not found.'); } $nodePath = $config->nodePath ?? (new NodeBinaryResolver())->resolve();